Bird
Raised Fist0
Arduinoprogramming~20 mins

Interrupt-driven button handling in Arduino - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Interrupt Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
What is the output when the button is pressed?

Consider this Arduino code that uses an interrupt to detect a button press. What will be printed to the serial monitor when the button is pressed?

Arduino
volatile bool buttonPressed = false;

void setup() {
  Serial.begin(9600);
  pinMode(2, INPUT_PULLUP);
  attachInterrupt(digitalPinToInterrupt(2), buttonISR, FALLING);
}

void loop() {
  if (buttonPressed) {
    Serial.println("Button pressed!");
    buttonPressed = false;
  }
}

void buttonISR() {
  buttonPressed = true;
}
AButton pressed! (printed continuously without stopping)
BButton pressed! (printed once per press)
CNo output because interrupts are not enabled
DCompilation error due to ISR function
Attempts:
2 left
💡 Hint

Think about what happens inside the interrupt and how the main loop reacts.

🧠 Conceptual
intermediate
1:30remaining
Why use volatile keyword for variables shared with ISR?

In interrupt-driven button handling, why is the volatile keyword used for variables shared between the ISR and the main code?

ATo make the variable constant and unchangeable
BTo allow the variable to be accessed only inside the ISR
CTo tell the compiler the variable can change unexpectedly and prevent optimization errors
DTo allocate the variable in EEPROM memory
Attempts:
2 left
💡 Hint

Think about how the compiler might optimize code if it assumes a variable never changes.

🔧 Debug
advanced
2:30remaining
Why does this interrupt code cause a crash?

Examine this Arduino code snippet. Why does it cause the program to crash or behave unpredictably?

Arduino
volatile int count = 0;

void setup() {
  pinMode(2, INPUT_PULLUP);
  attachInterrupt(digitalPinToInterrupt(2), incrementCount, FALLING);
  Serial.begin(9600);
}

void loop() {
  Serial.println(count);
  delay(1000);
}

void incrementCount() {
  count++;
  delay(10);
}
ACalling delay() inside an ISR causes the crash
BThe variable count is not declared volatile
CattachInterrupt is called with wrong pin number
DSerial.println() cannot be used in loop
Attempts:
2 left
💡 Hint

Think about what functions are safe to call inside an ISR.

📝 Syntax
advanced
1:30remaining
Which option correctly attaches an interrupt to pin 3 on FALLING edge?

Choose the correct Arduino code line to attach an interrupt to pin 3 that triggers on the FALLING edge.

AattachInterrupt(digitalPinToInterrupt(3), isrFunction, FALLING);
BattachInterrupt(3, isrFunction, FALLING);
CattachInterrupt(3, isrFunction, FALLING_EDGE);
DattachInterrupt(digitalPinToInterrupt(3), isrFunction, FALL);
Attempts:
2 left
💡 Hint

Remember the correct way to convert a pin number to an interrupt number.

🚀 Application
expert
3:00remaining
How many times will the LED toggle after 5 button presses?

This Arduino code toggles an LED each time a button connected to pin 2 is pressed using an interrupt. How many times will the LED state change after 5 distinct button presses?

Arduino
volatile int pressCount = 0;

void setup() {
  pinMode(2, INPUT_PULLUP);
  pinMode(13, OUTPUT);
  attachInterrupt(digitalPinToInterrupt(2), countPress, FALLING);
}

void loop() {
  static int lastCount = 0;
  if (pressCount != lastCount) {
    digitalWrite(13, !digitalRead(13));
    lastCount = pressCount;
  }
}

void countPress() {
  pressCount++;
}
A0 times
B10 times
C1 time
D5 times
Attempts:
2 left
💡 Hint

Each button press increments the count and triggers a toggle in the main loop.

Practice

(1/5)
1. What is the main advantage of using interrupts for button handling on an Arduino?
easy
A. The Arduino can react immediately when the button is pressed without waiting.
B. It makes the button press slower but more reliable.
C. It allows the button to be pressed multiple times at once.
D. It disables other parts of the program while the button is pressed.

Solution

  1. Step 1: Understand what interrupts do

    Interrupts pause the main program to run a special function immediately when an event happens, like a button press.
  2. Step 2: Compare interrupt behavior to normal checking

    Without interrupts, the program must keep checking the button, which can delay response. Interrupts react instantly.
  3. Final Answer:

    The Arduino can react immediately when the button is pressed without waiting. -> Option A
  4. Quick Check:

    Interrupts = Immediate reaction [OK]
Hint: Interrupts react instantly, no waiting needed [OK]
Common Mistakes:
  • Thinking interrupts slow down button response
  • Believing interrupts disable other code permanently
  • Confusing interrupts with polling multiple presses
