#define BUTTON_PIN 21 //GPIO21 pin connected to button
//Variables will change
int lastState = LOW; //The previous state from the input pin
int currentState; //The current reading from the input pin
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
// Initialize the pushbutton pin as an input pull-up
// the pull-up input pin will be HIGH when the switch is open and LOW when the switch is closed.
pinMode(BUTTON_PIN, INPUT_PULLUP);
}
void loop() {
// put your main code here, to run repeatedly:
// read the state of the button
currentState = digitalRead(BUTTON_PIN);
if (lastState == HIGH && currentState == LOW)
Serial.println("The button is pressed");
else if (lastState == LOW && currentState == HIGH)
Serial.println("The button is released");
// save the last state
lastState = currentState;
}