void forwardDistance(int dist) { int speed = 100; while(getDistanceValue(distance)>dist)//the limit is -85 degrees {//Turning right speed = getDistanceValue(distance) - dist; setMotorSpeed(left, speed );//left motor is going forward setMotorSpeed(right, speed);//right motor is going backwards } setMotorSpeed(left, 0); setMotorSpeed(right, 0); wait1Msec(1000); }
//drive forward using the ultrasonic sensor void driveWithUltraSonic(int distance, int speed) { if(distance > 0) { while(getDistanceValue(DISTANCE_SENSOR) < distance) { setMotorSpeed(LEFT_DRIVETRAIN_MOTOR, speed); setMotorSpeed(RIGHT_DRIVETRAIN_MOTOR, -speed); } } else { while(getDistanceValue(DISTANCE_SENSOR) > -distance) { setMotorSpeed(LEFT_DRIVETRAIN_MOTOR, - speed); setMotorSpeed(RIGHT_DRIVETRAIN_MOTOR, speed); } } setMotorSpeed(LEFT_DRIVETRAIN_MOTOR, 0); setMotorSpeed(RIGHT_DRIVETRAIN_MOTOR, 0); }