2. Which of the following is the correct syntax to attach an interrupt to pin 2 that calls the function buttonPressed on a FALLING edge?
easy
A. attachInterrupt(digitalPinToInterrupt(2), buttonPressed, FALLING);
B. attachInterrupt(2, buttonPressed, RISING);
C. attachInterrupt(buttonPressed, 2, FALLING);
D. attachInterrupt(2, FALLING, buttonPressed);

Solution

  1. Step 1: Recall attachInterrupt syntax

    The correct syntax is attachInterrupt(digitalPinToInterrupt(pin), ISR_function, mode).
  2. Step 2: Match parameters to the question

    Pin is 2, function is buttonPressed, mode is FALLING edge.
  3. Final Answer:

    attachInterrupt(digitalPinToInterrupt(2), buttonPressed, FALLING); -> Option A
  4. Quick Check:

    attachInterrupt(pin, function, mode) = attachInterrupt(digitalPinToInterrupt(2), buttonPressed, FALLING); [OK]
Hint: Use digitalPinToInterrupt(pin) for correct pin number [OK]
Common Mistakes:
  • Swapping function and pin arguments
  • Using pin number directly without digitalPinToInterrupt
  • Mixing up trigger modes like RISING and FALLING
3. What will be the output on the serial monitor after pressing the button once in this code?
volatile bool pressed = false;

void setup() {
  Serial.begin(9600);
  pinMode(2, INPUT_PULLUP);
  attachInterrupt(digitalPinToInterrupt(2), buttonISR, FALLING);
}

void loop() {
  if (pressed) {
    Serial.println("Button pressed!");
    pressed = false;
  }
}

void buttonISR() {
  pressed = true;
}
medium
A. Compilation error due to volatile variable
B. Button pressed! (printed repeatedly forever)
C. No output at all
D. Button pressed! (printed once)

Solution

  1. Step 1: Understand ISR and flag usage

    The ISR sets the volatile flag 'pressed' to true when the button is pressed.
  2. Step 2: Check loop behavior

    The loop prints "Button pressed!" once when 'pressed' is true, then resets it to false, so it prints only once per press.
  3. Final Answer:

    Button pressed! (printed once) -> Option D
  4. Quick Check:

    ISR sets flag, loop prints once [OK]
Hint: ISR sets flag; loop prints once per press [OK]
Common Mistakes:
  • Thinking the message prints repeatedly without reset
  • Assuming volatile causes errors
  • Ignoring that flag resets after printing
4. Identify the error in this interrupt-driven button code:
volatile int count = 0;

void setup() {
  Serial.begin(9600);
  pinMode(3, INPUT_PULLUP);
  attachInterrupt(digitalPinToInterrupt(3), countPresses, FALLING);
}

void loop() {
  Serial.println(count);
  delay(1000);
}

void countPresses() {
  count++;
}
medium
A. Using delay() in loop causes missed interrupts
B. Modifying a multi-byte variable without disabling interrupts
C. Incrementing a non-volatile variable in ISR
D. attachInterrupt() called with wrong pin number

Solution

  1. Step 1: Check variable type and ISR safety

    'count' is volatile int (multi-byte). Incrementing it in ISR and reading in loop can cause inconsistent values.
  2. Step 2: Understand atomicity issue

    Incrementing multi-byte variables is not atomic; main code reading 'count' may see partial updates causing errors.
  3. Final Answer:

    Modifying a multi-byte variable without disabling interrupts -> Option B
  4. Quick Check:

    Multi-byte variable access must be atomic [OK]
Hint: Use atomic operations or disable interrupts for multi-byte vars [OK]
Common Mistakes:
  • Ignoring atomicity of multi-byte variables
  • Thinking delay() blocks interrupts
  • Assuming attachInterrupt pin is wrong without checking
5. You want to count how many times a button connected to pin 4 is pressed, but only if the button is pressed and held for at least 2 seconds. Which approach best uses interrupts to achieve this?
hard
A. Poll the button state in loop without interrupts and count presses.
B. Use attachInterrupt on RISING edge to increment count immediately on press.
C. Use attachInterrupt on FALLING edge to start a timer, then check duration in loop before incrementing count.
D. Use attachInterrupt on CHANGE edge and increment count inside ISR.

Solution

  1. Step 1: Understand requirement for press duration

    Counting only if pressed for 2 seconds means timing the press length, not just detecting press.
  2. Step 2: Use interrupt to detect press start and timer in loop

    Interrupt on FALLING edge detects press start; loop checks if button held 2 seconds before counting.
  3. Final Answer:

    Use attachInterrupt on FALLING edge to start a timer, then check duration in loop before incrementing count. -> Option C
  4. Quick Check:

    Interrupt detects press start; loop checks hold time [OK]
Hint: Interrupt starts timer; loop checks hold duration [OK]
Common Mistakes:
  • Incrementing count immediately without timing
  • Using CHANGE edge causing multiple triggers
  • Polling without interrupts misses instant detection
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard