Vex IQ House Rover

This is my vex iq house rover. It simply explores your house by going forward until it is close to a wall. Then it turns in a random direction and starts going again. Even though there is one problem, i can’t figure out ho to make it randomly choose to turn left or right so i just have it turning right for now. So i was wandering if ya’ll could help me out.

2 Likes

I can probably help, but I can’t help at all if you don’t post pictures of your robot and a picture of your robot code. This sounds like a great project!
To post a picture of your robot, press the Upload button when making your post. To post a picture of your code, do the same thing, to post your actual exact code, click the Preformatted Text or Control+Shift+C when making your post and then paste your code into the box.

1 Like

IMG_0717

IMG_0720

1 Like

How are you programming this robot? (RobotC, VEXcode IQ Blocks, RobotMesh)

What language are you coding in?

1 Like

I’m using robotc.


#pragma config(Sensor, port7,  dis,            sensorVexIQ_Distance)
#pragma config(Sensor, port8,  bump,           sensorVexIQ_Touch)
#pragma config(Sensor, port9,  bumpp,          sensorNone)
#pragma config(Motor,  motor5,          lmoter,        tmotorVexIQ, PIDControl, driveLeft, encoder)
#pragma config(Motor,  motor10,         rmotor,        tmotorVexIQ, PIDControl, reversed, driveRight, encoder)
//*!!Code automatically generated by 'ROBOTC' configuration wizard               !!*//

int dist;




task main()
{
	

	repeat(forever)
	{
	
	dist = getDistanceValue(dis);
	displaySensorValues(1, dist);
	setMultipleMotors(100, lmoter, rmotor);
  	
	if (dist >= 20 & dist <= 200)
		{
			stopMultipleMotors(lmoter, rmotor);
			backward(1, seconds, 50);
			turnRight(random(250), degrees, 50);
      waitUntilMotorStop(rmotor);
			waitUntilMotorStop(lmoter);
      setMultipleMotors(100, lmoter, rmotor);
      }
      

	}

}

1 Like

rand() % 2 will evaluate to a random integer, either 0 or 1.

You can branch based on the result of this call like so:

if (rand() % 2){
    //this code will run if the random number generated is 1
}
else {
    //this code will run if the random number generated is 0
}

Note that if (rand() % 2) works in this case because integers evaluate to false if 0, and true otherwise. If you want more than 2 options, rand() % n will generate a random int from 0 to n-1 inclusive, and you can branch based on the result of that with a switch or a bunch of else ifs.

3 Likes

Thanks a lot. It works perfectly. Here’s the new code:

#pragma config(Sensor, port7,  dis,            sensorVexIQ_Distance)
#pragma config(Sensor, port8,  bump,           sensorVexIQ_Touch)
#pragma config(Sensor, port9,  bumpp,          sensorNone)
#pragma config(Motor,  motor5,          lmoter,        tmotorVexIQ, PIDControl, driveLeft, encoder)
#pragma config(Motor,  motor10,         rmotor,        tmotorVexIQ, PIDControl, reversed, driveRight, encoder)
//*!!Code automatically generated by 'ROBOTC' configuration wizard               !!*//

int dist;




task main()
{
	

	repeat(forever)
	{
	
	dist = getDistanceValue(dis);
	displaySensorValues(1, dist);
	setMultipleMotors(100, lmoter, rmotor);
  	
	if (dist >= 20 & dist <= 200)
		{
			stopMultipleMotors(lmoter, rmotor);
			backward(1, seconds, 50);
		if (rand() % 2){
    turnRight(random(350), degrees, 50);
    
    
    }
    else {
    turnLeft(random(350), degrees, 50);
    }
      waitUntilMotorStop(rmotor);
			waitUntilMotorStop(lmoter);
      setMultipleMotors(100, lmoter, rmotor);
      }
      

	}



}
2 Likes