Bird
Raised Fist0
Arduinoprogramming~10 mins

ISR best practices in Arduino - Interactive Code Practice

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

Practice

(1/5)
1. What is the main reason to keep Interrupt Service Routines (ISRs) short and simple in Arduino?
easy
A. To use more memory
B. To avoid delaying other important tasks and interrupts
C. To make the code look cleaner
D. To allow ISRs to run slower

Solution

  1. Step 1: Understand ISR impact on program flow

    ISRs interrupt the main program to handle urgent events, so they must finish quickly to avoid blocking other tasks.
  2. Step 2: Consider consequences of long ISRs

    If ISRs are long, they delay other interrupts and slow down the main program, causing performance issues.
  3. Final Answer:

    To avoid delaying other important tasks and interrupts -> Option B
  4. Quick Check:

    Keep ISRs short = avoid delays [OK]
Hint: Short ISR means faster response and less delay [OK]
Common Mistakes:
  • Thinking ISR length only affects code readability
  • Assuming ISRs can run as long as needed
  • Ignoring impact on other interrupts
2. Which of the following is the correct way to declare a variable shared between an ISR and the main program?
easy
A. volatile int counter = 0;
B. int counter = 0;
C. static int counter = 0;
D. const int counter = 0;

Solution

  1. Step 1: Identify variable usage in ISR and main code

    Variables shared between ISR and main code must be declared volatile to prevent compiler optimizations that cause wrong values.
  2. Step 2: Check each declaration

    Only 'volatile int counter = 0;' tells the compiler the variable can change anytime, ensuring correct behavior.
  3. Final Answer:

    volatile int counter = 0; -> Option A
  4. Quick Check:

    Shared variable = volatile [OK]
Hint: Use volatile for variables changed in ISR [OK]
Common Mistakes:
  • Forgetting volatile keyword
  • Using const for variables that change
  • Using static without volatile
3. What will be the output of this Arduino code snippet?
volatile int count = 0;
void setup() {
  Serial.begin(9600);
  attachInterrupt(digitalPinToInterrupt(2), increment, RISING);
}
void loop() {
  Serial.println(count);
  count = 0;
  delay(1000);
}
void increment() {
  count++;
}
Assuming pin 2 receives 3 rising signals per second, what will Serial print each second?
medium
A. 1
B. 0
C. 3
D. Random values

Solution

  1. Step 1: Understand ISR increments

    Each rising signal on pin 2 triggers the ISR 'increment' which increases 'count' by 1.
  2. Step 2: Calculate increments per second

    3 rising signals per second means 'count' increases by 3 every second.
  3. Final Answer:

    3 -> Option C
  4. Quick Check:

    3 signals/sec = count increments 3 times [OK]
Hint: ISR increments count per event; count matches event rate [OK]
Common Mistakes:
  • Ignoring volatile keyword effect
  • Assuming count resets automatically
  • Thinking ISR runs slower than events
4. Identify the error in this ISR code snippet:
volatile int flag = 0;
void setup() {
  attachInterrupt(digitalPinToInterrupt(3), toggleFlag, FALLING);
}
void loop() {
  if(flag) {
    // do something
    flag = 0;
  }
}
void toggleFlag() {
  flag = !flag;
}
medium
A. Not declaring 'flag' as volatile
B. Missing Serial.begin() in setup
C. Incorrect interrupt mode FALLING
D. Using non-atomic operation on 'flag' inside ISR

Solution

  1. Step 1: Analyze ISR operation on 'flag'

    The ISR toggles 'flag' using '!flag', which is a read-modify-write operation and not atomic.
  2. Step 2: Understand atomicity importance

    Non-atomic operations on shared variables can cause race conditions and unpredictable behavior.
  3. Final Answer:

    Using non-atomic operation on 'flag' inside ISR -> Option D
  4. Quick Check:

    ISR must avoid non-atomic variable changes [OK]
Hint: Avoid read-modify-write on shared vars in ISR [OK]
Common Mistakes:
  • Ignoring atomicity of operations
  • Thinking volatile alone fixes all issues
  • Confusing interrupt mode with logic error
5. You want to count button presses using an ISR on pin 2. Which approach best follows ISR best practices in Arduino?
hard
A. Increment a volatile counter in ISR; read and reset it safely in loop
B. Print a message directly inside the ISR using Serial.println()
C. Use delay() inside ISR to debounce the button
D. Declare the counter as static inside ISR without volatile

Solution

  1. Step 1: Review ISR best practices for counting events

    ISRs should be short and only update volatile variables; printing or delays inside ISR are bad.
  2. Step 2: Evaluate each option

    Increment a volatile counter in ISR; read and reset it safely in loop uses a volatile counter incremented in ISR and safely handled in loop, which is correct.
  3. Final Answer:

    Increment a volatile counter in ISR; read and reset it safely in loop -> Option A
  4. Quick Check:

    Keep ISR short; update volatile vars only [OK]
Hint: Update volatile counter in ISR; handle logic outside [OK]
Common Mistakes:
  • Using Serial.print inside ISR causing delays
  • Using delay() inside ISR causing blocking
  • Not declaring shared variables volatile
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