int relayPin = 19;
int buttonPin = 8;
bool state = false;
bool lastButton = HIGH;
void setup() {
pinMode(relayPin, OUTPUT);
pinMode(buttonPin, INPUT_PULLUP);
}
void loop() {
bool currentButton = digitalRead(buttonPin);
// phát hiện nhấn (HIGH → LOW)
if (lastButton == HIGH && currentButton == LOW) {
state = !state;
// relay active LOW
digitalWrite(relayPin, state ? LOW : HIGH);
}
lastButton = currentButton;
delay(50);
}