Repeating Blocks of Code (updated 9/20/05 7:35pm) Reference NQC Tutorial pp 9-12
Making Turns You can make your robot turn by reversing the direction of one of the two motors. Here is an example. We have no way to how many degrees the robot will turn. We can try different wait times until we approximate (let’s say) 90 degrees. task main() { OnFwd(OUT_A+OUT_C); Wait(100); OnRev(OUT_C); Wait(85); Off(OUT_A+OUT_C); }
Let us now try to write a program that makes the robot drive in a square. Going in a square means: driving forwards, turning 90 degrees, driving forwards again, turning 90 degrees, etc. We could repeat the above piece of code four times but this can be done a lot easier with the repeat statement. #define MOVE_TIME 100 #define TURN_TIME 85 task main() { repeat(4) { OnFwd(OUT_A+OUT_C); Wait(MOVE_TIME); OnRev(OUT_C); Wait(TURN_TIME); } Off(OUT_A+OUT_C); }
What happens in this program? /* 10 SQUARES by Mark Overmars This program makes the robot run 10 squares */ #define MOVE_TIME 100 // Time for a straight move #define TURN_TIME 85 // Time for turning 90 deg. task main() { repeat(10) // Make 10 squares { repeat(4) { OnFwd(OUT_A+OUT_C); Wait(MOVE_TIME); OnRev(OUT_C); Wait(TURN_TIME); } Off(OUT_A+OUT_C); // Now turn the motors off }