Page 82 · Robotics with the Boe-Bot
FOR...NEXT to Control Servo Run Time
Hopefully, by now you fully understand that pulse width controls the speed and direction
of a Parallax Continuous Rotation servo. It's a pretty simple way to control motor speed
and direction. There is also a simple way to control the amount of time a motor runs, and
that's with a
loop.
FOR...NEXT
Here is an example of a
FOR counter = 1 TO 100
PULSOUT 13, 850
PAUSE 20
NEXT
Let's figure out the exact length of time this code would cause the servo to turn. Each
time through the loop, the
for 20 ms, and it takes around 1.3 ms for the loop to execute.
One time through the loop = 1.7 ms + 20 ms + 1.3 ms = 23.0 ms.
Since the loop executes 100 times, that's 23.0 ms times 100.
=
×
time
100
23
0 .
ms
=
×
100
. 0
0230
=
. 2
30
s
Let's say you want the servo to run for 4.6 seconds. Your
execute twice as many times:
FOR counter = 1 TO 200
PULSOUT 13, 850
PAUSE 20
NEXT
Example Program: ControlServoRunTimes.bs2
√
Enter, save, and run ControlServoRunTimes.bs2.
√
Verify that the P13 servo turns counterclockwise for about 2.3 seconds, followed
by the P12 servo turning for twice as long
' Robotics with the Boe-Bot - ControlServoRunTimes.bs2
' Run the P13 servo at full speed counterclockwise for 2.3 s, then
' run the P12 servo for twice as long.
loop that will make the servo turn for a few seconds:
FOR...NEXT
command lasts for 1.7 ms, the
PULSOUT
s
command lasts
PAUSE
loop will have to
FOR...NEXT
Need help?
Do you have a question about the Boe-Bot and is the answer not in the manual?
Questions and answers