# Dual Sonar Turn with Robotc

We are working on a project that requires us to use two sonar sensors to avoid obstacles and move within the classroom. However, both sensors do not seem to work at the same time… here what we want to do:
//start moving forward
{
wait1Msec(2000); // Robot waits for 2000 milliseconds before executing program

if(SensorValue(sonarSensorFront) > 20 || SensorValue(sonarSensorFront) == -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 visible range)
motor[rightMotor] = 63; // Motor on port2 is run at half (63) power forward
motor[leftMotor] = 63; // Motor on port3 is run at half (63) power forward
}
//stop for 1 second
else
{
wait10Msec(1000);
}
else // turn right
{
motor[rightMotor] = -63; // Motor on port2 is run at half (63) power forward
motor[leftMotor] = 63; // Motor on port3 is run at half (63) power forward
wait1Msec(5000);
}

//keep on moving

if(SensorValue(sonarSensorFront) < 20)  //|| SensorValue(sonarSensorFront) == -1)		//  Ultrasonic sensor is more than 20 inches away from an object
{
motor[rightMotor] = 63;			// Motor on port2 is run at half (63) power forward
motor[leftMotor]  = 63;			// Motor on port3 is run at half (63) power forward
}

We think we should use a while look to make this program work instead of starting with an if statement, but we are not sure… we also want the back sensor to run the same way as the front sensor.

What ports are these on? There is a weird situation where ports 4 & 10 share an interrupt so don’t plug in untrasonics or quad encoders in both of those ports.

Also, ultrasonics need to have a nice perpendicular surface to bounce the sound waves back properly. So turning with ultrasonics does not always yield good results. Hook up the robot to the debugger and manually turn it to see what is going on. Then do it while driving and note any differences.

https://vexforum.com/t/dual-ultrasonic-sensors-with-robotc/23451/1

You want to use If’s, If you use a while loop for each they definitely will not work at the same time.

You shouldn’t have that “and” in there because both of those statements are essentially doing the same function. You also don’t need that first else statement because that may overwrite the second statement.

That is an OR statement?

OOPS! Sorry, I was trying to reply from a phone and was in a little bit of a rush. Disregard that part then. I was wondering why he was writing an and statement with || instead of &&

While loops are funky when working with sensors.

What I would do is put all of that code into a while loop.

I’m assuming you are trying to make a roomba type of program?

//Wait 2 seconds before running the program
wait1Msec(2000);

//When you use "while(true)", that says "while(true == true)", and true will always equal true, meaning it is an infinite loop
while(true){
//If the robot is far away from something
if (whatever){
//do something
}

//You could use "else" below, I just like to use else if

//Else if the robot is close to something
else if (whatever){
//do something else
}
}
}

You would need these if statements to always be running, the problem with your code is that it isn’t always running. The code needs to be looped.

The code says this:
Always run these if statements
Is this boolean true? Okay, then do this
Is this different boolean true? Okay, then do this other thing

Hope this helps

~Jack

I’d suggest creating functions for these, or at least the motor commands. It just makes the program more compact, and you can reuse those functions in other programs (like one for a competition routine).

void moveStraight(int pwr) {
motor[driveRight] =
motor[driveLeft] =
pwr;
}

void turnRight(int pwr) {
motor[driveLeft] = pwr;
motor[driveRight] = -pwr;
}

void turnLeft(int pwr) {
motor[driveRight] = pwr;
motor[driveLeft] = -pwr;
}