Imagine you want to blink an LED on and off every second using Arduino. Why is timing control important in this case?
Think about what happens if the program waits too long in one place.
Timing control helps the Arduino manage when to turn the LED on or off without stopping the whole program. This allows other parts of the program to run smoothly.
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?
void setup() {
pinMode(13, OUTPUT);
}
void loop() {
digitalWrite(13, HIGH);
delay(1000);
digitalWrite(13, LOW);
delay(1000);
}Look at the delay times and the digitalWrite commands.
The code turns the LED on, waits 1 second, turns it off, waits 1 second, then repeats. This creates a blinking effect.
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?
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);
}Think about what happens during delay and how it affects other code.
Using long delay stops the program from doing anything else during that time, so sensor reading and printing happen only after delays, making the program unresponsive.
Choose the code that blinks an LED every 1 second without using delay(), allowing other code to run smoothly.
Look for code that uses millis() and does not call delay().
Option A uses millis() to check elapsed time and toggles the LED without blocking other code. Others use delay or incorrect logic.
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?
void setup() {
pinMode(13, OUTPUT);
}
void loop() {
digitalWrite(13, HIGH);
delay(2000);
digitalWrite(13, LOW);
delay(3000);
}Add the ON and OFF delay times to find one cycle duration, then divide 10 seconds by that.
Each blink cycle takes 2s ON + 3s OFF = 5 seconds. In 10 seconds, 10 / 5 = 2 full cycles. But the loop starts immediately, so the LED turns ON at 0s, OFF at 2s, ON at 5s, OFF at 7s, ON at 10s (start of third ON). So 2 full ON-OFF cycles complete, and the third ON starts but does not complete within 10s. So 2 complete blinks.
