0
0
Arduinoprogramming~20 mins

Timer interrupts with TimerOne library in Arduino - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
TimerOne 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 TimerOne interrupt example?

Consider this Arduino sketch using the TimerOne library to toggle an LED every 1 second. What will be printed on the Serial Monitor?

Arduino
volatile bool ledState = false;

void setup() {
  Serial.begin(9600);
  pinMode(13, OUTPUT);
  Timer1.initialize(1000000); // 1 second
  Timer1.attachInterrupt(toggleLED);
}

void loop() {
  // main loop does nothing
}

void toggleLED() {
  ledState = !ledState;
  digitalWrite(13, ledState);
  Serial.println(ledState ? "LED ON" : "LED OFF");
}
AOnly LED ON is printed repeatedly every second
BNo output is printed because Serial.println cannot be used in interrupts
COnly LED OFF is printed repeatedly every second
DLED ON and LED OFF alternate every second on the Serial Monitor
Attempts:
2 left
💡 Hint

Think about what the toggleLED function does each time the interrupt fires.

🧠 Conceptual
intermediate
1:30remaining
Which statement about TimerOne interrupts is true?

Choose the correct statement about using TimerOne interrupts in Arduino sketches.

ATimerOne interrupts can be used to execute code at precise intervals without blocking the main loop
BTimerOne interrupts automatically disable all other interrupts when running
CTimerOne interrupts require the use of delay() inside the interrupt function
DTimerOne interrupts can only be used with Arduino Uno and not other boards
Attempts:
2 left
💡 Hint

Think about how interrupts help with timing and multitasking.

🔧 Debug
advanced
2:30remaining
Why does this TimerOne interrupt code cause the LED to flicker irregularly?

Examine the code below. The LED connected to pin 13 flickers irregularly instead of toggling every 0.5 seconds. What is the cause?

Arduino
volatile int ledState = LOW;

void setup() {
  pinMode(13, OUTPUT);
  Timer1.initialize(500000); // 0.5 seconds
  Timer1.attachInterrupt(toggleLED);
}

void loop() {
  // empty
}

void toggleLED() {
  ledState = !ledState;
  digitalWrite(13, ledState);
}
AdigitalWrite cannot be called inside an interrupt function
BTimer1.initialize is set to 500000 microseconds which is too fast for the LED
CledState is declared as int and toggled with ! operator, causing unexpected values
DpinMode(13, OUTPUT) must be called inside the interrupt function
Attempts:
2 left
💡 Hint

Consider the data type and how the ! operator works on integers.

📝 Syntax
advanced
1:30remaining
Which option fixes the syntax error in this TimerOne setup?

Identify the option that corrects the syntax error in the TimerOne initialization code below:

Timer1.initialize(1000000)
Timer1.attachInterrupt(toggleLED);
AAdd a semicolon after Timer1.initialize(1000000);
BChange Timer1.attachInterrupt(toggleLED) to Timer1.attachInterrupt(toggleLED);
CRemove the parentheses from Timer1.initialize
DReplace Timer1.initialize(1000000) with Timer1.init(1000000);
Attempts:
2 left
💡 Hint

Check the end of each statement for proper punctuation.

🚀 Application
expert
2:00remaining
How many times will the LED toggle in 10 seconds with this TimerOne code?

Given this code snippet, how many times will the LED connected to pin 13 toggle in 10 seconds?

volatile bool ledState = false;

void setup() {
  pinMode(13, OUTPUT);
  Timer1.initialize(2500000); // 2.5 seconds
  Timer1.attachInterrupt(toggleLED);
}

void loop() {}

void toggleLED() {
  ledState = !ledState;
  digitalWrite(13, ledState);
}
A5 times
B4 times
C10 times
D20 times
Attempts:
2 left
💡 Hint

Calculate how many 2.5 second intervals fit into 10 seconds.