/* LightToggleDebounced Toggle an LED on and off (with s/w debouncing) */ const int pushButtonPin = 2; // connect the push button to digital pin 2 const int ledPin = 13; // connect the LED to pin 13 const int debounceTime = 5; // number of millisecods to delay after button press boolean ledState = LOW; // used to set LED boolean lastButtonState = HIGH; // value of buttonState from previous loop iteration boolean buttonState = HIGH; boolean debounce(int pinNum) { delay(debounceTime); return digitalRead(pinNum); } 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() { buttonState = debounce(pushButtonPin); // read the input pin // if button goes down and previously it was high... if (buttonState == LOW && lastButtonState == HIGH) { ledState = !ledState; } lastButtonState = buttonState; digitalWrite(ledPin, ledState); // turn the LED on or off }