const int buttonPin = 14;
const int ledPin = 4;
bool lastButtonState = LOW; // status tombol terakhir
bool ledState = LOW; // status LED
void setup() {
pinMode(buttonPin, INPUT); // tombol sebagai input
pinMode(ledPin, OUTPUT); // LED sebagai output
}
void loop() {
bool buttonState = digitalRead(buttonPin); // cek status tombol
if(buttonState != lastButtonState){ // jika status tombol berubah
if(buttonState == LOW){ // jika tombol berubah ke status LOW (dilepas)
if(ledState == HIGH){ // jika status LED hidup
digitalWrite(ledPin, LOW); // maka LED dimatikan
ledState = LOW;
}else{
digitalWrite(ledPin, HIGH); // maka led dihidupkan
ledState = HIGH;
}
}
delay(50); // debaounching
}
lastButtonState = buttonState; // simpan status tombol untuk loop
}