# Using RobotC, trying to find why code seems to loop oddly

I am writing code in RobotC, (natural language PLTW) and what I want is this robot to go forward, then if it "sees" an obstacle, to reverse, turning on an LED (sort of an indicator light) until the obstacle is far enough away and then pick a random direction to try going forward again, lighting another LED (again, an indicator).

I had the code sort of working before, and I feel like I messed it up somehow. This is what I have so far. What's happening is as I run the debugger I can see the code looping back to the first while statement, but the wheels aren't even moving.

It was suggested I use state machines but I am new to RobotC and am not quite clear on how that would get set up.

In any case this is the code I have. Any assistance would be appreciated; I feel like there is some elementary error I am making. (Honestly I always have trouble with code that doesn't allow anything like the old GOTO statements from BASIC all those years back).

#pragma config(Sensor, dgtl3,  bumpSwitch,     sensorTouch)
#pragma config(Sensor, dgtl4,  greenlight,     sensorLEDtoVCC)
#pragma config(Sensor, dgtl5,  yellowlight,    sensorLEDtoVCC)
#pragma config(Sensor, dgtl6,  redlight,       sensorLEDtoVCC)
#pragma config(Sensor, dgtl9,  ultrasonic,     sensorSONAR_cm)
#pragma config(Motor,  port1,           leftMotor,     tmotorVex393_HBridge, openLoop)
#pragma config(Motor,  port10,          rightMotor,    tmotorVex393_HBridge, openLoop)
//*!!Code automatically generated by 'ROBOTC' configuration wizard               !!*//

{int direction; //introduce variable "direction"
direction = 0; // set the variable to zero
repeat(forever)

{turnLEDOff(dgtl4);
turnLEDOff(dgtl5);
turnLEDOff(dgtl6);
stopMotor(leftMotor);
stopMotor(rightMotor);
while (true)
{if (SensorValue(ultrasonic)>25)
{turnLEDOn(dgtl6);
startMotor(leftMotor,60); //start moving forward
startMotor(rightMotor,60);}
else
{stopMotor(rightMotor);
stopMotor(leftMotor);
turnLEDOn(dgtl4);
while(SensorValue(ultrasonic)<25)
{
startMotor(leftMotor,-:);
startMotor(rightMotor,-20);
}

direction = random(2);
if (direction == 0)
{turnLEDOn(dgtl5)
stopMotor(leftMotor);
stopMotor(rightMotor);
startMotor(leftMotor,-10);
startMotor(rightMotor,10);
wait(1.5);}
else
{
stopMotor(leftMotor);
stopMotor(rightMotor);
startMotor(leftMotor,10);
startMotor(rightMotor,-10);
wait(1.5);}}
turnLEDOff(dgtl4);
startMotor(leftMotor,60);
startMotor(rightMotor,60);

}}}

• What does the “-:” mean in your line “startMotor(leftMotor,-:);”? Nov 5 '19 at 3:59
• negative means that the motor is going in the other direction. I just noticed that one part should be a number and not a colon, so I will fix that and see if that changes things. Nov 5 '19 at 10:09