//Constant wont change
const int buttonPin = 33; // ESP32connected to button's pin
const int ledBLUE = 32; // ESP32connected to BLUE led's pin
const int ledRED = 34; // ESP32connected to RED led's pin
const int ledGREEN = 35; // ESP32connected to GREEN 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() {
Serial.begin(9600); // initialize serial
pinMode(buttonPin, INPUT_PULLUP); // set ESP32 pin to input pull-up mode
pinMode(ledBLUE, OUTPUT); // set ESP32 pin to output mode
pinMode(ledRED, OUTPUT); // set ESP32 pin to output mode
pinMode(ledGREEN, OUTPUT); // set ESP32 pin to output mode
currentButtonState = digitalRead(buttonPin);
}
void loop() {
lastButtonState = currentButtonState; // save the last state
currentButtonState = digitalRead(buttonPin); // read new state
if(lastButtonState == HIGH && currentButtonState == LOW) {
Serial.println("The button is pressed");
// toggle state of LED
ledState = !ledState;
// control LED arccoding to the toggled state
digitalWrite(ledGREEN, ledState);
}
delay(100);
}