Lesson 5 – Rotation Servos

Hummingbird Components

1 Rotation Servo, 1 Tri-color LED

Java Concepts

Generating random numbers

Teacher Materials

Get Access

In the previous lesson, you learned to use the position servo, which moves to a particular angle. In this lesson, you will learn to use the rotation servo. The rotation servo is a motor that can rotate at different speeds.

Plug in the rotation servo to SERVOS port 1 on the Bit. Make sure the black wire is aligned to ‘-,’ the red wire to ‘+,’ and the white wire to ‘S.’

To set the speed of the rotation servo, use the setRotationServo() method. This method requires two parameters. The first parameter is the port number of the servo (1-4), and the second is a speed from -100 to 100. A speed of 0 turns the motor off, while ±100 sets the motor to its maximum speed.

Exercise 1

Try out this code. It should make the rotation servo turn at full speed for one second. Then try out other speeds. What does a negative speed do?

Exercise 2

Write a program that makes the servo spin clockwise quickly for 2 seconds, then counterclockwise slowly for 3 seconds.

Exercise 3

Write a program that makes the motor run then stop 10 times.

To make the behavior of your robot less predictable, you may want to incorporate some variation so that your robot does something slightly different each time you run the program. To do this, you can use a random number generator. A random number generator is a function that picks a random number each time you call it. In Java, you can use the Random class.

First, declare an object of type Random.

Then use the nextInt() method to generate a random integer. This function takes a parameter that defines the range of numbers that you are interested in. For example, nextInt(6) will generate a random number from 0 to 5. This line of code will set the speed of the rotation servo to a random number between 0 and 100.

To use the Random class, you must import java.util.Random by placing this line of code at the beginning of your program.

Exercise 4

What do you think this code will do? Make a hypothesis, and then try it out. Remember to import java.util.Random before using the nextInt() method.

Exercise 5

Modify the code from Exercise 4 to set the rotation servo to a random speed between -100 and 100. Since you cannot generate integers less than 0, you will need to subtract after you generate a random number. For example, (rand.nextInt(51) – 50) will produce a random number between -25 and 25.

Exercise 6

Connect a tri-color LED to your Hummingbird. Use the nextInt() method to set the tri-color LED to a random color. Then use a loop to set the LED to a random color 5 times.

Extra Challenge

The Random class also includes other random number generators. For example, the nextDouble() method generates a random number between 0.0 and 1.0 (not including 1.0). Use this method to modify your code from Exercise 6 so that the LED stays each color for a random number of seconds between 0 and 1. Can you then make the LED stay each color for a random number of seconds between 0 and 5? Note: The nextDouble() method does not take any arguments.

Extra Challenge

Now that you know how to use the rotation servo, try building and programming a rover! Note that the two rotation servos on the rover point in opposite directions. This means that to move the rover forward, the two servos must move in opposite directions.

Back to Top