/* LightToggleLogic Toggle an LED on and off. */ const int pushButtonPin = 2; // connect the push button to digital pin 2 const int ledPin = 13; // connect the LED to pin 13 boolean ledState = LOW; // used to set LED boolean lastButtonState = LOW; // value of buttonState from previous loop iteration void setup() { pinMode(pushButtonPin, INPUT); // make the pushbutton's pin an input digitalWrite(pushButtonPin, HIGH); // turn on pullup resistors pinMode(ledPin, OUTPUT); // make LED's pin an output digitalWrite(ledPin, ledState); } void loop() { boolean buttonState = digitalRead(pushButtonPin); // read the input pin // if button goes down and previously it was high... if (buttonState == LOW && lastButtonState == HIGH) { if (ledState == HIGH) // toggle LED ledState = LOW; else ledState = HIGH; digitalWrite(ledPin, ledState); // turn the LED on or off } lastButtonState = buttonState; }