Lesson 4 – Controlling the Motors

Python 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.

bird.setMotors(50,50) 
The setMotors() function takes two parameters, both of which must be between 0 and 100. The first parameter is the speed of the lift wheel, and the second parameter is the speed of the right wheel.

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.

bird.setMotors(50,50) # Motors on 
sleep(1) 
bird.stop() # Motors off

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 and convert their response to a float value. Then you turn the motors on for that amount of time.

userResponse = input("Enter a time in seconds: ") 
time = float(userResponse) 
bird.setMotors(100,0) 
sleep(time) 
bird.stop()

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, 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 indented lines inside the if part of the if-else statement. If the comparison is false, the program runs all indented lines inside the else part of the if-else statement. Then the program moves on to the next unindented line. In this case, if time is less than zero, the program prints an error message. Otherwise, it turns the Finch for time seconds.

userResponse = input("Enter a time in seconds: ") 
time = float(userResponse) 
if (time < 0): # If the time is less than 0 
print("Negative time does not compute!") # Error 
else: # Otherwise 
bird.setMotors(100,0) # Turn for that time 
sleep(time) 
bird.stop()

Exercise 4

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

Note: Remember, the equals sign is used for assignment in Python. The double equals sign, on the other hand, returns True if two things are equal and False otherwise. 

color = input("Please enter a letter: ") 
if (color == 'g'): 
bird.setBeak(0,100,0) 
else: 
print('Sorry, that is not my favorite letter!') 
sleep(1) 
bird.stopAll()

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 operator and. The Boolean expression below is true if BOTH of the statements in parentheses are true. Otherwise, it is false. 

(speed >= -100) and (speed <= 100)

The logical 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.

(speed >= -100) or (speed <= 100)

Exercise 6

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

userResponse = input("Please enter a speed (-100 to 100): ") 
speed = int(userResponse) 
if (speed >= -100) and (speed <= 100): # If both are true 
bird.setMotors(speed,speed) #Move forward 
sleep(1) 
bird.stop() 
else: #Otherwise
print("That speed is not valid!") # Error

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