hey guys,
I am new to the arduino and need some advice on counting button presses. My main goal was to blink an led 5 times when a button is pressed once, 10 times if pressed 2 times, and 15 times if pressed yet a 3rd time. My problem is that the arduino, well, its like its reading the code all out of order.
so for example;
1st press it says its been pressed 1 time
2nd press, it still says 1 time
3rd press it says its been pressed 2 times
and so on.
here is my code
I have taken out the LED program and used the println funtion to simplify it (and I have tested this code and have gotten the same result as I did with the code for the LED).
If anyone can give a tip or some advice as to how or what I am doing wrong that would be awsome:)
int switchPin = 2; // switch is connected to pin 2
int val; // variable for reading the pin status
int buttonState; // variable to hold the button state
int buttonPresses = 0; // how many times the button has been pressed
void setup() {
pinMode(switchPin, INPUT); // Set the switch pin as input
Serial.begin(9600); // Set up serial communication at 9600bps
buttonState = digitalRead(switchPin); // read the initial state
}
void loop(){
val = digitalRead(switchPin);
if (val != buttonState) {
if (val == LOW) {
buttonPresses++;
Serial.println("1st button press");
//this is were the code for input/output code for the pins would go(like turning on an led etc).
}
}
buttonState = val;
val = digitalRead(switchPin);
if (val==HIGH && buttonState==LOW);
if (val != buttonState) {
if (val == LOW) {
buttonPresses++;
Serial.println("2nd button press");
//this is were the code for input/output code for the pins would go(like turning on an led etc).
}
}
buttonState = val; // save the new state in our variable
if (val==HIGH && buttonState==LOW);
if (val != buttonState) {
if (val == LOW) {
buttonPresses++;
Serial.println("3rd button press");
//this is were the code for input/output code for the pins would go(like turning on an led etc).
}
}
buttonState = val;
}
Thanks