Bird
Raised Fist0
Arduinoprogramming~20 mins

Why timing control is needed in Arduino - Challenge Your Understanding

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Challenge - 5 Problems
🎖️
Timing Control Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
1:30remaining
Why do we need timing control in Arduino programs?

Imagine you want to blink an LED on and off every second using Arduino. Why is timing control important in this case?

ATo increase the speed of the LED blinking beyond hardware limits.
BBecause Arduino cannot run any code without timing control.
CTo make sure the LED turns on and off at the right intervals without blocking other tasks.
DTo avoid using any delay functions in the program.
Attempts:
2 left
💡 Hint

Think about what happens if the program waits too long in one place.

Predict Output
intermediate
1:30remaining
What is the output of this Arduino code snippet?

Consider this Arduino code that blinks an LED connected to pin 13:

void setup() {
  pinMode(13, OUTPUT);
}

void loop() {
  digitalWrite(13, HIGH);
  delay(1000);
  digitalWrite(13, LOW);
  delay(1000);
}

What does this code do?

Arduino
void setup() {
  pinMode(13, OUTPUT);
}

void loop() {
  digitalWrite(13, HIGH);
  delay(1000);
  digitalWrite(13, LOW);
  delay(1000);
}
AThe LED on pin 13 turns on for 1 second, then off for 1 second, repeatedly.
BThe LED stays on permanently without blinking.
CThe LED blinks very fast without any delay.
DThe LED never turns on.
Attempts:
2 left
💡 Hint

Look at the delay times and the digitalWrite commands.

🔧 Debug
advanced
2:00remaining
Why does this Arduino code freeze and how to fix it?

This code is supposed to blink an LED and read a sensor value continuously, but it freezes after a while:

void setup() {
  pinMode(13, OUTPUT);
  Serial.begin(9600);
}

void loop() {
  digitalWrite(13, HIGH);
  delay(5000);
  digitalWrite(13, LOW);
  delay(5000);
  int sensorValue = analogRead(A0);
  Serial.println(sensorValue);
}

Why does it freeze and what is the main timing control issue?

Arduino
void setup() {
  pinMode(13, OUTPUT);
  Serial.begin(9600);
}

void loop() {
  digitalWrite(13, HIGH);
  delay(5000);
  digitalWrite(13, LOW);
  delay(5000);
  int sensorValue = analogRead(A0);
  Serial.println(sensorValue);
}
AThe analogRead function causes a memory leak leading to freeze.
BThe long delay blocks sensor reading and serial printing, causing the program to appear frozen.
CThe LED pin is not set as OUTPUT, so the program crashes.
DSerial.begin is called inside loop, causing repeated initialization and freeze.
Attempts:
2 left
💡 Hint

Think about what happens during delay and how it affects other code.

📝 Syntax
advanced
2:00remaining
Which Arduino code snippet correctly implements non-blocking timing to blink an LED?

Choose the code that blinks an LED every 1 second without using delay(), allowing other code to run smoothly.

A
unsigned long previousMillis = 0;
const long interval = 1000;

void loop() {
  unsigned long currentMillis = millis();
  if (currentMillis - previousMillis >= interval) {
    previousMillis = currentMillis;
    digitalWrite(13, !digitalRead(13));
  }
}
B
void loop() {
  delay(1000);
  digitalWrite(13, HIGH);
  delay(1000);
  digitalWrite(13, LOW);
}
C
void loop() {
  if (millis() % 1000 == 0) {
    digitalWrite(13, HIGH);
  } else {
    digitalWrite(13, LOW);
  }
}
D
void loop() {
  static int state = 0;
  if (state == 0) {
    digitalWrite(13, HIGH);
    state = 1;
  } else {
    digitalWrite(13, LOW);
    state = 0;
  }
  delay(1000);
}
Attempts:
2 left
💡 Hint

Look for code that uses millis() and does not call delay().

🚀 Application
expert
1:30remaining
How many times does the LED blink in 10 seconds with this code?

Given this Arduino code:

void setup() {
  pinMode(13, OUTPUT);
}

void loop() {
  digitalWrite(13, HIGH);
  delay(2000);
  digitalWrite(13, LOW);
  delay(3000);
}

How many complete ON-OFF blink cycles happen in 10 seconds?

Arduino
void setup() {
  pinMode(13, OUTPUT);
}

void loop() {
  digitalWrite(13, HIGH);
  delay(2000);
  digitalWrite(13, LOW);
  delay(3000);
}
A5
B3
C4
D2
Attempts:
2 left
💡 Hint

Add the ON and OFF delay times to find one cycle duration, then divide 10 seconds by that.

Practice

