// The button's and the LED's pins const int BUTTON_PIN = 2; const int LED_PIN = 8; // Time to let the contacts settle (ms) const unsigned long BOUNCE_TIME = 50; bool ledOn = false; int lastReading = HIGH; int presses = 0; void setup() { // Pull-up: a released button reads HIGH pinMode(BUTTON_PIN, INPUT_PULLUP); pinMode(LED_PIN, OUTPUT); Serial.begin(9600); Serial.println("Press the button to turn the LED on or off"); } void loop() { int reading = digitalRead(BUTTON_PIN); // Act only when the button changes if (reading != lastReading) { // LOW: the button was just pressed if (reading == LOW) { ledOn = !ledOn; digitalWrite(LED_PIN, ledOn ? HIGH : LOW); presses++; Serial.print("Press "); Serial.print(presses); Serial.println(ledOn ? ": LED on" : ": LED off"); } lastReading = reading; // Wait while the contacts bounce delay(BOUNCE_TIME); } }