#define BUTTON_PIN 21 // GPIO21 pin connected to button
#define ledpin 2
// 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(9600);
// initialize the pushbutton pin as an pull-up input
pinMode(BUTTON_PIN, INPUT_PULLUP);
pinMode(ledpin,OUTPUT);
}
void loop() {
// read the state of the switch/button:
currentState = digitalRead(BUTTON_PIN);
if (lastState == HIGH && currentState == LOW){
Serial.println("The button is pressed");
digitalWrite(ledpin, HIGH);
}
else if (lastState == LOW && currentState == HIGH){
Serial.println("The button is released");
digitalWrite(ledpin,LOW);
}
// save the last state
lastState = currentState;
}