Can't see what's wrong in making robot stop before hits obstacle

I have the following code:

#pragma config(Sensor, dgtl1, eyes1, sensorSONAR_cm)
#pragma config(Sensor, dgtl4, greenlite, sensorLEDtoVCC)
#pragma config(Sensor, dgtl8, eyes2, 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 !!//

task main()
{
stopMotor(leftMotor);
turnLEDOn(dgtl4);

startMotor(leftMotor, 127);
startMotor(rightMotor, 127);

untilSonarLessThan(10, dgtl8);

turnLEDOff(dgtl4);
stopMotor(leftMotor);
stopMotor(rightMotor);
}

What is supposed to happen is the robot goes forward, until the ultrasonic sensor sees something 10 cm or less in front of it, then it should stop. I ran the code, and the light goes on the way it should and the robot goes forward. But it seems to not “read” the sensor. I checked the connections and the sensor seems ok too.

Running the debugger I sense something is amiss, but I am not sure what (Order of commands here?) so any help would be appreciated. I want eventually to code up the 'bot to go forward, see an obstacle, stop, turn in a random direction (left or right about 45 degrees) and then go forward again, rinse, repeat.

Anyhow any assistance is much appreciated, I feel that I must be missing something relatively simple. I have tried some If - then loops before, and that didn’t seem to quite work but again I may have structured them badly.

thanks

You seen to have two ultra sonic sensors set up at Port 1 and Port 8. But the program only refers to Port 8. That seems like it could be the problem. :+1:

3 Likes

So I should have something in there that refers to port 1, even though for the moment I wasn’t using that sensor? Seems odd, but I will give it s shot.