Motor direction with H-Bridge (L293D/L298N) in Arduino - Time & Space Complexity
When controlling a motor with an H-Bridge, the code runs commands to set motor direction and speed.
We want to see how the time to run this code changes as we repeat motor commands more times.
Analyze the time complexity of the following code snippet.
const int motorPin1 = 3;
const int motorPin2 = 4;
void setup() {
pinMode(motorPin1, OUTPUT);
pinMode(motorPin2, OUTPUT);
}
void loop() {
for (int i = 0; i < 100; i++) {
digitalWrite(motorPin1, HIGH);
digitalWrite(motorPin2, LOW);
delay(1000);
}
}
This code sets motor direction by turning one pin on and the other off repeatedly 100 times, each with a delay.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The for-loop that runs 100 times.
- How many times: The motor direction commands inside the loop run exactly 100 times.
Each time we increase the loop count, the number of motor commands grows the same amount.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 motor direction sets |
| 100 | 100 motor direction sets |
| 1000 | 1000 motor direction sets |
Pattern observation: The number of operations grows directly with the number of loop repetitions.
Time Complexity: O(n)
This means the time to run the motor commands grows in a straight line as we increase the number of repeats.
[X] Wrong: "The motor commands inside the loop run instantly and don't add to time."
[OK] Correct: Each command takes some time to execute, and repeating them more times adds up linearly.
Understanding how loops affect execution time helps you write efficient code for hardware control, a useful skill in many projects.
"What if we added a nested loop inside the existing loop to run motor commands multiple times per iteration? How would the time complexity change?"
Practice
pinA and pinB?Solution
Step 1: Understand motor direction control
To spin the motor forward, one pin must be HIGH and the other LOW to create current flow in one direction.Step 2: Apply pin states for forward spin
SettingpinAHIGH andpinBLOW makes the motor spin forward.Final Answer:
Set pinA HIGH and pinB LOW -> Option DQuick Check:
Forward spin = pinA HIGH, pinB LOW [OK]
- Setting both pins HIGH or LOW thinking motor spins
- Confusing which pin should be HIGH for forward
- Assuming both pins LOW spins motor forward
pin1 HIGH and pin2 LOW to spin a motor forward using an H-Bridge?Solution
Step 1: Recall correct pin states for forward
Forward spin requires first pin HIGH and second pin LOW.Step 2: Match code to pin states
Code in digitalWrite(pin1, HIGH); digitalWrite(pin2, LOW); setspin1HIGH andpin2LOW correctly.Final Answer:
digitalWrite(pin1, HIGH); digitalWrite(pin2, LOW); -> Option BQuick Check:
Pin1 HIGH, Pin2 LOW = forward spin [OK]
- Swapping HIGH and LOW values
- Setting both pins HIGH or LOW
- Using analogWrite instead of digitalWrite
digitalWrite(3, LOW); digitalWrite(4, HIGH);
Solution
Step 1: Identify pin states and their meaning
Pin 3 is LOW and pin 4 is HIGH, which reverses the current flow compared to forward spin.Step 2: Determine motor direction
This pin combination makes the motor spin backward.Final Answer:
Motor spins backward -> Option AQuick Check:
LOW then HIGH = backward spin [OK]
- Assuming LOW then HIGH is forward spin
- Thinking motor stops when pins differ
- Confusing speed control with direction
digitalWrite(5, HIGH); digitalWrite(6, HIGH);
Solution
Step 1: Understand stopping motor with H-Bridge
To stop the motor, both pins should be LOW to cut power and stop current flow.Step 2: Analyze given pin states
Both pins are set HIGH, which causes the motor to brake but not coast to a stop.Final Answer:
Both pins HIGH causes motor to brake, not stop -> Option CQuick Check:
Stop motor = both pins LOW, not HIGH [OK]
- Setting both pins HIGH thinking motor stops
- Using INPUT mode instead of LOW
- Confusing brake with stop
Solution
Step 1: Set pins for forward spin and delay
Forward spin requires pin 7 HIGH and pin 8 LOW, then delay 3000 ms (3 seconds).Step 2: Stop motor by setting both pins LOW and delay
Both pins LOW stops motor, delay 2000 ms (2 seconds).Step 3: Set pins for backward spin and delay
Backward spin requires pin 7 LOW and pin 8 HIGH, delay 3000 ms.Final Answer:
digitalWrite(7, HIGH); digitalWrite(8, LOW); delay(3000); digitalWrite(7, LOW); digitalWrite(8, LOW); delay(2000); digitalWrite(7, LOW); digitalWrite(8, HIGH); delay(3000); -> Option AQuick Check:
Forward, stop, backward with correct pins and delays [OK]
- Swapping forward and backward pin states
- Not stopping motor between directions
- Using HIGH-HIGH which brakes but doesn't stop
Ready to prove it?
Pass the test and this pattern moves unknown
