Code: Select all
#define BUTTON_PIN 21 // GIOP21 pin connected to button
// Counter variable
int counter = 0;
// Variables will change:
int lastState = HIGH; // the previous state from the input pin
int currentState; // the current reading from the input pin
void setup() {
Serial.begin(115200);
// initialize the pushbutton pin as an pull-up input
pinMode(BUTTON_PIN, INPUT_PULLUP);
}
void loop() {
// read the state of the switch/button:
currentState = digitalRead(BUTTON_PIN);
// compare last state to the new one
if(lastState == LOW && currentState == HIGH)
counter = counter ++;
Serial.println(counter);
// save the last state
lastState = currentState;
delay (50);
}