Writing a Remote Control Program in Arduino
FBX Robot Curriculum, Module No. 5
Introduction
In this module, we will cover the following topics:
Planning Out a Basic Remote Control Program
Introducing the Cello Track
Diving Back into our Arduino Code
Putting it All Together
Booleans
If-Statements
Switch Statements
Before We Get Started...
Make sure that you have access to a Windows, Ubuntu (Linux), or macOS computer/laptop (no Chromebooks)! Although Arduino does provide a Cloud Editor on ChromeOS, the Arduino board we will be working with today (ESP32-WROOM-32D) is not compatible with this editor.
You can follow this tutorial even if your robot is only partially built! As long as you have access to the Arduino Board that comes with your robot (and a USB cable), you will be able to follow along with this module.
1. Planning Out a Basic Remote Control Program
You might be asking, why are we planning a remote control program?
In order to test the functionality and limitations of our robot once we implement our remote control code, we should have a basic set of tasks that we expect the robot to complete.
What should we expect our robot to do?
Once we are done implementing our remote control code, our robot should be able to…
Drive Forward/Backward
Turn Left/Right
Open/Close the Claw
Move the Arm Up/Down
[ insert top-down diagram/photo of FBX with labeled functionality]
Let’s come up with a basic remote control program that will test all of our robot’s eventual functionality.
Since we will be creating 10 additional functions today (wow!), we will need a slightly more sophisticated test…
Introducing the Cello Track
A good basic test of all our robot’s remote control code is to have our robot drive the Cello Track, as shown here.
The Blue part of the track represents when the robot is driving forward.
The Purple part of the track represents when the robot is driving backward.
The Green dot represents when the robot opens and closes, and raises/lowers its claw arm.
Again, the purpose of the Cello Track is to ensure that our remote controlled robot is able to do everything that it is expected to do. First, the robot will drive forward following the figure-eight pattern, starting from the center. Next, the robot will drive backwards in a straight line, starting from the center. Lastly, the robot will operate the claw and arm, completing the track.
Now that we have a more concrete goal for our robot to achieve, let’s dive back into our Arduino code!
2. Diving Back Into our Arduino Code
A Quick Refresher...
At the end of our last session, our robot code looked something like this:
In today’s module, we will be expanding on this code.
First, let’s tackle the easier functions: car_back()
and car_left()
.
car_back()
Function
car_back()
FunctionLet’s write the function car_back()
, which will make the clawbot drive backwards.
To do this, we will again be using two of Arduino’s built-in functions: analogWrite()
and digitalWrite()
.
First, let's define the car_back()
function in Arduino as follows:
...How do we call analogWrite()
and digitalWrite()
again?
digitalWrite()
is formatted as follows:
As a reminder, this function controls the direction that the motors are spinning.
When you call this function in your own code, be sure to:
Replace
MOTOR_CTRL
with the actual name of the variable you are trying to set (i.e., If I want to set my left motors usingdigitalWrite
, I would replaceMOTOR_CTRL
withML_Ctrl
.Replace
HIGH
orLOW
with HIGH to drive forwards, or LOW to drive backwardsEnd your calls to
digitalWrite
with a semicolon;
analogWrite()
is formatted as follows:
As a reminder, this function controls the speed of the motors.
When you call this function in your own code, be sure to:
Replace
MOTOR_PWM
with the actual name of the variable you are trying to set (i.e., If I want to set my left motors using analogWrite
, I would replaceMOTOR_PWM
withML_PWM
.Replace
0 -> 255
with a whole number between 0 and 2550 means the robot will not move, 255 means maximum speed
End your calls to
analogWrite
with a semicolon;
When you're finished, your car_back()
function should look something like this:
This function should look pretty familiar...
The car_back()
function is almost identical to the car_front()
function, with the exception of the two calls to digitalWrite()
.
As a hint, this is also the case with car_right()
and car_left()
!
car_left()
Function
car_left()
FunctionNow, let’s write the car_left()
function.
As mentioned above, the car_left()
function is going to be almost identical to the car_right()
function, with a slight difference…
Pay close attention to the direction that the left and right motors are moving…
When the robot is turning left (CCW)...
The right motors drive forward
The left motors drive backward
Since the only difference between car_right()
and car_left()
is the direction that the left/right motors are turning (i.e., they are opposite), we can now write the car_left()
function, using car_right()
as a reference:
Claw Functions
Now, let’s move on to our claw functions.
While the other 5 functions controlled motors that are connected to the robot’s drive train, claw_open()
and claw_close()
both use the claw motor.
Luckily, since the claw is only controlled by a single motor (MC
), this will make writing our claw_open()
and claw_close()
functions much easier.
Again, we will be making use of analogWrite()
and digitalWrite()
.
Similar to how digitalWrite()
was used to change the direction of the drive motors, we can use it to choose the function of our claw (opening or closing). Additionally, we can also use analogWrite()
to control the speed at which the claw operates.
[ Insert diagram of claw closed and then open ]
Since you now have a pretty foundational understanding of digitalWrite()
and analogWrite()
, try writing the claw_open()
and claw_close()
functions on your own!
When you’re finished, your claw_open() and claw_close() functions should look something like this:
Arm Functions
While you’re at it, try writing the arm_up()
and arm_down()
functions; These functions do exactly what they sound like they do— they move the robot’s arm up and down!
[ insert image of FBX arm raised ]
When you’re finished, your arm_up()
and arm_down()
functions should look something like this:
Now that we have written all of our claw and arm functions, there’s one additional function we need to write— armclaw_stop()
.
Why do we need the armclaw_stop()
function?
In order for the arm/claw to stop moving, we need to explicitly order them to do so!
Using the car_stop()
function as a reference, try to write the armclaw_stop()
function on your own!
When you’re finished, your armclaw_stop()
function should look something like this:
Last updated
Was this helpful?