Arduino IDE and sketch structure - Time & Space Complexity
Start learning this pattern below
Jump into concepts and practice - no test required
When we write Arduino sketches, it's helpful to know how the program's running time changes as we add more code or inputs.
We want to see how the structure of the sketch affects how long it takes to run.
Analyze the time complexity of the following Arduino sketch structure.
void setup() {
// runs once
pinMode(LED_BUILTIN, OUTPUT);
}
void loop() {
digitalWrite(LED_BUILTIN, HIGH);
delay(1000);
digitalWrite(LED_BUILTIN, LOW);
delay(1000);
}
This sketch turns the built-in LED on and off every second repeatedly.
Look for parts that repeat or run many times.
- Primary operation: The
loop()function runs over and over. - How many times: It repeats endlessly, cycling through turning LED on and off with delays.
Since the sketch runs forever, the time spent in each loop cycle stays the same no matter what.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 loops | About 10 cycles of turning LED on and off |
| 100 loops | About 100 cycles, each same length |
| 1000 loops | About 1000 cycles, still same time per cycle |
Pattern observation: The time per loop does not grow with input size; it stays constant.
Time Complexity: O(1)
This means each loop cycle takes the same amount of time regardless of how many times it runs.
[X] Wrong: "The loop() function takes longer as it runs more times because it does more work."
[OK] Correct: Each loop cycle runs the same code, so the time per cycle stays the same no matter how many times it repeats.
Understanding how Arduino sketches run helps you write efficient code and explain how your program behaves over time.
What if we added a loop inside the loop() function that runs n times? How would the time complexity change?
Practice
setup() function in an Arduino sketch?Solution
Step 1: Understand the role of
Thesetup()setup()function runs once when the Arduino starts to prepare the board.Step 2: Compare with
Theloop()loop()function runs repeatedly, butsetup()runs only once.Final Answer:
To run code once at the start to initialize settings -> Option AQuick Check:
setup()runs once = B [OK]
- Confusing setup() with loop()
- Thinking setup() runs repeatedly
- Believing setup() stops the program
Solution
Step 1: Recall Arduino sketch structure
The Arduino sketch must havesetup()andloop()functions defined with void return type.Step 2: Check options for correct function names
Only void setup() { } void loop() { } usessetup()andloop()correctly.Final Answer:
void setup() { } void loop() { } -> Option AQuick Check:
Correct function names = C [OK]
- Using wrong function names like main()
- Missing either setup() or loop()
- Using incorrect return types
void setup() {
Serial.begin(9600);
Serial.println("Start");
}
void loop() {
Serial.println("Looping");
delay(1000);
}Solution
Step 1: Analyze setup() output
Serial.begin(9600)starts serial communication, then "Start" is printed once.Step 2: Analyze loop() output
Insideloop(), "Looping" is printed every 1000 milliseconds (1 second) repeatedly.Final Answer:
Start printed once, then Looping printed every second -> Option DQuick Check:
setup() once, loop() repeats = D [OK]
- Thinking setup() runs repeatedly
- Forgetting Serial.begin() is needed
- Assuming no delay means no output
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
Inloop(), the linedigitalWrite(13, HIGH)is missing a semicolon at the end.Step 2: Verify other parts
pinModeis correctly insetup(),delay()is allowed inloop(), anddigitalWrite()has correct arguments.Final Answer:
Missing semicolon after digitalWrite(13, HIGH) -> Option BQuick Check:
Syntax error: missing semicolon = A [OK]
- Putting pinMode in loop() unnecessarily
- Thinking delay() is not allowed in loop()
- Miscounting digitalWrite() arguments
Solution
Step 1: Understand blinking 5 times
Sinceloop()runs forever, use a counter variable insideloop()to count blinks.Step 2: Evaluate options
Putting blinking code insetup()runs once, so it performs only one blink cycle, not 5. Removingloop()is invalid. Using delay(5000) only delays, does not blink 5 times.Final Answer:
Use a counter variable in loop() and stop blinking after 5 times -> Option CQuick Check:
Counter in loop() controls blink count = A [OK]
- Trying to remove loop() function
- Putting repeated code only in setup()
- Using delay() to count blinks incorrectly
