Next, you'll code an app that uses the accelerometer to detect if the robot is upside-down by measuring its pitch and roll. If the robot is upside-down, it will stop its motors and make a distress sound.
Open your Arduino code editor, and create a new app template.
Add a block comment at the beginning of the app code to identify your new app:
Rename the the new app as: upside_down_test
If you need a reminder, here are instructions for how to rename an app.
Follow the steps to include the SparkFun RedBot Library in your app. (You don't need to add the library to your code editor again — just include the library in this new app.)
Your app will need to create new objects (as global variables) to represent the robot's motors, button, and accelerometer. Add this code before the setup()
function:
This app will use the "Press to Start" code. You'll press the D12 button to "start" the robot. Once the robot is "started," you can press the button again to "pause" the robot. (Pressing the button yet again will "start" the robot again.)
You need to declare global variables for the LED pin and speaker pin. You also need a global variable to keep track of whether or not the robot has been "started." Add this code before the setup()
function:
Set the pin modes for the LED and speaker by adding this code within the setup()
function:
Next, you need to add the custom function named checkButton()
that will check whether the D12 button is pressed, in order to "start" or "pause" the robot.
Add this custom function after the loop()
function:
Now add this code within the loop()
function:
Later, you'll add the code to be performed when the robot is started or paused.
You'll add a custom function named checkUpsideDown()
which will contain code to use readings from the accelerometer to detect when the robot's pitch or roll is greater than 90° (which indicates the robot has flipped over).
The function will use the absolute value for pitch and roll because 90° and -90° both indicate the robot is about to tip over.
This custom function will return a boolean value when it is called:
If the robot's pitch or roll is greater than 90 degrees, the function returns a value of true
Otherwise, the function returns a value of false
Add this custom function after the loop()
function:
Functions that do not return a value have void
listed as the data type before the function's name.
If a function does return a value when called, the data type of the returned value must be listed before the function's name.
In this case, bool
is listed before the checkUpsideDown()
function's name because the function will return a value of either true
or false
.
When the D12 button is pressed to "start" the robot, we want the robot to check whether it is upside-down by calling the checkUpsideDown()
function and then performing appropriate actions based on the result:
If the robot is upside-down, it should brake its motors and make a distress sound.
Otherwise, the robot should drive forward.
The boolean value returned by the checkUpsideDown()
function will be stored in a local variable named upsideDown
.
Add this code within the if
statement in the loop()
function, so it will be performed when started
is true
:
Once the robot has been "started," the D12 button can be pressed again to "pause" the robot.
When the robot is "paused," we want make the robot stop driving.
Add this code statement within the else
statement in the loop()
function, so it will be performed when started
is false
:
Follow the steps to connect your robot to your computer, and upload the app.
Unplug the USB cable from the robot, and hold the robot in the air with both hands.
Press the D12 button to "start" the robot. Its motors will start as it "drives" forward.
Simulate the robot flipping upside-down by rotating it until the pitch (front-to-back tilt) or roll (side-to-side tilt) is greater than 90° in any direction. The robot should brake its motors and make a distress sound.
Then rotate the robot back to a "normal" position (where its pitch and roll are less than 90°). The distress sound should stop, and the motors should start again.
You can press the D12 button to "pause" the robot. If you want to repeat the test, press the button to "start" the robot again.