0
0
Arduinoprogramming~20 mins

Why interrupts improve responsiveness in Arduino - Challenge Your Understanding

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 using interrupts?

Consider this Arduino code snippet using an interrupt to count button presses. What will be printed to the serial monitor after pressing the button 3 times?

Arduino
volatile int count = 0;

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

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

void incrementCount() {
  count++;
}
A
3
3
3
3
...
B
0
1
2
3
...
C
0
0
0
0
...
D
1
2
3
4
...
Attempts:
2 left
💡 Hint

Interrupts update the count immediately when the button is pressed, so the main loop sees the updated value.

🧠 Conceptual
intermediate
1:30remaining
Why do interrupts improve responsiveness?

Why do interrupts help an Arduino respond faster to events compared to checking inputs inside the loop()?

ABecause interrupts let the Arduino ignore inputs until the loop finishes.
BBecause interrupts make the Arduino run code slower to avoid errors.
CBecause interrupts allow the Arduino to skip setup and start loop faster.
DBecause interrupts pause the main code and run immediately when an event happens.
Attempts:
2 left
💡 Hint

Think about how the Arduino reacts when an event happens outside the normal loop.

🔧 Debug
advanced
2:30remaining
Why does this interrupt code fail to count correctly?

Look at this Arduino code. The count variable does not increase as expected when the button is pressed. What is the problem?

Arduino
int count = 0;

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

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

void incrementCount() {
  count++;
}
ASerial.begin must be called inside the interrupt function.
Bcount should be declared volatile to be safely used in interrupt.
CattachInterrupt is called with wrong pin number.
Ddelay(1000) inside loop causes the interrupt to be missed.
Attempts:
2 left
💡 Hint

Variables shared between interrupts and main code need special declaration.

📝 Syntax
advanced
1:30remaining
Which code correctly attaches an interrupt on pin 3?

Choose the correct Arduino code to attach an interrupt on pin 3 that triggers on a rising signal.

AattachInterrupt(3, handleInterrupt, RISING);
BattachInterrupt(handleInterrupt, 3, RISING);
CattachInterrupt(digitalPinToInterrupt(3), handleInterrupt, RISING);
DattachInterrupt(RISING, digitalPinToInterrupt(3), handleInterrupt);
Attempts:
2 left
💡 Hint

Remember the correct order of parameters for attachInterrupt.

🚀 Application
expert
3:00remaining
How many times will the LED toggle in 5 seconds?

This Arduino code toggles an LED each time a button connected to pin 2 is pressed. The main loop runs a 5-second timer. How many times will the LED toggle if the button is pressed 4 times during those 5 seconds?

Arduino
volatile int toggleCount = 0;
const int ledPin = 13;

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

void loop() {
  unsigned long start = millis();
  while (millis() - start < 5000) {
    // wait 5 seconds
  }
  Serial.println(toggleCount);
}

void toggleLED() {
  digitalWrite(ledPin, !digitalRead(ledPin));
  toggleCount++;
}
A4
B5
C0
D1
Attempts:
2 left
💡 Hint

Each button press triggers the interrupt once, toggling the LED and increasing the count.