Lesson 4 – Controlling the Motors

Java Concepts

If-else statements, comparison operators, logical operators

Teacher Materials

Get Access

In the first lesson, you learned to use setMove() and setTurn() to make the Finch move in a straight line and turn in place. But there are a lot of other ways the Finch can move! What if you want the Finch to draw a figure-8 or move in a large circle?

Setting Motor Speeds

To get a wider range of movements, you can use setMotors() to set the speed of each wheel individually. This method takes two parameters, which are the speed of the left and right wheels.

To move the Finch using setMotors, you must turn the wheels on, pause the program, and then turn the wheels off. Sample code is shown below. This code moves the robot forward for one second. Note that you could also use setMotors(0,0) or stopAll() to turn off the motors.

Exercise 1

Try out the code shown above. The Finch moves in a straight line when the speed of the left and right wheels are the same. What happens when one speed is positive and the other speed is negative?

Exercise 2

Write a program that asks the user for the speeds of the right and left wheels and then moves the Finch for 2 seconds with those speeds. What happens when the right and left speeds are close together? What happens when they are far apart?

Exercise 3

Write a program to make the Finch move in a figure-8 pattern.

If-else Statements

In Exercise 2, you probably just assumed that the user entered speeds between -100 and 100, but it is a good idea to check whether the user entered valid values. You can do this with an if-else statement. An if-else statement enables your program to make a decision. For example, suppose you want to turn on the Finch’s motors for a certain number of seconds. First, you ask the user for input. Then you turn one motor on for that amount of time. Note that this code will allow the user to enter a decimal number.

However, this code will have an error if the user enters a negative number (try it!). Before you set the beak and pause, you want to check if time is less than zero. The code below shows how to do this with an if-else statement. The keyword if is followed by a comparison in parentheses, time < 0. This comparison is a Boolean expression, which means that it is either true or false. If the comparison is true, the program runs all the lines of code inside the curly braces {} that follow the if. If the comparison is false, the program runs all the lines of code inside the curly braces that follow the else. Then the program moves on to the next line following the if-else statement. In this case, if time is less than zero, the program prints an error message. Otherwise, it turns the Finch for time seconds.

Exercise 4

What do you think the code below will do? Make a hypothesis, and then try it out.

Note: To check whether two strings are equal in Java, you use the equals() method. To check whether two integers are equal, you would use an expression like (number == 3).

Exercise 5

Write a program that asks the user to enter r or l. If the user enters “r”, the Finch should make a wide turn to the right. Otherwise, the Finch should make a wide turn to the left.

Logical Operators

Sometimes you want to check whether two Boolean statements are true. For example, in Exercise 2, it would have been helpful to check whether the speed was greater than or equal to -100 and less than or equal to 100. You can do this with the logical and operator &&. The Boolean expression below is true if BOTH of the statements in parentheses are true. Otherwise, it is false. 

The logical or operator || or is slightly different. The Boolean expression below is true if EITHER of the statements in parentheses are true. It is false when both statements are false.

Exercise 6

Try out this code. Then modify it so that only slow speeds are valid.

Exercise 7

Write a program that asks the user for a wheel speed between 0 and 50. If the user enters an invalid value, the program should print an error. Otherwise, the Finch should turn for 5 seconds with the speed of the left wheel equal to the speed entered by the user and the speed of the right wheel equal to twice the entered speed. How does the radius of the turn depend on the speed entered by the user?

Exercise 8

Time for a Finch dance party! Make your Finch move and light up in a creative way. See if you can synchronize your Finch’s dance to music. Can you add user input so that the user chooses one of multiple dances? Make sure the program checks that the user enters a valid dance.

Back to Top