Software and Programming

Ease of use was one of my main priorities in the design of this robot. I wanted students to be able to get to the point where they could be creative with this robot as soon as possible without spending a large amount of time learning technical details. Software plays a major role in ease of use because many of the students who could benefit from this type of kit may have never programmed a computer before. For this reason, I chose a microcontroller that could be used with the Arduino software. Arduino is a microcontroller programming environment which is very easy to use because of a number of built-in functions, and it has a broad following on the internet that provides support. If you want to know how to do something in Arduino, the answer is usually just a Google™ search away.

Since there are so many internet resources that can teach you how to use Arduino, I will provide links to these excellent references instead of reinventing the wheel.  For example, click here to access a great tutorial on how to begin programming in Arduino.  This and other links are also accessible in the Reference section of this website.

One important point needs to be made. In order to keep system cost low, I chose to run the microprocessor with its internal clock instead of using an external source like the Arduino boards do. This is a somewhat small technical detail, but it will be important for you when it comes time to program your board. In early testing I wasn’t able to use the most recent version of Arduino (version 1.0). I used Arduino 0021 which, while not supported officially, can still be downloaded from the Arduino website. You will need to follow the link below to learn how to burn the Arduino bootloader onto your ATmega 328 chip. While this process may take some time to get used to, once you understand it, you will be able to program many chips relatively quickly.

How to Program Your Robot

Now onto the code. It is here that Arduino really shines. Below is the code, which I have broken down to be a little easier to digest. Don’t worry about retyping all of this code, it is available in the References tab.

These variables tell the program which pins to use for each board function. The names in the first section may seem somewhat cryptic, but they correspond to the labels in the motor driver datasheet. The variables L and R correspond to the makeshift switches on the front of the robot. If you’ve followed my design, you won’t need to change any of these, but I assigned them to variables so that it would be easy to make changes if needed.

This section initializes all of the pins that will be used in this program. The motor driver pins are set as outputs, while the switches are used as inputs with pull-up resistors.

This section of code is from the main body of our program. Everything inside of void loop() will run repeatedly until the robot is turned off. The main code is made up of three conditional statements. If either of the switches is pressed, the robot will back up and turn away from the obstacle. Two variables, tBack and tTurn, control the number of milliseconds each action takes place. This block of code defines what happens when the left antenna is pressed. Note that digitalWrite(EN#,HIGH) turns the motors on while digitalWrite(a#,HIGH) or digitalWrite(a#,LOW) controls the direction of the motors. These commands turn on or off the pins on the motor driver. More details about why these pins need to be switched on and off in this way can be found in the datasheet for the motor driver.

This code defines what happens if the right antenna is pressed or if neither antenna is pressed.

That’s all there is to it, just 72 lines of code to make a robot sense its environment and respond to it. The code may seem confusing at first, but take your time, and search on the internet if there is anything you are unfamiliar with. I have found that the examples built into Arduino are an excellent resource to learn from.

How to Upload the Program to Your Robot

Now it’s time to upload the program we just wrote on to your robot.

First, attach the USB to serial converter to your robot as shown above. Make sure that the top of the board is pointing up, as shown in the picture. Plug your USB cable into the board and into the computer.

Under Tools/Board, select ATmega328 on a breadboard as shown above.

Under Tools/Serial Port, select the port number of your USB to serial converter. It is probably the highest-number port listed, but will probably not be COM16 like shown above.

Last, turn the battery box on to program the robot. Make sure the robot doesn’t drive off the table when you turn it on. Click the upload button shown above to load your code on to the robot.

That’s it! You should be ready to start experimenting with different programs.

Advertisements