#define RELAY_PIN 18
#define buttonPin 2 // the number of the pushbutton pin
// variables will change:
// variables for reading the pushbutton status
int buttonStatePrevious = LOW;
int buttonStateCurrent = LOW;
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
//Serial.println("Hello, ESP32!");
pinMode(RELAY_PIN, OUTPUT);
// initialize the pushbutton pin as an input:
pinMode(buttonPin, INPUT);
}
void buttonPressed(){
int buttonPush = LOW;
buttonPush = digitalRead(buttonPin);
if (buttonPush == HIGH && buttonStatePrevious == LOW){
buttonStateCurrent = HIGH;
buttonStatePrevious = buttonStateCurrent;
}
else if(buttonPush == HIGH && buttonStatePrevious == HIGH){
buttonStateCurrent = LOW;
buttonStatePrevious = buttonStateCurrent;
}
}
void loop() {
buttonPressed();
if( buttonStateCurrent == HIGH){
digitalWrite(RELAY_PIN, HIGH); // turn on
}else if(buttonStateCurrent == LOW){
digitalWrite(RELAY_PIN, LOW); // turn off
}
}