const int ledPin = 9; // Pin LED
const int buttonPin = 8; // Pin Tombol
bool ledState = false; // Keadaan awal LED (mati)
bool buttonState = true; // Keadaan tombol
bool lastButtonState = false;
void setup() {
pinMode(ledPin, OUTPUT);
pinMode(buttonPin, INPUT);
digitalWrite(ledPin, LOW); // Matikan LED awalnya
}
void loop() {
// Baca keadaan tombol
buttonState = digitalRead(buttonPin);
// Jika tombol ditekan (logika HIGH)
if (buttonState != lastButtonState) {
if (buttonState == HIGH) {
ledState = !ledState; // Balikkan keadaan LED
digitalWrite(ledPin, ledState); // Nyalakan atau matikan LED
}
delay(50); // Debounce delay
}
lastButtonState = buttonState; // Simpan keadaan tombol sebelumnya
}