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;
}
}