(1/5)
1. Why do we need timing control in Arduino programs?
easy
A. To make sure actions happen at the right time
B. To increase the speed of the Arduino processor
C. To change the color of the Arduino board
D. To connect the Arduino to the internet

Solution

  1. Step 1: Understand the purpose of timing control

    Timing control allows the Arduino to perform tasks at specific times or intervals.
  2. Step 2: Identify the correct reason for timing control

    It helps in making sure actions like blinking LEDs or reading sensors happen when needed.
  3. Final Answer:

    To make sure actions happen at the right time -> Option A
  4. Quick Check:

    Timing control = right time actions [OK]
Hint: Timing control means doing things at the right moment [OK]
Common Mistakes:
  • Thinking timing control speeds up the processor
  • Confusing timing control with internet connection
  • Believing timing control changes hardware color
2. Which Arduino function is used to pause the program for a specific time?
easy
A. digitalWrite()
B. analogRead()
C. pinMode()
D. delay()

Solution

  1. Step 1: Recall Arduino functions for timing

    The delay() function pauses the program for a set number of milliseconds.
  2. Step 2: Match function to description

    delay() is the only function among options that pauses execution.
  3. Final Answer:

    delay() -> Option D
  4. Quick Check:

    Pause program = delay() [OK]
Hint: delay() pauses program; others control pins or read values [OK]
Common Mistakes:
  • Using digitalWrite() to pause program
  • Confusing pinMode() with timing control
  • Thinking analogRead() pauses execution
3. What will the following Arduino code do?
void setup() {
  pinMode(13, OUTPUT);
}

void loop() {
  digitalWrite(13, HIGH);
  delay(1000);
  digitalWrite(13, LOW);
  delay(1000);
}
medium
A. Turn LED on pin 13 on and off every second
B. Keep LED on pin 13 always on
C. Keep LED on pin 13 always off
D. Blink LED on pin 13 every 100 milliseconds

Solution

  1. Step 1: Analyze the loop code

    The code turns pin 13 HIGH (LED on), waits 1000 ms (1 second), then LOW (LED off), waits 1000 ms again.
  2. Step 2: Understand the effect on LED

    This causes the LED to blink on and off every second.
  3. Final Answer:

    Turn LED on pin 13 on and off every second -> Option A
  4. Quick Check:

    delay(1000) = 1 second blink [OK]
Hint: delay(1000) means 1 second pause, blinking LED [OK]
Common Mistakes:
  • Thinking delay(1000) is 100 milliseconds
  • Assuming LED stays always on or off
  • Ignoring the delay between on and off
4. Identify the problem in this Arduino code for blinking an LED:
void setup() {
  pinMode(13, OUTPUT);
}

void loop() {
  digitalWrite(13, HIGH);
  delay(1000);
  digitalWrite(13, HIGH);
  delay(1000);
}
medium
A. delay() function is used incorrectly
B. LED never turns off because digitalWrite(13, LOW) is missing
C. pinMode() should be in loop()
D. digitalWrite() should use pin 12 instead of 13

Solution

  1. Step 1: Check LED on/off commands

    The code sets pin 13 HIGH twice but never sets it LOW, so LED stays on.
  2. Step 2: Identify missing part for blinking

    To blink, the LED must be turned off with digitalWrite(13, LOW) between delays.
  3. Final Answer:

    LED never turns off because digitalWrite(13, LOW) is missing -> Option B
  4. Quick Check:

    Missing LOW command = LED stays on [OK]
Hint: Blink needs both HIGH and LOW commands [OK]
Common Mistakes:
  • Thinking delay() is wrong here
  • Moving pinMode() inside loop() unnecessarily
  • Changing pin number without reason
5. You want to read a sensor every 500 milliseconds without stopping other tasks. Which timing method should you use?
hard
A. Use delay(500) inside loop()
B. Use digitalWrite() to pause sensor reading
C. Use millis() to check elapsed time and read sensor when 500 ms passed
D. Use pinMode() to set sensor pin to INPUT every 500 ms

Solution

  1. Step 1: Understand delay() effect

    delay(500) pauses the whole program, stopping other tasks temporarily.
  2. Step 2: Use millis() for non-blocking timing

    millis() lets you check time passed without stopping the program, so other tasks run smoothly.
  3. Final Answer:

    Use millis() to check elapsed time and read sensor when 500 ms passed -> Option C
  4. Quick Check:

    Non-blocking timing = millis() [OK]
Hint: millis() checks time without stopping program [OK]
Common Mistakes:
  • Using delay() and freezing program
  • Confusing digitalWrite() with timing control
  • Resetting pinMode() repeatedly