delay() function behavior in Arduino - Time & Space Complexity
Start learning this pattern below
Jump into concepts and practice - no test required
We want to understand how the delay() function affects the time a program takes to run on an Arduino.
Specifically, how does the program's waiting time grow when we change the delay duration?
Analyze the time complexity of the following code snippet.
void loop() {
digitalWrite(LED_BUILTIN, HIGH); // turn LED on
delay(1000); // wait for 1 second
digitalWrite(LED_BUILTIN, LOW); // turn LED off
delay(1000); // wait for 1 second
}
This code turns an LED on and off with a 1-second pause between each change.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The delay() function pauses the program for a set time.
- How many times: delay() is called twice every loop cycle, which repeats forever.
Explain the growth pattern intuitively.
| Input Size (delay ms) | Approx. Waiting Time per Loop (ms) |
|---|---|
| 1000 | 2000 |
| 2000 | 4000 |
| 5000 | 10000 |
Pattern observation: The total waiting time grows directly with the delay value; doubling delay doubles wait time.
Time Complexity: O(n)
This means the program's waiting time grows linearly with the delay duration you set.
[X] Wrong: "delay() runs instantly and does not affect program speed."
[OK] Correct: delay() actually pauses the whole program, so longer delays make the program take longer to run.
Understanding how delay() affects program timing helps you write responsive Arduino code and shows you can think about how time grows with input.
"What if we replaced delay() with a non-blocking timer? How would the time complexity change?"
Practice
delay(1000); function do in an Arduino program?Solution
Step 1: Understand the delay() parameter
The number inside delay() is the time in milliseconds to pause the program.Step 2: Interpret delay(1000)
1000 milliseconds equals 1 second, so the program pauses for 1 second.Final Answer:
Pauses the program for 1000 milliseconds (1 second) -> Option AQuick Check:
delay(1000) = 1 second pause [OK]
- Thinking delay stops the program forever
- Confusing milliseconds with seconds
- Assuming delay speeds up the program
Solution
Step 1: Check correct function usage
delay() takes an integer number of milliseconds inside parentheses.Step 2: Validate each option
A uses delay(0.5); which is a float and incorrect. B assigns delay which is invalid. C uses delay(500); which is correct for 500 milliseconds. D uses '500s' which is invalid syntax.Final Answer:
delay(500); -> Option DQuick Check:
delay(500) is correct syntax [OK]
- Using decimal numbers instead of integers
- Assigning delay like a variable
- Adding units like 's' inside delay()
void setup() {
Serial.begin(9600);
Serial.println("Start");
delay(2000);
Serial.println("End");
}
void loop() {}Solution
Step 1: Analyze Serial prints and delay
"Start" prints immediately, then delay(2000) pauses 2 seconds before next line.Step 2: Understand delay effect on output
After 2 seconds pause, "End" prints. Both lines appear but with 2 seconds gap.Final Answer:
Start immediately, then End after 2 seconds -> Option CQuick Check:
delay pauses program, output delayed [OK]
- Thinking delay stops Serial output completely
- Assuming both prints happen instantly
- Believing delay affects only loop(), not setup()
void setup() {
pinMode(13, OUTPUT);
}
void loop() {
digitalWrite(13, HIGH);
delay(1000)
digitalWrite(13, LOW);
delay(1000);
}Solution
Step 1: Check syntax line by line
delay(1000) is missing a semicolon at the end, causing a syntax error.Step 2: Validate other statements
pinMode is correctly in setup(), digitalWrite and delay usage is correct except missing semicolon.Final Answer:
Missing semicolon after delay(1000) -> Option BQuick Check:
Every statement must end with ; [OK]
- Putting pinMode inside loop unnecessarily
- Thinking delay() can't be in loop
- Forgetting semicolons after statements
delay() to do this?Solution
Step 1: Understand blinking requirements
LED must turn ON and OFF 3 times, each ON and OFF lasting 0.5 seconds (500 ms).Step 2: Analyze each option
A uses a for loop 3 times with 500ms delay ON and OFF, matching requirements. B loops forever, no stop. C delays 1500ms which is too long and only blinks once. D loops 4 times (i<=3) with 1000ms delays, wrong timing and count.Final Answer:
for(int i=0; i<3; i++) { digitalWrite(9, HIGH); delay(500); digitalWrite(9, LOW); delay(500); } -> Option AQuick Check:
Loop 3 times with 500ms delay ON/OFF [OK]
- Using infinite loops instead of fixed count
- Wrong delay times for ON/OFF
- Looping one extra time with <= instead of <
