C-1 Driving
First, you'll code an app to make your robot drive forward. Then you'll modify the app so the robot drives forward and then backward.
Create New App Template
If necessary, log in to the Arduino Create web editor, or open the Arduino IDE desktop editor.
Create a new app template. If you need a reminder, here are instructions for creating a new app template.
Rename App
Rename the the new app as: driving_test
If you need a reminder, here are instructions for how to rename an app.
Add Block Comment
Add this block comment at the beginning of your app code. Modify the comment to list your information.
Include RedBot Library
Arduino apps can include one or more libraries. A library is a pre-built code file that makes it easier to program certain things in your app.
There is a SparkFun RedBot Library that makes it much easier to control the motors and sensors connected to your RedBot.
You will need to add a copy of the SparkFun RedBot Library to your code editor, which is a one-time process. Then you will also need to include a copy of this library in each new robot app.
You should now have an #include
statement for the RedBot library (filename: RedBot.h
) listed at the beginning of your app code.
Create Object for Motors
The RedBot.h
library contains Arduino code that defines different classes of objects. Each class defines a set of properties (variables) and methods (functions) for a specific type of object.
Your robot apps will use these classes to create objects in your app code. An object is a special type of variable that represents a specific instance (member) of a class. An object has all the properties and methods defined for that class.
The objects in your robot app code will correspond to real-life parts on your robot (such as: motors, wheel encoders, mechanical bumpers, etc.).
One of the classes in the RedBot library that you'll use in your apps is the RedBotMotors
class, which defines methods to control your robot's left and right motors.
You'll need to create a new RedBotMotors
object as a global variable in your app, so you can use this object to control your robot's motors. Add this code statement before the setup()
function:
This code statement does two things (in order):
It declares the class for the object. This is similar to declaring a data type for a variable. In this case,
RedBotMotors
is the name of the class in the RedBot library being used to create the new object.It declares the object's name. In this case, the object will be named
motors
. Just like with other variables, you get to decide what to name your objects.
Add Code for "Press to Start"
As a reminder, whenever you upload a new app to your robot, the new app starts running immediately (even before you've had a chance to unplug the USB cable from your robot). In a worst-case scenario, your robot might accidentally drive off your desk and crash onto the floor.
Therefore as a safety feature, most of the robot apps in these tutorials will use an if
statement within the loop()
function to check whether the D12 button on the circuit board has been pressed before making the robot drive around. When the button is pressed, the speaker and built-in LED will beep and blink as a confirmation before performing other robot actions (such as driving, etc.).
Create global variables for the LED pin, speaker pin, and button pin by adding this code before the setup()
function:
Set the pin modes for the LED, speaker, and button by adding this code within the setup()
function:
Check whether the button has been pressed by adding this code within the loop()
function:
Basically, this code for "Press to Start" is the same as the final version of the "Hello World" app you completed in the previous tutorial — except the second delay()
statement was removed (because the code that will be added for "other robot actions" will act as a delay between each check of the button).
HOW TO COPY CODE: When using this IoT code guidebook, you can copy a code block simply by clicking the copy icon displayed in the upper right of the code block.
Drive Forward
The RedBotMotors
class defines a method named drive()
that can be used to drive the motors either forward or backwards. The RedBotMotors
class also defines a method named brake()
that can be used to stop the motors.
Therefore, your motors
object has a motors.drive()
method and a motors.brake()
method (as well as any other methods defined in the RedBotMotors
class).
When the motors.drive()
method is used in your code, the motors will start and will keep driving continuously (sort of like cruise control on a car). You'll use a delay()
statement to allow the motors to drive for a certain amount of time before turning the motors off with the motors.brake()
method.
When the robot's button is pressed, let's make your robot drive forward for 2 seconds and then brake. Add this code within the if
statement in the loop()
function (after the noTone()
statement):
The motors.drive()
method requires one parameter inside its parentheses:
The motor power, which can be any integer (whole number) between
-255
and255
. A positive power drives the robot forward, and a negative power drives the robot backward. A larger absolute power produces a faster driving speed (-255
and255
are the fastest speeds, while-1
and1
are the slowest speeds). In this case, the power will be200
.
SLOW DOWN: Driving the motors at high power can sometimes cause the wheels to slip due to insufficient traction with the surface. If you notice traction issues while driving, use a lower motor power (slower speed). In general, use a motor power of 200 or less for driving.
Upload App to Robot
Follow the steps to upload the app to your robot:
Connect Robot to Computer
Turn on Robot Power
Select Correct Board and Port
Upload App to Robot
Unplug the USB cable from the robot, and place the robot on the floor. Be sure a path of at least 3 feet in front of the robot is clear of any obstacles. (Just to be safe, also be sure a path of 3 feet behind the robot is clear — in case your robot's motor wires were accidentally reversed during assembly.)
Press the D12 button on your robot's circuit board. Your robot should beep and then drive forward for 2 seconds (about 30 inches).
TROUBLESHOOTING
If your robot doesn't drive at all, first check that its Motor switch is set to RUN. If the switch was correct, next check the left and right motor wires on the circuit board to verify the red and black wires are correctly plugged in. If the wires were correct, replace the batteries in the robot's battery pack.
If your robot spins clockwise (to the right), unplug and reverse the red and black wires of the right motor on the circuit board.
If your robot spins counter-clockwise (to the left), unplug and reverse the red and black wires of the left motor on the circuit board.
If your robot drives backward, first check your app code to make sure you used a positive value for the motor power (not a negative value). If the app code is correct, unplug and reverse the red and black wires for each motor on the circuit board.
NOTE: If your robot drives forward but not in a perfectly straight line, this is actually normal. Even though the two motors are supposed to be identical and are receiving identical power, they might not necessarily rotate at the exact same rate. Even a minor difference in their rotation rates will cause the robot to drift either to the left or to the right as it drives (depending on which motor is rotating more slowly).
Later in this tutorial, you'll learn how to use the wheel encoders to measure the rotation rates of the motors, in order to make small power adjustments to each motor so the robot drives in a straight line.
Drive Backward (Reverse)
Next you'll modify the app so the robot will drive forward for 2 seconds, stop briefly, drive backward for 2 seconds, and finally stop approximately at its starting point.
Add this code within the if
statement in the loop()
function (after the motors.brake()
statement):
Upload the modified app to your robot. Unplug the USB cable from the robot, and place the robot on the floor. Be sure a path of at least 3 feet in front of the robot is clear of any obstacles.
Press the D12 button on your robot's circuit board. Your robot should beep and then drive forward for 2 seconds (about 30 inches). It should stop and pause for 1 second before driving backward for 2 seconds (about 30 inches), returning approximately to its starting point.
If you want, you can modify the app further to try different motor powers or different delay times — just be sure you have a clear driving path to avoid crashing your robot into any obstacles.