Infinite loop not working

Hi,
For my school, we are working on a project that uses an Ultrasonic sensor. Our goal is to have the robot move until an object gets within 20 inches of the sensor. Being fairly near to ROBOTC programming, I have got the robot to move until the sensor detects an object. The problem now is that we want the robot to start moving again once the object moves out of the sensor range. What can I do?

-SScrub

Here is the code

#pragma config(Sensor, dgtl5, sonarSensor, sensorSONAR_inch)
#pragma config(Motor, port2, leftDrive, tmotorVex393_MC29, openLoop, driveLeft)
#pragma config(Motor, port3, rightDrive, tmotorVex393_MC29, openLoop, driveRight)
//!!Code automatically generated by ‘ROBOTC’ configuration wizard !!//

task main()
{

wait1Msec(2000); // Robot waits for 2000 milliseconds before executing program

while(SensorValue(sonarSensor) > 20 || SensorValue(sonarSensor) == -1) // Loop while robot’s Ultrasonic sensor is further than 20 inches away from an object
{ // || (or) it is ‘-1’. (-1 is the value returned when nothing is in it’s visable range)
motor[rightDrive] = 63; // Motor on port2 is run at half (63) power forward
motor[leftDrive] = 63; // Motor on port3 is run at half (63) power forward}
}
}

You can put that loop inside of a while(true) loop in order for it to be constantly checking the inner program to see if something is within range.

In your code, once the while loop is satisfied the program is finished and the sensor comparison will no longer be evaluated.

Wrapping the code within a


while

loop will not fix your issue because the motors are never instructed to stop. The only reason that they stop now is because the program finishes.
I am not a fan of


while

loops other than being the outermost function in the main task. They tend to cause trouble if not properly handled. I prefer using


if

which evaluates the sensor but then allows other functions to be processed regardless of the result. It doesn’t make too much of a difference in this example but I think it’s a better practice.

The code below will repeatedly evaluate the sensor. If the sensor result meets your criteria (over 20 inches away or no result found) it will run the motors. If it doesn’t meet the criteria it tells the motors to stop.


#pragma config(Sensor, dgtl5, sonarSensor, sensorSONAR_inch)
#pragma config(Motor, port2, leftDrive, tmotorVex393_MC29, openLoop, driveLeft)
#pragma config(Motor, port3, rightDrive, tmotorVex393_MC29, openLoop, driveRight)
//*!!Code automatically generated by 'ROBOTC' configuration wizard !!*//

task main()
{
	wait1Msec(2000);	// Robot waits for 2000 milliseconds before executing program
	while(1) {
		//If robot's Ultrasonic sensor is further than 20 inches away from an object
		// || (or) it is '-1'. (-1 is the value returned when nothing is in it's visable range)
		if(SensorValue(sonarSensor) > 20 || SensorValue(sonarSensor) == -1)	
			{ 
				//Power motors
				motor[rightDrive] = 63;	// Motor on port2 is run at half (63) power forward
				motor[leftDrive] = 63;	// Motor on port3 is run at half (63) power forward
			}
			else {
				//Stop motors
				motor[rightDrive] = 0;	// Motor on port2 is stopped
				motor[leftDrive] = 0;	// Motor on port3 is stopped
			}
	}
}

@Pete thanks for the help