/*
* 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-button-led
*/
#define BUTTON_PIN 19 // ESP32 pin GPIO18, which connected to button
#define LED_PIN 16 // ESP32 pin GPIO21, which connected to led
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd = LiquidCrystal_I2C(0x27, 16, 2);
// variables will change:
int button_state = 0; // variable for reading the button status
void setup() {
// initialize the LED pin as an output:
Serial.begin(9600);
lcd.init();
lcd.backlight();
lcd.clear();
lcd.setCursor(0, 0);
pinMode(LED_PIN, OUTPUT);
// initialize the button pin as an pull-up input:
// the pull-up input pin will be HIGH when the button is open and LOW when the button is pressed.
pinMode(BUTTON_PIN, INPUT_PULLUP);
}
void loop() {
// read the state of the button value:
lcd.setCursor(0,0);
lcd.print("GG test:");
button_state = digitalRead(BUTTON_PIN);
Serial.print("first loop state=");
Serial.println(button_state);
// control LED according to the state of button
if (button_state == LOW) // if button is pressed
{ digitalWrite(LED_PIN, HIGH); // turn on LED
lcd.clear();
lcd.setCursor(0,0);
lcd.print("HEEEEEY");
delay (2000);
}
else // otherwise, button is not pressing
digitalWrite(LED_PIN, LOW); // turn off LED
}
/* // initialize the LED pin as an output:
PULLDOWN
pinMode(LED_PIN, OUTPUT);
// initialize the button pin as an pull-up input:
// the pull-up input pin will be HIGH when the button is open and LOW when the button is pressed.
pinMode(BUTTON_PIN, INPUT_PULLDOWN);
}
void loop() {
// read the state of the button value:
button_state = digitalRead(BUTTON_PIN);
// control LED according to the state of button
if (button_state == HIGH) // if button is pressed
digitalWrite(LED_PIN, HIGH); // turn on LED
else // otherwise, button is not pressing
digitalWrite(LED_PIN, LOW); // turn off LED
}*/