Page 138 ·
Robotics with the Boe-Bot
pulseCount VAR Word
FOR pulseCount = 1 TO 100
PULSOUT 13, 750 + pulseCount
PULSOUT 12, 750 - pulseCount
PAUSE 20
NEXT
Figure 4-4
Ramping
Example
Recall from Chapter 2, Activity #5 that
FOR…NEXT
loops can also count downward from a
higher number to a lower number. You can use this to ramp the speed back down again
by using
FOR
pulseCount = 100 TO 1
. Here is an example program that uses
FOR…NEXT
loops to ramp up to full speed, then ramp back down.
Example Program: StartAndStopWithRamping.bs2
√
Enter, save, and run StartAndStopWithRamping.bs2.
√
Verify that the Boe-Bot gradually accelerates to full speed, maintains full speed
for a while, and then gradually decelerates to a full stop.
' -----[ Title ]--------------------------------------------------------------
' Robotics with the Boe-Bot - StartAndStopWithRamping.bs2
' Ramp up, go forward, ramp down.
' {$STAMP BS2}
' {$PBASIC 2.5}
DEBUG "Program Running!"
pulseCount VAR Word ' FOR...NEXT loop counter.
' -----[ Initialization ]----------------------------------------------------
FREQOUT 4, 2000, 3000 ' Signal program start/reset.
' -----[ Main Routine ]-------------------------------------------------------
' Ramp up forward.
FOR pulseCount = 1 TO 100 ' Loop ramps up for 100 pulses.
PULSOUT 13, 750 + pulseCount ' Pulse = 1.5 ms + pulseCount.
PULSOUT 12, 750 - pulseCount ' Pulse = 1.5 ms – pulseCount.
PAUSE 20 ' Pause for 20 ms.
1, 2, 3,
…100