0
0
Arduinoprogramming~20 mins

attachInterrupt() function 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 of this Arduino sketch using attachInterrupt()?

Consider the following Arduino code snippet. What will be printed on the Serial Monitor after pressing the button connected to pin 2?

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++;
}
AThe Serial Monitor prints random numbers due to interrupt noise.
BThe Serial Monitor prints increasing numbers every second starting from 0, incrementing each time the button is pressed.
CThe Serial Monitor prints 0 repeatedly and never changes.
DThe Arduino resets continuously causing no output.
Attempts:
2 left
💡 Hint

Think about what happens when the button is pressed and how the interrupt updates the variable.

🧠 Conceptual
intermediate
1:30remaining
Which statement about attachInterrupt() is true?

Choose the correct statement about the attachInterrupt() function in Arduino.

A<code>attachInterrupt()</code> allows you to specify a function to run when a pin changes state without polling in <code>loop()</code>.
B<code>attachInterrupt()</code> can only be used with pins 0 and 1 on all Arduino boards.
CThe interrupt service routine (ISR) can safely use <code>delay()</code> to wait inside the function.
DYou must call <code>detachInterrupt()</code> before using <code>attachInterrupt()</code> again on the same pin.
Attempts:
2 left
💡 Hint

Think about what interrupts do compared to normal code flow.

🔧 Debug
advanced
2:00remaining
Why does this attachInterrupt() code cause a compile error?

Identify the cause of the compile error in this Arduino code snippet:

Arduino
void setup() {
  attachInterrupt(2, blinkLED(), RISING);
}

void blinkLED() {
  digitalWrite(13, HIGH);
  delay(100);
  digitalWrite(13, LOW);
}
AThe function blinkLED() must return an int, not void.
BThe pin number 2 is invalid for attachInterrupt() on Arduino Uno.
CThe ISR blinkLED() uses delay(), which is not allowed inside interrupts.
DThe interrupt function blinkLED() is called immediately instead of passing its name as a pointer.
Attempts:
2 left
💡 Hint

Look carefully at how the function is passed to attachInterrupt().

📝 Syntax
advanced
1:30remaining
Which option correctly uses attachInterrupt() to trigger on a falling edge?

Choose the correct syntax to attach an interrupt on pin 3 that triggers on a falling edge and calls the function handleInterrupt.

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

Remember how to convert a pin number to an interrupt number and how to pass the function.

🚀 Application
expert
2:30remaining
How many times will the variable 'count' increment in this scenario?

Given this Arduino code, if a noisy button connected to pin 2 is pressed once causing multiple rapid FALLING edges, how many times will count increment after the press?

Arduino
volatile int count = 0;

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

void loop() {}

void incrementCount() {
  count++;
}
AMultiple times, because each FALLING edge triggers the interrupt, including noise.
BCount will increment randomly due to debounce hardware.
CZero times, because interrupts are disabled by default.
DExactly 1 time, because the interrupt triggers only once per press.
Attempts:
2 left
💡 Hint

Think about how mechanical button noise affects interrupts triggered on edges.