Servo motor control with Servo library in Arduino - Time & Space Complexity
When controlling a servo motor using the Servo library, it is helpful to understand how the time the program takes changes as we send commands repeatedly.
We want to know how the program's running time grows when we move the servo many times.
Analyze the time complexity of the following code snippet.
#include <Servo.h>
Servo myServo;
void setup() {
myServo.attach(9);
}
void loop() {
for (int pos = 0; pos <= 180; pos += 1) {
myServo.write(pos);
delay(15);
}
}
This code moves the servo from 0 to 180 degrees in steps of 1 degree, pausing briefly at each step.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The for-loop that moves the servo position from 0 to 180 degrees.
- How many times: It runs 181 times each time the loop() function runs.
As the number of positions to move the servo increases, the total time grows proportionally.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 servo moves and delays |
| 100 | 100 servo moves and delays |
| 180 | 181 servo moves and delays |
Pattern observation: The time grows linearly as the number of servo positions increases.
Time Complexity: O(n)
This means the time to complete the servo movements grows directly in proportion to the number of positions you move through.
[X] Wrong: "The delay inside the loop does not affect the time complexity because it's just waiting."
[OK] Correct: Even though delay pauses the program, it still adds to the total time the program takes, so it grows with the number of loop steps.
Understanding how loops and delays affect program time helps you explain how your code behaves in real devices, showing you can think about efficiency in embedded systems.
"What if we changed the step size from 1 degree to 10 degrees? How would the time complexity change?"
Practice
Servo library in Arduino?Solution
Step 1: Understand the Servo library role
The Servo library is designed to control servo motors by sending signals to set their position.Step 2: Compare options with library purpose
Only To control servo motors easily by setting angles matches the library's function of controlling servo motors by setting angles.Final Answer:
To control servo motors easily by setting angles -> Option AQuick Check:
Servo library controls servo motors = D [OK]
- Confusing Servo library with sensor or display libraries
- Thinking it manages Wi-Fi or other hardware
- Assuming it reads data instead of controlling motors
Solution
Step 1: Recall the Servo attach syntax
The Servo library uses the methodattach(pinNumber)to connect the servo to a pin.Step 2: Match the correct method call
Onlyservo.attach(9);matches the correct syntax.Final Answer:
servo.attach(9); -> Option AQuick Check:
Attach servo with attach(pin) = C [OK]
- Using incorrect method names like attachPin or connect
- Forgetting the object name before attach
- Using wrong syntax without parentheses
#include <Servo.h>
Servo myServo;
void setup() {
myServo.attach(10);
myServo.write(90);
}
void loop() {
// no code here
}Solution
Step 1: Analyze the setup function
The servo is attached to pin 10 and immediately set to 90 degrees usingwrite(90).Step 2: Check the loop function
The loop is empty, so no further commands change the servo position.Final Answer:
The servo moves to 90 degrees and stays there -> Option BQuick Check:
Servo set once in setup = stays at 90 [OK]
- Thinking servo moves continuously without commands
- Assuming loop is needed to move servo once
- Confusing servo rotation with continuous rotation motors
#include <Servo.h>
Servo servo1;
void setup() {
servo1.attach(5);
servo1.write(180);
}
void loop() {
servo1.write(0);
delay(1000);
servo1.write(180);
delay(1000);
}Solution
Step 1: Check servo declaration and attach
The servo object is declared asServo servo1;and attached to pin 5, which is valid.Step 2: Review write and delay usage
Thewrite()commands set angles 0 and 180 with delays of 1000 milliseconds, all correctly terminated with semicolons.Final Answer:
No error; the code will run correctly -> Option CQuick Check:
Correct syntax and logic = A [OK]
- Assuming pin 5 is invalid without checking board specs
- Missing semicolons after delay calls
- Incorrect servo object declaration syntax
#include <Servo.h>
Servo myServo;
void setup() {
myServo.attach(6);
}
void loop() {
// Which code goes here?
}Solution
Step 1: Understand smooth sweeping
Sweeping smoothly means moving the servo in small steps with short delays between each step.Step 2: Analyze each option
for(int pos=0; pos<=180; pos+=1) { myServo.write(pos); delay(15); } for(int pos=180; pos>=0; pos-=1) { myServo.write(pos); delay(15); } uses two for-loops to increment and decrement the angle by 1 degree with 15 ms delay, creating a smooth sweep. Options A, C, and D do not create smooth back-and-forth movement.Final Answer:
for(int pos=0; pos<=180; pos+=1) { myServo.write(pos); delay(15); } for(int pos=180; pos>=0; pos-=1) { myServo.write(pos); delay(15); } -> Option DQuick Check:
Smooth sweep needs incremental steps and delays = B [OK]
- Using only two positions without gradual steps
- Missing delay causing jerky movement
- Re-attaching servo inside loop causing errors
Ready to prove it?
Pass the test and this pattern moves unknown
