// Program toogle Switch On - Switch Off
int buttonPin = 7; // the pin that the pushbutton is attached to
int ledPin = 13; // the pin that the LED is attached to
int buttonState = 0; // current state of the button
int lastButtonState = 0; // previous state of the button
int buttonPushCounter = 0; // counter for the number of button presses
void setup() {
pinMode(buttonPin, INPUT);
pinMode(ledPin, OUTPUT);
}
void loop() {
buttonState = digitalRead(buttonPin); // membaca nilai tombol tekan (sensor digital)
if (buttonState != lastButtonState) { // jika nilai sekarang tidak sama dengan nilai terakhir
if (buttonState == HIGH) {
buttonPushCounter++;
}
}
lastButtonState = buttonState;
if (buttonPushCounter % 2 == 0) {
digitalWrite(ledPin, HIGH);
}
else {
digitalWrite(ledPin, LOW);
}
}