Bird
0
0
Arduinoprogramming~20 mins

Servo angle positioning in Arduino - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Servo Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the servo angle after this code runs?

Consider this Arduino code controlling a servo motor. What angle will the servo be set to after loop() runs once?

Arduino
 #include <Servo.h>
Servo myServo;

void setup() {
  myServo.attach(9);
  myServo.write(45);
}

void loop() {
  myServo.write(90);
  delay(1000);
  myServo.write(135);
  delay(1000);
  // loop ends here
}
A135 degrees
B45 degrees
C90 degrees
D0 degrees
Attempts:
2 left
💡 Hint

Remember the loop() function runs repeatedly. The last write() call sets the final angle before the loop restarts.

🧠 Conceptual
intermediate
1:30remaining
Why do we use myServo.attach(pin) in Arduino servo control?

What is the main purpose of calling myServo.attach(pin) in an Arduino program controlling a servo?

ATo calibrate the servo's speed
BTo set the initial angle of the servo
CTo power the servo motor from the Arduino
DTo specify which pin the servo signal wire is connected to
Attempts:
2 left
💡 Hint

Think about how the Arduino knows where to send the control signal.

🔧 Debug
advanced
2:30remaining
Why does this servo code cause the servo to jitter?

Look at this Arduino code snippet. The servo jitters and does not hold a steady angle. What is the cause?

Arduino
#include <Servo.h>
Servo myServo;

void setup() {
  myServo.attach(10);
}

void loop() {
  for (int angle = 0; angle <= 180; angle += 10) {
    myServo.write(angle);
    delay(15);
  }
}
AThe servo is not attached to the correct pin
BThe delay is too short for the servo to reach the angle before changing
CThe for loop should count down from 180 to 0
DThe servo library is not included properly
Attempts:
2 left
💡 Hint

Think about how fast the servo can physically move and how often the angle changes.

📝 Syntax
advanced
1:30remaining
Which option fixes the syntax error in this servo control code?

Find the option that corrects the syntax error in the following Arduino code:

#include <Servo.h>
Servo myServo;

void setup() {
  myServo.attach(9)
  myServo.write(90);
}

void loop() {
  // empty
}
AAdd a semicolon after <code>myServo.attach(9)</code>
BChange <code>myServo.attach(9)</code> to <code>myServo.attach(9);;</code>
CRemove the semicolon after <code>myServo.write(90);</code>
DAdd parentheses to <code>setup</code> like <code>setup();</code>
Attempts:
2 left
💡 Hint

Check the end of each statement for proper punctuation.

🚀 Application
expert
2:30remaining
How many unique angles will this servo sweep cover?

This Arduino code sweeps a servo from 0 to 180 degrees in steps of 30 degrees. How many unique angles will the servo be set to during one full sweep?

Arduino
#include <Servo.h>
Servo myServo;

void setup() {
  myServo.attach(9);
}

void loop() {
  for (int angle = 0; angle <= 180; angle += 30) {
    myServo.write(angle);
    delay(500);
  }
  while(true) {}
}
A4
B6
C7
D5
Attempts:
2 left
💡 Hint

Count all values from 0 to 180 stepping by 30, including both ends.