Skip to content

Servo Control

  • Lesson
  • 1 min read
  • 5 of 6 in this chapter

What is a servo?

A Servo is a rotational motor that can be set to exact positions.

Let's program the Servo

First, we need to set an initial Servo Position. We will do this near all the other motor declarations.

Java
while (opModeIsActive()) {
 double leftPower;
 double rightPower;
 
 double servoPosition = 0.45;

Now, all that's left is to add code that takes user input to change the servo position. The servoPosition variable will be set by user input (clicking the 'B' button) and will then change the actual servo's position on the robot through the code added previously.

Java
if (gamepad1.a) {
 clawServo.setPosition(servoPosition);
 }
 
 if (gamepad1.b){
 clawServo.setPosition(-servoPosition);
 }