Wednesday 24 September 2014

Arduino – One Button Multiple Functions

One of the things that I want to do with my LED functions is to control them with a single momentary switch. This means that I’ll have less hardware that the user needs to interact with, but the trade-off is that the user will need to press the button multiple times to get the function that they want.

The idea is to use a momentary switch that supplies current to the three LED functions (Glow, Chase, Lamp). I plan to do this by implementing an ATTiny85 to serve as a switch controller. First, though, the proof of concept is by controlling three LED with one momentary switch.

The breadboard is fairly straight forward. The Momentary Switch is connected to Pin 12 on the UNO and has a pull-down resistor. The three LED (acting as proxies for the ATTiny85 that will control the LED functions) connect to Pins 7, 8 and 9 on the UNO. When the button is pressed, a variable is incremented and passed to a switch statement, so that you get all possible combinations of the three LED (X00, 0X0, 00X, XX0, 0XX, X0X, XXX and 000) (I think).

One Button Three Functions

I’ve had a bit of a look around on the interweb and haven’t found anything startling … so I’ve used the basic Button example on the Arduino site and mixed it up with the switch control to arrive at a design that I think will work.

Most of the smarts for this happen in the sketch.

int buttonPin = 12;

int fi = 7;
int se = 8;
int th = 9;

int buttonPushCounter = 0;  
int buttonState = 0;        
int lastButtonState = 0;    

void setup()
{
  Serial.begin(9600);
  pinMode(buttonPin, INPUT);
  pinMode(fi, OUTPUT);
  pinMode(se, OUTPUT);
  pinMode(th, OUTPUT);
 
  digitalWrite(fi, HIGH);
  digitalWrite(se, HIGH);
  digitalWrite(th, HIGH);
  delay(300);
  digitalWrite(fi, LOW);
  digitalWrite(se, LOW);
  digitalWrite(th, LOW);
}

void loop()
{
// read the pushbutton input pin:
  buttonState = digitalRead(buttonPin);

  // compare the buttonState to its previous state
  if (buttonState != lastButtonState) {
    // if the state has changed, increment the counter
    if (buttonState == HIGH) {
      // if the current state is HIGH then the button
      // wend from off to on:
      buttonPushCounter++;
      if(buttonPushCounter == 9){ buttonPushCounter = 1;}
      Serial.println("on");
      Serial.print("number of button pushes:  ");
      Serial.println(buttonPushCounter);
    }
    else {
      // if the current state is LOW then the button
      // went from on to off:
      Serial.println("off");
    }
  }
  // save the current state as the last state,
  //for next time through the loop
  lastButtonState = buttonState;
  switch(buttonPushCounter)
  {
    case 1:
      digitalWrite(fi, HIGH);
      digitalWrite(se, LOW);
      digitalWrite(th, LOW);
      break;
    case 2:
      digitalWrite(fi, LOW);
      digitalWrite(se, HIGH);
      digitalWrite(th, LOW);
      break;
    case 3:
      digitalWrite(fi, LOW);
      digitalWrite(se, LOW);
      digitalWrite(th, HIGH);
      break;
    case 4:
      digitalWrite(fi, HIGH);
      digitalWrite(se, HIGH);
      digitalWrite(th, LOW);
      break;
    case 5:
      digitalWrite(fi, HIGH);
      digitalWrite(se, LOW);
      digitalWrite(th, HIGH);
      break;
    case 6:
      digitalWrite(fi, LOW);
      digitalWrite(se, HIGH);
      digitalWrite(th, HIGH);
      break;
    case 7:
      digitalWrite(fi, HIGH);
      digitalWrite(se, HIGH);
      digitalWrite(th, HIGH);
      break;
    case 8:
      digitalWrite(fi, LOW);
      digitalWrite(se, LOW);
      digitalWrite(th, LOW);
      break;
  }
  Serial.println(buttonState);
}

I think that I can probably simplify the code a bit more, but it works OK. The only real problem that I have with this code is that the button press has to be fairly confident … a little press does nothing.

I guess that you could scale the number of functions up to the number of pins that you have available, or maybe a shift register? Dunno, I haven’t tried that yet.

The next step with this design is to replace the 3 LED with 3 ATTiny85 to see if that works. The plan is to connect Vcc on the ATTiny85 to Pins 7, 8, 9 of the UNO, if it works the way I hope, then the button press will turn each of the ATTiny85 on and off according to the switch statement. We’ll see.

No comments:

Post a Comment

Paypal Donations

Donations to help me to keep up the lunacy are greatly appreciated, but NOT mandatory.