6.b. Compiling a Simple Program

A very simple demo program for the 3pi is available in the folder examples\atmegaxx8\simple-test-3pi, using a few basic commands from the Pololu AVR Library. Here is a copy of the source code:

#include <pololu/3pi.h>

int main()
{
    print("Hello!");

    play("L16 ceg>c");

    while(1)
    {
        red_led(0);
        green_led(1);

        delay_ms(100);

        red_led(1);
        green_led(0);

        delay_ms(100);
    }

    return 0;
}

Navigate to the simple-test-3pi folder, double-click on the file test.aps, and the project should open automatically in AVR Studio, showing the C file. Make sure you select the version that is appropriate for the microcontroller on your 3pi: atmega328 if your serial number is 0J5840 or greater, else atmega168.

AVR Studio showing the 3pi sample program.

To compile this program, select Build > Build or press F7. Look for warnings and errors (indicated by yellow and red dots) in the output displayed below. If the program compiles successfully, the message “Build succeeded with 0 Warnings…” will appear at the end of the output, and a file test.hex will have been created in the examples\atmegaxx8\simple-test-3pi\default folder.

AVR Studio build window, compiling the example project.

Connect your programmer to your computer and to the ISP port of your 3pi, and turn on the 3pi’s power by pressing the button labeled POWER. If you are using the Pololu Orangutan Programmer, the green status LED close to the USB connector should be on, while the other two LEDs should be off, indicating that the programmer is ready.

Warning: Do not attempt to program your 3pi if its batteries are drained or uncharged (make sure you charge any new rechargeable batteries fully before you first use them). Losing power during programming could permanently disable your 3pi.

Note: Your programmer must be installed correctly before you use it. If you are using the Orangutan USB programmer, please see its user’s guide for installation instructions.

Pololu 3pi robot with an Orangutan USB programmer connected to its ISP port.

Select Tools > Program AVR > Connect to connect to the programmer. For the Orangutan Programmer, the default options of “STK500 or AVRISP” and “Auto” should be fine, so click Connect and the AVRISP programming window should appear.

You will use AVRISP to load test.hex into the flash memory of your 3pi. To do this, click “...” in the Flash section and select file test.hex that was compiled earlier. Note that you have to first navigate to your project directory! Now click “Program” in the Flash section, and the test code should be loaded onto your 3pi.

Programming the 3pi from AVR Studio.

If your 3pi was successfully programmed, you should hear a short tune, see the message “Hello!” on the LCD, and the LEDs on the board should blink. If you hear the tune and see the lights flashing, but nothing appears on the LCD, make sure that the LCD is correctly plugged in to the 3pi, and try adjusting the contrast using the small potentiometer on the underside of the 3pi, closest to the ball caster.