Listing 3: PropBot_Navigate.
int main() { // main function
int turret = 14; // Standard servo connected to P14
int dlay = 2000; // Wait delay: 2 seconds
while(1) {
servo_angle(turret, 900); // Servo to center
pause(dlay); // Wait for delay period
servo_angle(turret, 0); // Servo turn clockwise
pause(dlay); // Wait
servo_angle(turret, 1800); // Turn counter-clockwise
pause(dlay); // Wait
}
}
#include "simpletools.h"
#include "ping.h"
#include "servo.h"
Figure 6.
Connection of the
turret sensor into the
P14 header along the
top of the Propeller
Activity board.
int main() {
int turretPin = 14;
int pingPin = 15;
int dlay = 2000;
while(1) {
servo_angle(turretPin, 900);
doPing(pingPin,dlay);
servo_angle(turretPin, 0);
doPing(pingPin, dlay);
servo_angle(turretPin, 1800);
doPing(pingPin, dlay);
}
}
int doPing(int pinNum, int waitDelay) {
pause(750);
int Dist = ping_inches(pinNum);
print("Dist = %d\n", Dist);
pause(waitDelay);
return Dist;
“settling” time to allow the servo to cease any jittery
movement once stopped. Note that I’ve used user-defined
variables to set both the I/O pin connecting to the servo
and the delay period. This is good practice when your
programs refer to the same data multiple times. If a change
needs to be made, you only have to update the variable
definition rather than every separate instance of the data
throughout the program. That is, instead of:
any additional extensions or wiring. Note the configuration
of the jumper next to the P14/P15 connectors. To operate
the turret servo at the full 6V of the PropBot’s battery pack,
be sure to move the jumper from Vin to 5V.
Listing 2: PropBot_Test TurretServo shows a demo
program for rotating the turret forward, fully right, and fully
left. Some wait delays are included to allow time for the
servo to transition to its new position. At 6V, these
standard servos move 60 degrees in about 250
milliseconds. The two second delay in the test program is
for demonstration purposes only; in actual practice, you
want to limit the delay to only what’s needed to transit the
servo to its new position, along with a slight amount of
servo_angle(14, 900);
to rotate the turret on P14 to the center, you can use:
int turret = 14;
so whenever you want to change the position of the turret,
use the variable name instead, as in:
servo_angle(turret, 900);
Listing 2: PropBot_TestTurretServo.
#include "simpletools.h" // Include simpletools library
#include "servo.h" // Include servo library
This moves the servo
connected to pin P14 to
position 900 (which means
centered). As noted elsewhere
in the PropBot_Test TurretServo
program, the values 0 and 1800
move the servo fully clockwise
and counter-clockwise,
respectively. Listing 3:
PropBot_TurretTurnAndPing
demonstrates combining both
the Ping and the turret servo
functions, rotating the servo
center, right, and left, and
34 SERVO 12.2014