FOR...NEXT question...
nightrider
Posts: 9
Hello all, someone help me understand this please.· I'm working through Robotic with the Boe-Bot manual and I'm on page 82.· Here is the exerpt that I do not understand.
Here is an example of a FOR…NEXT loop that will make the servo turn for a few seconds:
······· 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 PULSOUT command lasts for 1.7 ms, the PAUSE command lasts
for 20 ms, and it takes around 1.3 ms for the loop to execute.
One time throught the loop=1.7ms+20ms+1.3ms = 23.0ms
I don't understand the statements in red.· 1.3ms? what loop is the author talking about? The FOR-NEXT, loop? If that's the case, I don't understand how it can take 1.3ms to execute.
Can someone explain this to me?
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Thank you,
Alex Cordero
San Diego, Ca.
robots: AIBO ERS-220, Robosapien, Roomba, Boe-bot (basic), a bunch of other little robots and toys.
"Fill your boots, man!"
-Irish saying
Here is an example of a FOR…NEXT loop that will make the servo turn for a few seconds:
······· 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 PULSOUT command lasts for 1.7 ms, the PAUSE command lasts
for 20 ms, and it takes around 1.3 ms for the loop to execute.
One time throught the loop=1.7ms+20ms+1.3ms = 23.0ms
I don't understand the statements in red.· 1.3ms? what loop is the author talking about? The FOR-NEXT, loop? If that's the case, I don't understand how it can take 1.3ms to execute.
Can someone explain this to me?
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Thank you,
Alex Cordero
San Diego, Ca.
robots: AIBO ERS-220, Robosapien, Roomba, Boe-bot (basic), a bunch of other little robots and toys.
"Fill your boots, man!"
-Irish saying
Comments
The timing in question regards ONLY the instructions involved specifically with FOR and NEXT, and nothing else in between. NEXT begins the loop process. FOR either ends it, or continues it.
If you want more PBASIC instruction timing information, here is a resource:
http://www.emesystems.com/BS2speed.htm
Regards,
Bruce Bates
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
In other words, if you piled up instructions in your program between pulsouts to the servos, you would quickly exceed the reasonable limit of 20-30ms between pulses.
In the code above, you will get about 23ms between pulses, which is OK for most servos. The 20ms is a little flexible. If anything, driving the servos too often (<20ms between pulses) is more problematic, but intervals that are too long will underdrive the servos.
Longer and more complex programs that drive servos often do something like the following to be able to run all the code and still refresh the servos every 20ms or so, e.g.
- run task 1
- refresh servo
- run task 2
- refresh servo
- run task 3
- refresh servo
- run task 4
- refresh servo
- do it all again
As opposed to:
- run task 1
- run task 2
- run task 3
- run task 4
- refresh servo
- do it all again
If you want to "see" how slow instructions can be when they are piled up, hook up an LED that is flashed on then turned off instead of pulsing out to servos. With nothing else going on, the LED will blink so fast you can barely see it blink. As you pile instructions in between the blinks, you will start to see it blink slower and sloower and slooooower...
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
When the going gets weird, the weird turn pro. -- HST
"oh yea!·all things happen instantaneously... NOT!"
-- "I want it NOW!" -- geez...
*there's so much to learn... [noparse]:([/noparse]
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Thank you,
Alex Cordero
San Diego, Ca.
robots: AIBO ERS-220, Robosapien, Roomba, Boe-bot (basic), a bunch of other little robots and toys.
"Fill your boots, man!"
-Irish saying
Post Edited (nightrider) : 3/13/2007 7:35:31 PM GMT
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
- Stephen