Hello. I'm working on a project that involves two switches an a push counter. I want one switch to increase the number the counter is at, while the other should decrease it. I used the buttonState change code found here but modified it a bit to work with my project. I added the second button input and removed the LED. I'm testing my code out now with buttons, but every time I pull up the serial monitor, there's no information. At one point it did read "number of button pushes: 0", but wouldn't change after that. Then it just stopped printing out any information whatsoever. Here's my code:
const int buttonPin1 = 2; // the pin that the pushbutton is attached to
const int buttonPin2 = 4;
// Variables will change:
int buttonPushCounter = 0; // counter for the number of button presses
int buttonState1 = 0; // current state of the button
int lastButtonState1 = 0; // previous state of the button
int buttonState2 = 0;
int lastButtonState2 = 0;
void setup() {
// initialize the button pin as a input:
pinMode(buttonPin1, INPUT);
// initialize the LED as an output:
pinMode(buttonPin2, INPUT);
// initialize serial communication:
Serial.begin(9600);
}
void loop() {
// read the pushbutton input pin:
buttonState1 = digitalRead(buttonPin1);
buttonState2 = digitalRead(buttonPin2);
// compare the buttonState to its previous state
if (buttonState1 != lastButtonState1) {
// if the state has changed, increment the counter
if (buttonState1 == HIGH) {
// if the current state is HIGH then the button
// wend from off to on:
buttonPushCounter++;
}
if (buttonState2 != lastButtonState2) {
if (buttonState2 == HIGH) {
buttonPushCounter--;
Serial.println("on");
Serial.print("number of button pushes: ");
Serial.println(buttonPushCounter);
}
else {
// if the current state is LOW then the button
// wend from on to off:
Serial.println("off");
}
}
// save the current state as the last state,
//for next time through the loop
lastButtonState1 = buttonState1;
lastButtonState2 = buttonState2;
}
}
as you can probably guess, I'm new to this, so I apologize if my error is some big oversight. It could also be in my circuit, if you follow the link to arduino's site, it looks like that except I added a second button. If you need any more information let me know.
Thank you so much!