Lilypad with LilyPad Push Button

Hello Everyone,

I have a Lilypad for a project, and recently purchased a lilypad button board (https://www.sparkfun.com/products/8776). I wanted to come here to ask advice on how to hook this board up. Previous push buttons I’ve used have had three connections: power, ground, digital i/o (used as a toggle, or some function). However, this board only has two connections, and so I’m not entirely sure how to use it.

I have an RGB LED project which has several “modes” i.e. the device can display different configurations of LEDs and colors. Right now, to switch between the modes, I have to manually reprogram it. What I’d like to do is have this push button “cycle” through the different modes. Currently, I have it wired such that one end of the push button is connected to ground, and the other to on of the digital I/O pins. I write HIGH to that pin every loop cycle, and check to see if it has gone low (i.e. the switch has closed and the digital I/O pin has now been grounded).

I was thinking something like this, but I can’t seem to make it work. What I’m looking for is advice on the overall idea/framework/architecture.

Thank you in advance!

Chris

//Define button pin
const int  buttonPin = 2;    // the pin that the pushbutton is attached to

// variables will change:
int buttonState = 0;         // variable for reading the pushbutton status
int buttonPushCounter = 1;

//Set max modes
int maxModes = 3;

void setup() {
  
   // initialize the button pin as a input:
   // initialize the pushbutton pin as an input:
  pinMode(buttonPin, INPUT);  

  //initialize the buttonPin as output
  digitalWrite(buttonPin, HIGH);  
  
}


void loop(){
    // read the state of the pushbutton value:
    buttonState = digitalRead(buttonPin);
  
    // check if the pushbutton is pressed.
    // if it is, the buttonState is HIGH:
    if (buttonState == HIGH) {     

    } 
    else {
      
      buttonPushCounter++;
      if(buttonPushCounter>maxModes)
      {
        buttonPushCounter = 1;
      }
    }
   
   switch (buttonPushCounter) {
     case 1:
       mode1();
     break;
     
     case 2:
       mode2();
     break;
     
     case 3:
       mode3();
 
     break;
     
   }
  
}

The switch is a normally open (NO) momentary contact switch, quite common for your use. I would point you to this tutorial on the best way to use it.

http://arduino.cc/en/Tutorial/InputPullupSerial

In your code you need to use the INPUT_PULLUP condition when declaring the pin.

pinMode(buttonPin, INPUT_PULLUP);

When the pin is not pushed, the code will read a HIGH. When pushed it’ll read a LOW. You need to read (poll) the switch often enough to catch a quick push and release. Luckily microcontrollers are much faster than humans.

The next problem to be solved when using these switches is switch bounce. When you push a switch it will close but then “bounce” back to open then closed then open and eventually settling to closed. Same thing happens then the switch is released. Your LP micro, sampling very fast, may catch a bounce or two and mistake that for a push or two. There are many ways to “debounce” a switch but for now you might want to use this library of code.

http://playground.arduino.cc/Code/Bounce

http://www.allaboutcircuits.com/vol_4/chpt_4/4.html

OK, now you’ll have code to read the switch and not be fooled by bouncing. The rest of your outline looks good to me. Increment a push counter and use that to switch amongst the modes.

FWIW this code snippet …

   // initialize the pushbutton pin as an input:
  pinMode(buttonPin, INPUT); 
  //initialize the buttonPin as output
  digitalWrite(buttonPin, HIGH);

… doesn’t make the pin an output but rather enables the internal pullup resistor. It’s the old way of doing it. The method above is the recommended way now.

You might also want to correct this comment in the code.

    // check if the pushbutton is pressed.
    // if it is, the buttonState is HIGH:

Mee_n_Mac,

Thank you very much for all this great information! It is going to be very useful I’m sure, I’ll go try some of it out now!

Chris