const int buttonPin = 2;
const int ledPin = LED_BUILTIN;
//counter for the number of button presses
int buttonPushCounter = 0;
//current state of the button
int buttonState = 0;
//previous state of the button
int lastButtonState = 0;
void setup() {
pinMode(buttonPin, INPUT);
pinMode(ledPin, OUTPUT);
Serial.begin(9600);
}
void loop() {
// read the pushbutton input pin:
buttonState = digitalRead(buttonPin);
// compare the buttonState to its previous state
if (buttonState != lastButtonState) {
// if the state has changed, increment the counter
if (buttonState == HIGH) {
buttonPushCounter++;
Serial.println("on");
Serial.println("number of button pushes: ");
Serial.println(buttonPushCounter);
} else {
Serial.println("off");
}
delay(50);
}
// save state
lastButtonState = buttonState;
}