Concept-based Lesson: Waddle Bot Unit 1 Lesson 4

Printer-friendly version
Objectives: 

Repetition is key when creating long lasting programs. With the addition of "logic traps", we can pause NXT action until a certain condition is met.

  • Use loops to "wait for a condition".
  • Shorten a program by repeating sections of code. 
Lesson: 

One of the problems with a simple IF statement is that it happens in only once. There is not an easy way for the NXT to adjust a wait timer depending on where it is on the board. With while loops, that becomes possible. Let's look at a simple while loop.


  1. #pragma config(Sensor, S3, Light, sensorLightActive)
  2. #pragma config(Sensor, S4, Sonar, sensorSONAR)
  3. #pragma config(Motor,  motorA, Left, tmotorNormal, PIDControl, encoder)
  4. #pragma config(Motor,  motorC, Right,  tmotorNormal, PIDControl, encoder)
  5. //*!!Code automatically generated by 'ROBOTC' configuration wizard!!*//
  6.  
  7. // Unit 1 - Lesson 4 - While Loops
  8. int threshold = 40;
  9. int distance = 10;
  10. int motospeed = 50;
  11.  
  12. task main
  13. {
  14.   //move forward
  15.   motor[Left] = motospeed;
  16.   motor[Right] = motospeed;
  17.  
  18.   //Logic Trap to wait for line detection.
  19.   while(SensorValue[Light] > threshold && SensorValue[Sonar] > distance)
  20.   {
  21.     //Idle system
  22.     nxtDisplayString(1,"Waiting for Line");
  23.   }
  24.  
  25.   //Stop motors
  26.   motor[Left] = 0;
  27.   motor[Right] = 0;
  28.   eraseDisplay();
  29.   nxtDisplayString(1,"All Stop");
  30.   wait1Msec(3000);
  31. }

while (condition) {}

Printer-friendly version

 

A while loops acts in a very similar manner to an IF statement. Where an IF statement would run the attached code block only once, an IF loop will run the same code multiple times. As long as the conditional statement evaluates to TRUE as the start of the loop, the NXT remains in the loop. If isn't until the conditional statement evaluates to FALSE will it skip the code block and move onto the next part of the program.

The timing of this evaluation is important. A long loop can take a number of seconds to complete and sensor values may fluctuate while the loop block is processed. The conditional statement is only evaluated at the start of the loop. If a sensor value changes after the logical loop, it will only affect the while loop once the entire block of command have been completed.

Wait for Condition

Printer-friendly version

One of the most common ways to use a while loop is as a wait command. This is better than a simple wait1sec command because you can wait for almost any condition to be met. In our sample program, we use a while loop to wait for the NXT to cross a line. We start by moving the NXT forward. We then trap the NXT in a while loop until a line is detected. We should leave this section blank, but it is generally a good idea to give yourself some feedback when you wait for a condition. Once the light sensor detects a line, the NXT will leave the while loop and run into our stop commands.

Logical Expressions

Printer-friendly version

 

As long as the NXT is on a white board, it will run continuously until it runs into the first black line. While this is going on, the NXT can't do anything but what is included in the while code block. Let's say that we setup a wall around our board. We don't want the NXT to crash into the wall so we need to add a second condition to the while loop. The NXT should leave the while loop if it gets too close to a wall. This would result in the NXT stopping.

To have a conditional statement that has two conditions, we need to use a logical statement like "AND". This logical statement will return a TRUE only when both conditions are TRUE. we use the double ampersand "&&" to designate an AND statement. By separating the two conditions with an double ampersand, we change the behavior of the NXT. Now if a line or a wall is detected, the NXT will stop.

Infinite Loops

Printer-friendly version

 

The while command can only accept a single Boolean value. Whatever is inside the conditional statement will always be evaluated to a Boolean. If we want to force a while block to loop continuously, we can simply put a boolean into the while statement. Doing this with a TRUE value will cause something called in infinite loop. Since there is not a conditional statement to change the evaluation to FALSE, the code block will loop continuously. The while command would look something like this:

InfiniteLoop

Incrementing Integers/Counters

Printer-friendly version

 

Sometimes, you will want to be able to count how many times you have gone through a loop. Using a while loop and an integer variable, you can add this functionality. Without the ability to control the number of times a loop occurs, you would be forced to write out all of the commands each time you want to repeat. This leads to a needlessly long program and makes you a less effective programmer. The while loop is constructed like this:

LoopCounter

The key variable in this is the "counter" integer. Each time we complete a loop we add one to the counter variable. Once the NXT has gone through this loop 3 times, the counter will equal 3 which is not less than our limit. This will cause the conditional statement to evaluate as FALSE and exit the loop. The double plus sign at the end of the variable name is shorthand for add 1 to this integer. It is the same as the following:

  • counter = counter + 1 ;