• The program starts with a series of #include
statements that use several pre-defined libraries that
come with the SimpleIDE software. These libraries
make C language coding on the PAB much easier.
Important! In this version of the PropBot, “front” and
“back” have been swapped. The leaf switches described in
earlier parts of this series defined the “front” of the robot.
• Next are some variable definitions for I/O pins.
• The main program contains the main block of
executable code. It begins — as demonstrated in
earlier installments — with a three-tone song that
signals to us things have loaded up and are about to
begin. The while(1) statement block that follows
forms an endless loop that repeats indefinitely. In this
loop, the
We now want the Ping sensor to be in the front. To
accomplish this, the motor control routines are exchanged
so that the servos work in the reverse fashion of what they
did in earlier articles.
Enhancing the
Navigating Program
if (doPingSimple() <5)
With variations in code, you can have your PropBot do
all sorts of nifty things. Listing 5: PropBot_NavigateAnd
Ping Wander lets the PropBot seek out things to investigate
while checking for obstacles that are too close. This is a
statement fires the Ping
and waits for a response.
If the result is four or
less, the robot is too
close to an object in
front of it. If so, the
robot backs up, turns,
and heads in a new
direction.
Listing 5: PropBot_NavigateAndPingWander.
#include "simpletools.h" // Include simpletools lib
#include "servo.h" // Include servo lib
#include "ping.h" // Include ping lib
void watcher(); // Forward declaration for
// background timer
• The series of motor
control statements are
simple functions that
encapsulate the
commands for turning
the robot’s drive servos
in the proper direction.
int turretPin = 14; // Standard servo connected to P14
int pingPin = 15; // Ping connected to P15
int piezo = 4; // Piezo connected to P4
int minDistance = 5; // Minimum distance (inches) allowed
int watchDog; // Re-used variable for cog ID
int main() { // Main function
• Two user-defined
functions at the end —
doPing and
doPingSimple — fire the
Ping sensor and return
the result. The functions
differ in the control of
the turret servo.
// Sound at startup
freqout(piezo, 200, 500);
freqout(piezo, 200, 1000);
freqout(piezo, 200, 2000);
// Turret positioning values
int turretCenter = 900;
int turretRight = 400;
int turretLeft = 1400;
print ("Started\n"); // Send text to Terminal
watchDog = cog_run(&watcher, 10); // Run background timer in another
To try this program, select
position 1 on the PAB’s main
switch. This turns the Propeller
on, but doesn’t power the
servos.