Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
The Arduino code editor does NOT autosave as you type (though you can turn this feature on in the web editor), so be sure to periodically save your code as you work. However, the Arduino code editor will save your code when verifying or uploading it.
At the top of the code editor panel, hover your mouse cursor over the button with 3 dots, and then select "Save" from the pop-up menu.
TURN ON AUTOSAVE: You can turn on autosave in the Arduino Create web editor (but not the desktop editor). Click the Preferences menu in the left navigation to display its menu options. Be sure "Enable Autosave" and "Save when verifying and uploading" are both checked.
Under the File menu, select "Save" – alternatively, you can click the Save icon (looks like a downward arrow) at the top of the code editor window.
By default, a new sketch will be given a generic filename that starts with sketch_
and includes the current date (plus a letter, such as: a
, b
, c
, etc.).
You should rename your new sketch to give it a filename that will make it easy for you (or anyone else) to identify and find the program later.
Hover over the 3-dot button at the top of the code editor panel, and then select "Rename Sketch..."
In the pop-up, replace the generic sketch name with a unique filename.
Use a name that will make it easy for anyone to identify this sketch later (especially once you have multiple sketches saved in your Arduino account).
The filename cannot contain spaces (instead you can use an underscore as a "space").
Your teacher might have a specific filename format that you should use for certain programs.
Click the OK button to save the new name and close the pop-up.
Rename New Program (not yet saved): Under the File menu, select "Save". A pop-up dialog window will appear. Enter a new filename, and then click the Save button.
Rename Existing Program (previously saved): Click the down arrow icon in the top-right corner of the code editor window, and then select "Rename" in the drop-down list. A small dialog will appear at the bottom of the code editor window. Enter a new filename, and then click the OK button.
You can use the "Save As" command to save a copy of an existing app as a new app with a different name. This is a useful way to copy an app and then modify its code.
Hover over the 3-dot button at the top of the code editor panel, and then select "Save As."
In the pop-up, replace the existing sketch name with a new filename.
Click the OK button to save the new app and close the pop-up.
Under the File menu, select "Save As". A pop-up dialog window will appear. Enter a new filename, and then click the Save button.
An Arduino program (or app) is also referred to as a "sketch" because the Arduino language is designed to allow you to quickly create a program — just like a sketch is a quick drawing.
This guidebook will primarily use the term "app" but just keep in mind that program, app, and sketch all mean the same thing in Arduino: a set of coded software instructions to control the operation of a computing device (which is your robot, in this case).
If necessary, click the Sketchbook menu link in the left navigation panel to display the Sketchbook menu options in the middle panel.
Click the New Sketch button at the top of the middle panel.
Under the File menu, select "New" – or you can click the New icon (looks like a document) at the top of an existing code editor window.
If you're using the Arduino Create web editor, your new app template will probably look like this:
If you're using the Arduino IDE desktop editor, your new app template will probably look like this:
In both cases, the starter code contains a setup()
function and a loop()
function. You'll need to add code within these functions to complete your app.
It is recommended to add a comment block at the beginning of your code to list a title for your app and any other information that might be helpful to you or anyone reviewing the program code.
A blank comment block is created with slashes and asterisks like this:
In between the asterisks, you can list as many lines of comments as you want or need. This would be a good place to list your app's name and perhaps describe its purpose. You could also include other information, such as your team information, your teacher's name and class period, etc.
Ask your teacher if there is specific information that should be listed in this block comment.
For example, your block comment might look like this:
You will use an Arduino code editor (also called an IDE – Integrated Development Environment) to create and save your Arduino apps for your robot. You'll also use the code editor to upload apps to your robot, in order to run them.
You can set up your computer to use an online code editor or a desktop code editor (though your teacher might instruct you to use a particular editor).
If possible, it is highly recommended that you use the online Arduino Create web editor to create and save your Arduino programs in the cloud.
The Arduino Create web editor is compatible with Windows, Mac, and Linux and with most browsers (Chrome, Firebox, Safari, and Edge). You will need to create a free Arduino account and also install a plugin (which requires Administrator privileges on your computer).
Here are steps for Getting Started with Arduino Editor on Various Platforms. The basic steps are:
Create a new Arduino account. Sign up with an email account that you can access which can receive outside email (some school email addresses cannot receive mail from outside the school district). Be sure to create a secure password that you will remember.
After signing up, you will receive an email with a link to verify your new account. Click the link.
After verifying your new account, follow these steps to install the Arduino Web Editor plugin on your computer (which requires Administrator privileges).
Once the browser plugin is installed, login to the Arduino Create Web Editor.
Alternatively, you can download and install the Arduino IDE desktop editor, which saves your Arduino programs locally on your computer.
Here is the page with links to download the Arduino IDE. The basic steps are:
Click the appropriate download link for your computer platform (Windows, Mac, Linux). If you're using a Windows computer and do not have Administrator access, download the Windows ZIP file for non-admin install. (The Mac download is a different ZIP file.)
The download page asks for a donation – however, you can click the free link to "just download."
After the download is complete, locate the ZIP file in the Downloads folder on your computer, and uncompress the ZIP file:
On a Windows computer, right-click the ZIP file, and select "Extract All." Browse to the destination where you want to save the Arduino application (choose Desktop if you don't have Administrator privileges to install in the Programs folder), and click "Extract."
On a Mac computer, just double-click the ZIP file. Then drag-and-drop the Arduino application to your Desktop (or to your Applications folder if you have Administrator privileges).
Double-click the Arduino application icon to start the code editor.
If you installed the Arduino desktop editor on Windows using the ZIP file, you also need to install drivers for your Arduino board (the RedBot is equivalent to an Arduino Uno board). You might need Administrator privileges to install the board drivers.
Connect your RedBot's USB cable to the RedBot's Mini-USB port, and connect the other end of the cable to a USB port on your computer.
Place the RedBot so it is standing upright on its back end (with its wheels in the air).
Turn the RedBot's Power switch to ON. (If you received an existing robot and the robot's wheels start spinning, temporarily turn the Motor switch to STOP.)
Follow these instructions from Arduino to install the board drivers on Windows. Alternatively, you could follow these instructions from SparkFun to install FTDI board drivers on Windows.
The Arduino code editor does NOT check your code syntax as you type, so be sure to periodically verify your code to check for errors. You can verify your code even if you're not done creating your entire app.
In order to verify the code, the code editor first needs to know which type of Arduino board is being used. The RedBot circuit board is equivalent to an Arduino Uno circuit board.
Once you've selected the correct type of Arduino board, the code editor should remember this selection for the future.
Click "Select Other Board & Port" in the drop down menu at the top of the code editor panel.
In the pop-up, select "Arduino/Genuino Uno" and then click the OK button.
Under the Tools menu, go to the Board sub-menu, and select "Arduino/Genuino Uno" in the Board.
Verify your app code by clicking the Verify icon (looks like a checkmark) at the top of the code editor panel. (The Arduino code editor will first save your code before verifying it.)
After the verification is done, a message will appear in a status bar at the bottom of the code editor panel:
If your code compiled without any errors, the status bar will display a success message. (In the web editor, the message will say "Success." In the desktop editor, the message will say "Done compiling.")
If your code contains an error, the status bar will display an error message with a description of the error, and the code editor will highlight the specific line number in your code where the error was detected (though the root cause of the error usually occurs on a previous line). You'll want to fix the error and then try verifying your code again.
MULTIPLE ERRORS: Sometimes your app code might contain multiple errors. However, the Arduino code editor will stop verifying the code at the first error that is detected. Once you fix that error and verify the code again, you might see a new error message for another error that occurs later in the code.
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 (filename: RedBot.h
) that makes it much easier to control the motors and sensors connected to your RedBot. You will need to add a copy of this 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.
OTHER LIBRARIES: You can follow the same steps below to add other Arduino code libraries (such as the OneButton library, etc.) to your code editor, and then include the library in an app.
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.).
The RedBot.h
library defines the following classes:
RedBotButton
class — used to control the built-in D12 button
RedBotMotors
class — used to control the left and right motors
RedBotBumper
class — used to control the left and right mechanical bumpers
RedBotSensor
class — used to control analog sensors, such as the IR line sensors
RedBotEncoder
class — used to control the left and right wheel encoders
RedBotAccel
class — used to control the accelerometer
You must add a copy of the RedBot.h
library to your code editor. This is a one-time process.
If you're using the Arduino Create web editor, you should add the SparkFun RedBot library to your "Favorites" tab in the Libraries menu.
You only need to do this once, and then you'll be able to quickly and easily include a copy of the RedBot.h
library in each of your robot apps.
Login to the Arduino Create web editor.
Click Libraries in the navigation menu on the left.
Click the Library Manager button at the top of the middle panel. This will allow you to search the libraries contributed by Arduino community members.
In the pop-up, type redbot
into the "Search Library" field, and press enter.
In the search results, click the star icon to the right of "SparkFun RedBot Library" to add this library to your Favorites. Then click the Done button to close the pop-up.
If you're using the desktop version of the Arduino IDE code editor, you need to download and install the SparkFun RedBot library on your computer, which will add it to your list of libraries in the Sketch menu.
You only need to do this once, and then you'll be able to quickly and easily include a copy of the RedBot.h
library in each of your robot apps.
Open the Arduino IDE application on your computer.
Under the Sketch menu, select "Include Library" and then select "Manage Libraries" in the sub-menu.
A pop-up will appear. It will list all the Arduino libraries available for downloading. (If you have a slower Internet connection, it make take a few seconds for the full list to populate). Type redbot
into the search field at the top-right, and press enter.
In the search results, select the most recent version of the "SparkFun RedBot Library" and then click the Install button.
After the library has downloaded and installed, click the Close button to close the pop-up.
You must include a copy of the RedBot.h
library in each of your robot apps.
Create a new app, or open an existing app.
If necessary, click the Libraries menu in the left navigation to show its options in the middle panel.
Click the Favorites tab in the middle panel. Hover your mouse cursor over "SparkFun RedBot Library" and click the Include button that appears.
Create a new app, or open an existing app.
Under the Sketch menu, select "Include Library" and then select "SparkFun RedBot Library" in the sub-menu (the library will be listed toward the bottom under Contributed Libraries).
The following #include
statements will be automatically inserted at the beginning of your app code:
The #include
statements shown above actually add two RedBot libraries to your program:
The first library (RedBot.h
) is the main RedBot library, which is what you need.
The second library is the RedBot Software Serial library, which you do not need.
You should delete the second #include
statement for the RedBot Software Serial library. This library is only used for the XBee Wireless Antenna module (which is not included in a standard RedBot kit – and will not be used for this project).
Arduino has a built-in Serial
class that can be used to send serial data from your robot's sensors to your computer for viewing in the serial monitor (so you don't need the RedBot Software Serial library).
Uploading an app to your robot from your code editor requires several steps:
Connect Robot to Computer
Turn on Robot Power
Select Correct Board and Port
Upload App to Robot
If you haven't already done so, open the Arduino code editor on your computer.
Your RedBot kit should have a USB to Mini-USB cable that allows you to connect the robot to a computer, in order to update the robot's app (or to send serial data to the computer).
Carefully plug the small end of the cable into the Mini-USB port on your RedBot circuit board. Plug the other end of the cable into a USB port on your computer.
IMPORTANT: Stand the RedBot upright on its back end (so its wheels are in the air). This is a precaution to make sure your robot doesn't drive away while connected to your computer.
Your RedBot is powered by a battery pack containing 4 AA batteries. Be sure the battery pack cable is plugged into the barrel jack on your RedBot circuit board.
Slide the RedBot's Power switch to ON. The RedBot's green Power LED light should turn on.
STOP MOTORS: If your robot's wheels start spinning when powered on (because the robot is running an existing app), you can temporarily slide the Motor switch to STOP if desired.
NO POWER: If the robot's Power LED doesn't turn on, verify the battery pack cable is plugged in and the Power switch is set to ON. Next, try replacing the AA batteries in the battery pack.
In order to upload your app to your robot, the code editor must know which type of Arduino board your robot has and which USB port on your computer that the robot is connected to.
If you previously selected your Arduino board type (which should be "Arduino/Genuino Uno"), the code editor should remember this selection.
Click "Select Other Board & Port" in the drop down menu at the top of the code editor panel.
In the pop-up, verify that "Arduino/Genuino Uno" is selected as the board, and then select the correct USB port that your robot is connected to. Finally, click the OK button.
On Mac, the correct port should include "usbserial" as part of its name.
On Windows, there should be one or more numbered COM ports listed. You may have to select one, try uploading your app — and then if the app won't upload, switch to another COM port instead until you identify the correct port.
Under the Tools menu, verify that "Arduino/Genuino" is selected in the Board sub-menu, and then select the correct USB port in the Port sub-menu:
On Mac, the correct port should include "usbserial" as part of its name.
On Windows, there should be one or more numbered COM ports listed. You may have to select one, try uploading your app — and then if the app won't upload, switch to another COM port instead until you identify the correct port.
Once you've selected the correct port, the code editor should remember this selection while you keep the code editor open. However, if you close the code editor, you'll have to select the correct port again the next time you open and use the code editor.
Click the Upload icon (looks like a right arrow) at the top of the code editor panel. The code editor will automatically save and verify the app before uploading it to your robot.
During the upload process, you may notice two green LED lights (labeled TX and RX, located next to the Mini USB port) blinking rapidly as the app code is transferred to the robot.
Once the upload is complete, the new app will immediately start running on your robot.
RUN MOTORS: If you used the Motor switch to temporarily stop the motors from running, you will need to slide the switch to RUN to allow the robot to drive around.
If necessary, you can press the Reset button on the robot's circuit board to restart the app.
Confirm the app works as you intended. If the robot doesn't do what you expected, you'll have to modify your app code, and then upload the modified app to your robot.
To stop the robot from running its app, slide the RedBot's Power switch to OFF.
UPLOAD ERROR: If the Arduino code editor indicates there was a problem uploading to the board, it most likely means that the correct port was not selected. Be sure the correct USB port is selected in the code editor.
ONE APP AT A TIME: Arduino devices, such as the RedBot, can only store and run one app at a time. If you want to change the app running on your robot, you have to upload a different app to your robot from your code editor.
The Arduino code editor has a serial monitor which can be used to view data sent by your robot over a USB connection to your computer.
Connect your robot to your computer using the USB cable. If necessary, upload your app to your robot.
Two important reminders:
If your app will be driving the robot's motors, be sure the robot is standing upright on its back end (so its wheel are in the air) to prevent your robot from driving away while it's connected to your computer. (Another option is that you could temporarily set the Motor switch to STOP.)
Do not unplug the USB cable. You'll need to keep the robot connected to your computer, in order to allow and view the serial data communication.
Click the Monitor menu link in the left navigation to display the serial monitor in the middle panel.
When you are finished viewing the serial data, you can click the Disconnect button.
Under the Tools menu, select "Serial Monitor." A new window will appear displaying the serial monitor.
When you are finished viewing the serial data, you can close the serial monitor window.
If necessary, you can obtain a copy of your app code (as .ino
file).
You can view the file (using a text editor), copy the file, print it, upload it (to submit to a teacher), etc.
All of your sketches (apps/programs) are saved in the cloud and can be easily downloaded.
If necessary, log in to the web editor.
If necessary, click the Sketchbook menu link in the left navigation panel to display the Sketchbook menu options in the middle panel.
All of your saved sketches are listed in the middle panel. If you have lots of saved sketches, you can order the list by name or by last modified date. You can also search by name.
Once you find the sketch you want to download, you can click the drop-down arrow next to the sketch name, and in the pop-up menu, select "Download Sketch."
Alternatively, you can click on the sketch's name to load the sketch into the code editor panel. Then click the 3-dot button at the top of the code editor panel, and select "Download Sketch."
The sketch will be downloaded as a ZIP file (compressed file). After the download is complete, locate the ZIP file in the Downloads folder on your computer, and uncompress the ZIP file:
On a Windows computer, right-click the ZIP file, and select "Extract All." Browse to the destination where you want to save the uncompressed file folder, and click "Extract."
On a Mac computer, just double-click the ZIP file. The uncompressed file folder will appear in your Downloads folder.
The uncompressed sketch will be contained inside a file folder with the same name as your sketch. Open the sketch's file folder. The sketch's filename will have an extension of .ino
(which identifies it as an Arduino program). For example: hello_world.ino
The folder will also contain a file named ReadMe.adoc
and another file named sketch.json
. You don't need to submit those two files to your teacher.
All of your sketches (apps/programs) are saved on your computer and can be easily located.
Open the Documents folder on your computer, and locate the folder named Arduino (which was automatically created when you first installed the Arduino IDE desktop editor).
Inside the Arduino folder, there is a subfolder for each of your sketches. (There will also be a folder named libraries that contains any libraries that you installed, such as the SparkFun RedBot Library.)
Open the subfolder for the sketch that you need. The sketch's filename will have an extension of .ino
(which identifies it as an Arduino program). For example: hello_world.ino
Here is an app template that can be used as "starter" code to program your team's robot demonstration.
This app template has been designed to demonstrate 3 task scenarios:
The loop()
function checks to see if the button has been pressed to "start" the robot.
If the robot is "started", it performs the next task (represented by a number) by calling a custom function for that task, such as task1()
, task2()
, etc.
At the end of each task, the robot is "paused," and the next task number is changed (so the robot will perform the tasks in order and start over once it reaches the last task).
Pressing the button again will start the next task.
If necessary, you can easily modify the app code to demonstrate more (or fewer) tasks.
You'll need to add other code to complete your app:
You may need to add code to declare other global variables or create other objects. For example, if your robot will use the IR line sensors, you'll need to add code to create an object for each line sensor.
You may need to add code within the setup()
function. For example, if your robot will use an ultrasonic sensor, you'll need to add code to set the pin modes for the ultrasonic sensor's transmitter and receiver pins.
You may need to modify the code within the loop()
function if your robot will demonstrate more than 3 tasks (or fewer). Otherwise, this code should work as is.
You'll need to add code within the task1()
function, task2()
function, and task3()
function to perform your specific task scenarios. If necessary, you can include a for a task to perform behaviors that must be continuously called (such as: avoiding line, etc.) and stop them after a set duration.
You will need to add other custom functions depending on the navigation mode(s) and other behaviors that your robot will use to complete its tasks. For example, you'll most likely need to add the driveDistance()
and pivotAngle()
functions — as well other custom functions.
You may want or need to create some of your own custom functions to perform specific actions or decisions. This is especially helpful if the same set of actions will be performed multiple times within your robot tasks.
APP TEMPLATE FOR ROBOT DEMO
ONE BUTTON LIBRARY: If desired, you could modify this app template to , so your robot will perform its different tasks based on different types of button presses (single-press, double-press, or long-press).