Hi,
I have an Arduino Lilypad USB and a Lilypad Button Board. I'm simply trying to read when the button is pressed or not, but that's not working.
I know a little about bouncing, i.e when I press the button and release, it will oscillate back to the High value.
But this does not seem to be a bouncing issue : when I run the following program and don't press the button at all any time, it reads Low and High values alternatively...
What's the problem thanks ?
NB. I have connected the S of the button board to 2 (I also tried A2, A3...) and - to -.
int buttonPin = 2;
int nbPressed = 0;
void setup()
{
pinMode(buttonPin, INPUT);
pinMode(13, OUTPUT);
// Initialize Serial, set the baud rate to 9600 bps.
Serial.begin(9600);
nbPressed = 0;
}
void loop()
{
digitalWrite(13, HIGH);
int buttonValue = digitalRead(buttonPin);
if (buttonValue == LOW) {
Serial.print("Button is pressed ");
nbPressed = nbPressed + 1;
Serial.println(nbPressed);
} else {
Serial.println("Button is NOT pressed");
}
// Delay so that the text doesn't scroll too fast on the Serial Monitor.
// Adjust to a larger number for a slower scroll.
delay(1000);
digitalWrite(13, LOW);
delay(500);
}