User manual

ASURO - 61
-
C for ASURO
9.1.5. Loops
Loops are to be used to repeat command execution.
In a “while”-loop a condition is checked every time the loop is passed. If the condition is true the
command block will be executed. The condition is checked again until it turns to false. At a false
condition the program is continued at the  rst command following the condition block.
while( Condition)
Command block
Example:
#include “asuro.h”
int main(void) {
MotorDir(FWD,FWD); // Both engines running forward
MotorSpeed(120,120); // Both engines running at around half speed
StatusLED(GREEN); // Turn on Status-LED green
while (PollSwitch()==0) { // As long as there is no collision
SerWrite(“All OK!\n”,10); // ... Feeling groovy ....
}
MotorSpeed(0,0); // Collision! Stop immediatedly!
StatusLED(RED); // Turn on Status-LED red
while (1) {
SerWrite(“Ouch!\n”,5); // start crying!
}
}
A “for (expr1, epr2, expr3)”- sequence is equivalent to:
expr1;
while( expr2) {
Command block
expr3;
}
A “for”-sequence is often used as counter sequence.
for (i = 0; i < n; i++)
. . .
Example:
#include “asuro.h”
int main(void) {
int counter; // de ne a variable for counting
for (counter =0;counter <10;counter ++) { // repeat ten times:
SerWrite(“Go ahead!\n”,10); // “Go ahead” message
}
MotorDir(FWD,FWD); // Both engines forward
MotorSpeed(120,120); // Both engines running at around half speed
while (1) { // No more action!
}
}