Duty cycle control for motor/LED in Embedded C - Time & Space Complexity
We want to understand how the time it takes to control the duty cycle changes as we adjust the input size.
Specifically, how does the program's work grow when changing the duty cycle steps?
Analyze the time complexity of the following code snippet.
void setDutyCycle(int dutyCyclePercent) {
for (int i = 0; i < 100; i++) {
if (i < dutyCyclePercent) {
turnOnOutput();
} else {
turnOffOutput();
}
delayMicroseconds(10);
}
}
This code sets the duty cycle by turning the output on or off in a loop of 100 steps, simulating PWM control.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The for-loop running 100 times to set output on/off.
- How many times: Always 100 times, regardless of dutyCyclePercent.
The loop always runs 100 times, so the work stays the same no matter the duty cycle value.
| Input Size (dutyCyclePercent) | Approx. Operations |
|---|---|
| 10 | 100 loop steps |
| 50 | 100 loop steps |
| 90 | 100 loop steps |
Pattern observation: The number of operations does not change with input size; it stays constant.
Time Complexity: O(1)
This means the time to run the code stays the same no matter what duty cycle value you use.
[X] Wrong: "If I increase the duty cycle percent, the program will take longer to run because it turns on more."
[OK] Correct: The loop always runs 100 times no matter what, so the total steps stay the same. Turning on or off inside the loop doesn't change how many times the loop runs.
Understanding how loops and input sizes affect time helps you explain how embedded systems keep timing precise and predictable.
"What if the loop ran up to the dutyCyclePercent value instead of always 100? How would the time complexity change?"
Practice
Solution
Step 1: Understand duty cycle meaning
The duty cycle is the fraction of one period in which a signal or system is active.Step 2: Relate to motor/LED control
For motors or LEDs, it controls how long the device is ON in each cycle, affecting speed or brightness.Final Answer:
The percentage of time the device is ON during each cycle -> Option AQuick Check:
Duty cycle = ON time percentage [OK]
- Confusing duty cycle with voltage
- Thinking it controls frequency
- Assuming it controls temperature
Solution
Step 1: Understand timer resolution
An 8-bit timer counts from 0 to 255, so max value is 255.Step 2: Calculate 50% duty cycle value
50% of 255 is approximately 128, so setting OCR0 to 128 sets 50% duty cycle.Final Answer:
OCR0 = 128; // Assuming 8-bit timer with max 255 -> Option CQuick Check:
50% of 255 = 128 [OK]
- Using max value for 50% duty cycle
- Using zero for 50% duty cycle
- Confusing 25% with 50%
unsigned char duty = 64; unsigned char max = 255; float dutyCycle = (duty / (float)max) * 100;
Solution
Step 1: Calculate duty/max ratio
Divide duty (64) by max (255): 64 / 255 ≈ 0.25098.Step 2: Convert ratio to percentage
Multiply by 100: 0.25098 * 100 ≈ 25.1%.Final Answer:
25% -> Option AQuick Check:
(64/255)*100 ≈ 25% [OK]
- Using integer division without casting
- Confusing duty value with percentage
- Rounding incorrectly
unsigned char duty = 75; unsigned char max = 255; OCR0 = duty / max * 255;
Solution
Step 1: Analyze division operation
Both duty and max are unsigned char, so duty/max does integer division: 75/255 = 0 (integer).Step 2: Effect on OCR0 assignment
Multiplying 0 by 255 results in 0, so OCR0 is set to 0, not 75% duty cycle.Final Answer:
Integer division causes duty/max to be zero -> Option BQuick Check:
Integer division truncates to zero [OK]
- Ignoring integer division rules
- Assuming OCR0 cannot be assigned
- Using wrong max value
Solution
Step 1: Understand smooth brightness control
Smooth brightness change requires gradually changing the duty cycle from 0 to max.Step 2: Implement gradual PWM change
Increment OCR0 from 0 to 255 in a loop with delays to create smooth brightness ramp.Final Answer:
Use a loop to increment OCR0 from 0 to 255 with a delay between steps -> Option DQuick Check:
Gradual OCR0 change = smooth brightness [OK]
- Setting OCR0 once for full brightness only
- Using fixed mid value for all brightness
- Not using PWM for brightness control
Ready to prove it?
Pass the test and this pattern moves unknown
