Sub topic 10
Blinking and Button Handling Together
This chapter assumes that you know the basic Arduino sketch structure and can use pinMode(), digitalRead(), digitalWrite(), if statements, Boolean variables, and integer types in C++. You also need a pushbutton and an LED circuit.
The goal is to run two jobs in one loop():
- Blink the built-in LED every 500 milliseconds.
- Read and debounce a button without stopping the blink.
- Toggle an external LED once for each button press.
Wiring the button
Use the Arduino's internal pull-up resistor. Connect one side of the button to digital pin 2 and the other side to GND. Connect an external LED, with a suitable resistor, to pin 8 if you want a separate output LED.
| Part | Connection |
|---|---|
| Button | Pin 2 and GND |
| External LED | Pin 8 through a resistor |
| Built-in LED | `LED_BUILTIN` |
With INPUT_PULLUP, the input has inverted logic:
HIGHmeans the button is released.LOWmeans the button is pressed.
Why `delay()` does not work well here
A call such as delay(500) stops the processor from running the rest of the sketch. During that wait, the sketch cannot promptly read the button or perform another task. A button press can be missed, and the program feels unresponsive.
Instead, millis() returns the number of milliseconds since the board started. Each pass through loop() checks whether enough time has elapsed. The loop remains available for other work.
Combining blinking and debouncing
The following sketch uses separate timing variables for the two jobs. The button debounce interval is 30 milliseconds. A raw button reading must remain unchanged for that period before it becomes the stable state.
const uint8_t BUTTON_PIN = 2;
const uint8_t PRESS_LED_PIN = 8;
const unsigned long DEBOUNCE_MS = 30;
const unsigned long BLINK_MS = 500;
int lastReading = HIGH;
int stableButtonState = HIGH;
unsigned long lastRawChange = 0;
unsigned long previousBlink = 0;
bool blinkState = LOW;
bool pressLedState = LOW;
void setup() {
pinMode(BUTTON_PIN, INPUT_PULLUP);
pinMode(PRESS_LED_PIN, OUTPUT);
pinMode(LED_BUILTIN, OUTPUT);
}
void loop() {
unsigned long now = millis();
// Button sampling and debounce
int reading = digitalRead(BUTTON_PIN);
if (reading != lastReading) {
lastRawChange = now;
lastReading = reading;
}
if ((now - lastRawChange >= DEBOUNCE_MS) &&
(reading != stableButtonState)) {
stableButtonState = reading;
if (stableButtonState == LOW) {
pressLedState = !pressLedState;
digitalWrite(PRESS_LED_PIN, pressLedState);
}
}
// Independent non-blocking blink task
if (now - previousBlink >= BLINK_MS) {
previousBlink = now;
blinkState = !blinkState;
digitalWrite(LED_BUILTIN, blinkState);
}
}The expression now - previousBlink measures elapsed time. Since the variables are unsigned, this subtraction continues to work when the millis() counter rolls over from its maximum value back to zero.
The button action occurs only when stableButtonState changes to LOW. Holding the button does not repeatedly toggle the LED because the state does not change again until the button is released and pressed.
Exercise
Exercise
Change the sketch so the built-in LED blinks every 100 milliseconds while the button press LED changes state only once per press. Which constants must you change, and why does holding the button not cause repeated changes?
Show answer
Change BLINK_MS to 100. The press LED still changes only when the debounced stable state changes to LOW; holding the button produces no new state change.