0
0
Arduinoprogramming~10 mins

Interrupt-driven button handling in Arduino - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Interrupt-driven button handling
Setup Interrupt
Wait for Button Press
Interrupt Triggered?
NoWait
Yes
Run ISR (Interrupt Service Routine)
Handle Button Press
Return to Main Loop
The program sets up an interrupt for the button pin, waits for a press, then runs a special function (ISR) immediately when pressed, handling the event quickly before returning to normal.
Execution Sample
Arduino
volatile bool buttonPressed = false;

void setup() {
  pinMode(2, INPUT_PULLUP);
  attachInterrupt(digitalPinToInterrupt(2), buttonISR, FALLING);
}

void buttonISR() {
  buttonPressed = true;
}

void loop() {
  if (buttonPressed) {
    // Handle button press event
    buttonPressed = false;
  }
}
This code sets up an interrupt on pin 2 to detect button presses and sets a flag when pressed.
Execution Table
StepEventInterrupt Triggered?ISR Called?buttonPressed ValueMain Loop Action
1Program startsNoNofalseSetup pins and interrupt
2Waiting for button pressNoNofalseIdle or other tasks
3Button pressed (pin 2 FALLING)YesYestrueISR sets buttonPressed to true
4Return to main loopNoNotrueMain loop detects buttonPressed true, handles event
5ButtonPressed flag resetNoNofalseMain loop resets buttonPressed after handling
6Waiting for next pressNoNofalseIdle or other tasks
ExitProgram runs continuouslyNoNofalse or trueLoop continues waiting for interrupts
💡 Program runs indefinitely, responding to button presses via interrupts.
Variable Tracker
VariableStartAfter ISR CallAfter Main Loop HandlingFinal
buttonPressedfalsetruefalsefalse
Key Moments - 3 Insights
Why does buttonPressed change to true immediately when the button is pressed?
Because the interrupt triggers the ISR right away, setting buttonPressed to true as shown in step 3 of the execution_table.
Why do we use 'volatile' for buttonPressed?
Because buttonPressed is changed inside an ISR and read in the main loop, 'volatile' tells the compiler not to optimize it away, ensuring correct updates as seen in variable_tracker.
What happens if we don't reset buttonPressed after handling?
The main loop will think the button is still pressed and may repeatedly handle the event, as shown in step 4 and 5 where resetting is necessary.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the value of buttonPressed right after the ISR is called?
Afalse
Btrue
Cundefined
Dnull
💡 Hint
Check row 3 in the execution_table where ISR sets buttonPressed to true.
At which step does the main loop reset buttonPressed to false?
AStep 2
BStep 3
CStep 5
DStep 4
💡 Hint
Look at the Main Loop Action column in step 5 of the execution_table.
If we remove 'volatile' from buttonPressed, what might happen?
AbuttonPressed might not update correctly in main loop
BButton won't trigger interrupt
CISR won't run
DProgram will crash
💡 Hint
Refer to key_moments about why 'volatile' is needed for variables changed in ISR.
Concept Snapshot
Interrupt-driven button handling:
- Use attachInterrupt(pin, ISR, mode) to detect button presses.
- ISR runs immediately on event, sets a flag.
- Use 'volatile' for variables shared with ISR.
- Main loop checks flag and handles event.
- Reset flag after handling to avoid repeated triggers.
Full Transcript
This example shows how Arduino uses interrupts to handle button presses efficiently. The program sets up an interrupt on pin 2 to detect when the button is pressed (falling edge). When pressed, the interrupt triggers an Interrupt Service Routine (ISR) that sets a flag variable 'buttonPressed' to true immediately. The main loop then checks this flag to perform actions and resets it after handling. The 'volatile' keyword is important to ensure the variable updates correctly between ISR and main code. This method allows quick response to button presses without constantly checking the button state in the main loop.