0
0
Arduinoprogramming~20 mins

ISR best practices in Arduino - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
ISR Mastery in Arduino
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 ISR example?

Consider the following Arduino code with an ISR that increments a counter. What will be printed to the serial monitor?

Arduino
volatile int counter = 0;

void setup() {
  Serial.begin(9600);
  attachInterrupt(digitalPinToInterrupt(2), incrementCounter, RISING);
}

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

void incrementCounter() {
  counter++;
}
AThe counter value increases rapidly and prints large numbers quickly.
BThe counter value remains 0 because ISRs cannot modify variables.
CThe counter value increases approximately once per second.
DThe program will not compile due to ISR syntax errors.
Attempts:
2 left
💡 Hint

Think about how often the interrupt triggers and how the counter variable is updated.

🧠 Conceptual
intermediate
1:30remaining
Why should variables shared between ISR and main code be declared volatile?

In Arduino programming, why is it important to declare variables shared between an ISR and the main program as volatile?

ATo automatically synchronize the variable between multiple Arduino boards.
BTo allow the variable to be stored in EEPROM memory for persistence after reset.
CTo make the variable accessible only inside the ISR and not in the main program.
DTo prevent the compiler from optimizing out accesses to the variable, ensuring the main code always reads the latest value updated by the ISR.
Attempts:
2 left
💡 Hint

Think about how the compiler might optimize variables that seem unchanged in the main code.

🔧 Debug
advanced
2:00remaining
Identify the problem with this ISR code snippet

Examine the following ISR code. What issue might cause unexpected behavior?

Arduino
volatile int count = 0;

void setup() {
  attachInterrupt(digitalPinToInterrupt(3), countUp, FALLING);
}

void loop() {
  // main code
}

void countUp() {
  count += 2;
  delay(10);
}
AThe variable <code>count</code> is not declared volatile, so updates may be missed.
BUsing <code>delay()</code> inside an ISR causes the program to hang or behave unpredictably.
CThe interrupt is attached to the wrong pin number, causing no interrupts to trigger.
DThe ISR function must return a value, but <code>countUp()</code> returns void.
Attempts:
2 left
💡 Hint

Consider what functions are safe to use inside an ISR.

📝 Syntax
advanced
1:30remaining
Which ISR declaration is correct in Arduino?

Which of the following ISR function declarations is syntactically correct for Arduino?

Avoid ISR_handler() { /* code */ }
Bint ISR_handler() { /* code */ return 0; }
Cvoid ISR_handler(void) { /* code */ }
Dvoid ISR_handler() interrupt { /* code */ }
Attempts:
2 left
💡 Hint

Think about the standard function declaration syntax in Arduino C++.

🚀 Application
expert
2:30remaining
How to safely update a multi-byte variable in an ISR?

You have a 16-bit volatile variable volatile uint16_t sensorValue; updated inside an ISR and read in the main loop. How should you safely read sensorValue in the main loop to avoid corrupted data?

ADisable interrupts before reading <code>sensorValue</code>, then re-enable them after reading.
BRead <code>sensorValue</code> directly without disabling interrupts because it is volatile.
CUse <code>noInterrupts()</code> inside the ISR before updating <code>sensorValue</code>.
DDeclare <code>sensorValue</code> as <code>volatile uint8_t</code> to avoid multi-byte issues.
Attempts:
2 left
💡 Hint

Think about atomicity when reading multi-byte variables shared with ISRs.