/*
* This ESP32 code is created by esp32io.com
*
* This ESP32 code is released in the public domain
*
* For more detail (instruction and wiring diagram), visit https://esp32io.com/tutorials/esp32-led-blink-without-delay
*/
#define LED_PIN 21 // ESP32 pin GPIO21 connected to LED
#define BUTTON_PIN 18 // ESP32 pin GPIO18 connected to button
#define BLINK_INTERVAL 1000 // interval at which to blink LED (milliseconds)
// Variables will change:
int ledState = LOW; // ledState used to set the LED
int previousButtonState = LOW; // will store last time button was updated
void setup() {
Serial.begin(9600);
// set the digital pin as output:
pinMode(LED_PIN, OUTPUT);
// set the digital pin as an input:
pinMode(BUTTON_PIN, INPUT_PULLUP);
}
void loop() {
// if the LED is off turn it on and vice-versa:
ledState = (ledState == LOW) ? HIGH : LOW;
// set the LED with the ledState of the variable:
digitalWrite(LED_PIN, ledState);
delay(BLINK_INTERVAL); // If button is pressed during this time, Arduino CANNOT detect
int currentButtonState = digitalRead(BUTTON_PIN);
if (currentButtonState != previousButtonState) {
// print out the state of the button:
Serial.println(currentButtonState);
// save the last state of button
previousButtonState = currentButtonState;
}
// DO OTHER WORKS HERE
}