0
0
Arduinoprogramming~10 mins

ISR best practices in Arduino - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to attach an interrupt service routine (ISR) to pin 2 on a rising edge.

Arduino
attachInterrupt(digitalPinToInterrupt(2), [1], RISING);
Drag options to blanks, or click blank then click option'
AinterruptHandler
BonInterrupt
CISR
DhandleInterrupt
Attempts:
3 left
💡 Hint
Common Mistakes
Using a function name that is not defined.
Passing the ISR function with parentheses (e.g., interruptHandler()).
2fill in blank
medium

Complete the ISR function to set a flag variable buttonPressed to true.

Arduino
volatile bool buttonPressed = false;

void [1]() {
  buttonPressed = true;
}
Drag options to blanks, or click blank then click option'
AISR
BhandleButton
CbuttonISR
DinterruptHandler
Attempts:
3 left
💡 Hint
Common Mistakes
Not declaring the flag variable as volatile.
Using a function name different from the one attached to the interrupt.
3fill in blank
hard

Fix the error in the ISR by removing the disallowed function call inside it.

Arduino
void buttonISR() {
  [1];
}
Drag options to blanks, or click blank then click option'
ASerial.println("Pressed")
BbuttonPressed = true
Cdelay(100)
DdigitalWrite(LED_BUILTIN, HIGH)
Attempts:
3 left
💡 Hint
Common Mistakes
Calling Serial.println inside ISR causing unexpected behavior.
Using delay inside ISR which blocks execution.
4fill in blank
hard

Fill both blanks to safely check and reset the flag buttonPressed in the main loop.

Arduino
void loop() {
  if ([1]) {
    [2] = false;
    // Handle button press here
  }
}
Drag options to blanks, or click blank then click option'
AbuttonPressed
CbuttonPressed == true
DbuttonPressed == false
Attempts:
3 left
💡 Hint
Common Mistakes
Using assignment = instead of comparison == in the if condition.
Not resetting the flag after handling.
5fill in blank
hard

Fill all three blanks to declare a volatile flag, attach the ISR, and handle the flag in the loop.

Arduino
volatile bool [1] = false;

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

void loop() {
  if ([3]) {
    [1] = false;
    // Respond to interrupt
  }
}
Drag options to blanks, or click blank then click option'
AbuttonPressed
BbuttonISR
DinterruptHandler
Attempts:
3 left
💡 Hint
Common Mistakes
Using different names for the flag variable in declaration and loop.
Mismatching ISR function names between declaration and attachInterrupt.