// constants won't change
#define button 4 // ESP32connected to button's pin
#define LED 2 // ESP32 pin connected to LED's pin
// variables will change:
int ledState = LOW; // the current state of LED
int lastButtonState; // the previous state of button
int currentButtonState; // the current state of button
void setup()
{
pinMode(button, INPUT_PULLUP); // set ESP32 pin to input pull-up mode
pinMode(LED, OUTPUT); // set ESP32 pin to output mode
currentButtonState = digitalRead(button);
}
void loop() {
lastButtonState = currentButtonState; // save the last state
currentButtonState = digitalRead(button); // read new state
if(lastButtonState == HIGH && currentButtonState == LOW)
{
ledState = !ledState;
// control LED arccoding to the toggled state
digitalWrite(LED, ledState);
}
delay(100);
}