0
0
Arduinoprogramming~20 mins

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

Choose your learning style9 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.