Motor speed control with PWM in Arduino - Time & Space Complexity
We want to see how the time the program takes changes as we adjust motor speed control using PWM.
How does the program's work grow when we change the input size or speed settings?
Analyze the time complexity of the following code snippet.
const int motorPin = 9;
void setup() {
pinMode(motorPin, OUTPUT);
}
void loop() {
for (int speed = 0; speed <= 255; speed++) {
analogWrite(motorPin, speed);
delay(10);
}
}
This code gradually increases motor speed from 0 to 255 using PWM signals.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The for-loop that runs from speed 0 to 255.
- How many times: It runs 256 times each time the loop() function runs.
As the speed range increases, the number of loop steps grows directly with it.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 steps |
| 100 | 100 steps |
| 255 | 255 steps |
Pattern observation: The work grows in a straight line as the speed steps increase.
Time Complexity: O(n)
This means the time to run grows directly in proportion to the number of speed steps.
[X] Wrong: "The delay inside the loop does not affect the time complexity."
[OK] Correct: The delay adds fixed time per step, so total time grows with the number of steps, affecting overall time.
Understanding how loops and delays affect program time helps you explain how embedded systems handle tasks efficiently.
"What if we replaced the for-loop with a while-loop that increments speed by 5 each time? How would the time complexity change?"
Practice
Solution
Step 1: Understand PWM function
PWM (Pulse Width Modulation) controls speed by rapidly switching power on and off.Step 2: Compare options
Only It switches power on and off quickly to control speed. correctly describes PWM's role in speed control, others describe different functions.Final Answer:
It switches power on and off quickly to control speed. -> Option BQuick Check:
PWM controls speed by switching power on/off [OK]
- Thinking PWM changes voltage permanently
- Confusing speed control with direction control
- Believing PWM measures speed
Solution
Step 1: Identify PWM function
analogWrite(pin, value) sets PWM signal on a pin with value 0-255 controlling speed.Step 2: Check other functions
digitalWrite sets HIGH/LOW only, pinMode sets pin mode, delay pauses code; none control PWM speed.Final Answer:
analogWrite(pin, value) -> Option CQuick Check:
Use analogWrite for PWM speed control [OK]
- Using digitalWrite instead of analogWrite
- Confusing pinMode with speed control
- Trying to use delay to control speed
analogWrite(9, 128);?
Solution
Step 1: Understand PWM value range
analogWrite value ranges 0 (off) to 255 (full speed). 128 is about half of 255.Step 2: Interpret speed from value
Value 128 means roughly half the power, so motor runs at about half speed.Final Answer:
Motor runs at about half speed -> Option DQuick Check:
128/255 ≈ half speed [OK]
- Thinking 128 means full speed
- Assuming motor is off at 128
- Believing speed can be doubled
int speed = 300; analogWrite(6, speed);
Solution
Step 1: Check PWM value limits
analogWrite accepts values from 0 to 255. 300 is outside this range.Step 2: Verify other options
Pin 6 supports PWM, analogWrite takes two arguments, and int is fine for speed.Final Answer:
Speed value is out of PWM range (0-255). -> Option AQuick Check:
PWM value must be 0-255 [OK]
- Using values above 255 for PWM
- Thinking analogWrite needs 3 arguments
- Believing PWM pins are limited incorrectly
Solution
Step 1: Understand smooth speed increase
Gradually increasing PWM value from 0 to 255 in small steps with delay creates smooth speed change.Step 2: Analyze code snippets
for(int speed=0; speed<=255; speed++) { analogWrite(10, speed); delay(20); } uses a for loop from 0 to 255 with delay(20)ms, totaling about 5 seconds. Others either jump speed or use digitalWrite which is not PWM.Final Answer:
for(int speed=0; speed<=255; speed++) { analogWrite(10, speed); delay(20); } -> Option AQuick Check:
Gradual PWM increase with delay = smooth speed [OK]
- Using digitalWrite instead of analogWrite
- Jumping speed without gradual steps
- Not using delay to slow speed change
Ready to prove it?
Pass the test and this pattern moves unknown
