const int buttonPin = 2;
int oldValue = LOW; // default/idle value for pin 8 is low.
#define LED 5
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
Serial.println("Hello, ESP32!");
pinMode(buttonPin, INPUT);
pinMode(LED, OUTPUT);
}
int val = HIGH;
int newValue = digitalRead(buttonPin);
void loop() {
// put your main code here, to run repeatedly:
// Serial.println(newValue);
// delay(1000);
// Check if the value was changed,
// by comparing it with the previous value.
// if(newValue != oldValue)
// {
// if(newValue == HIGH)
// {
// digitalWrite(LED, HIGH);
// delay(100);
// Serial.println("The button is pressed.");
// }
// else
// {
// Serial.println("The button is released.");
// }
// // Remember the value for the next time.
// oldValue = newValue;
// digitalWrite(LED, LOW);
// }
if(newValue != val){
if(newValue == HIGH){
Serial.println("The button is pressed. ");
digitalWrite(LED, HIGH);
val = HIGH;
}
else{
Serial.println("The button is released");
digitalWrite(LED, LOW);
val = LOW;
}
}
// delay(10000); // this speeds up the simulation
}