obstacles avoiding robot

i need to make car robot to get out from maze.i only be provided by one sensor.anyone know how to program it to go out of the maze and stop at the finish line?here is my program,but i only can make it turn left.i want to program it to turn left at first,and then right when it found obstacles.then,it must stop at finish line.any suggestion?

int distance;
int speedr=50;
int speedl=50;
int mode=0;

void motor_control(){ //forward
if(mode==1){
digitalWrite(1,LOW);
digitalWrite(2,HIGH);
digitalWrite(12,HIGH);
digitalWrite(13,LOW);

} else if(mode==2){ //left
digitalWrite(1,LOW);
digitalWrite(2,HIGH);
digitalWrite(12,LOW);
digitalWrite(13,HIGH);

} else if(mode==3){ //right
digitalWrite(1,HIGH);
digitalWrite(2,LOW);
digitalWrite(12,HIGH);
digitalWrite(13,LOW);

} else if(mode==4){ //backward
digitalWrite(1,HIGH);
digitalWrite(2,LOW);
digitalWrite(12,LOW);
digitalWrite(13,HIGH);

} else { //stop
digitalWrite(1,LOW);
digitalWrite(2,LOW);
digitalWrite(12,LOW);
digitalWrite(13,LOW);
}
}
void setup(){
pinMode(1,OUTPUT);
pinMode(2,OUTPUT);
pinMode(12,OUTPUT);
pinMode(13,OUTPUT);
analogWrite(3,speedr);
analogWrite(11,speedl);
}

void loop(){
distance = analogRead(0);
if(distance<500){
mode=1;
motor_control();
delay(50);
}else{
mode=2;
motor_control();
delay(1000);
}
}

if(distance<500){
    mode=1;
    motor_control();
    delay(50);
  }else{

This isn’t BASIC; functions can have parameters - there is no need for them to be global.

,but i only can make it turn left

Well, because you never set “mode” to be three, I think I can understand that.

(What is wrong with your shift key?)

i really new to this..how to add mode 3?

Well first you need to decide what condition you want that will make it turn right, after that, the comments in the code will help you.

the condition to turn left or right are same,so what should i do?anyway tq for reply..

the condition to turn left or right are same

So how do you decide which one you want to do?
A random number?

Who wrote the code above?

my teacher give me this code,but this is basic one.i have to change it a little bit.i have to go forward,turn left,then turn right to get out of the wall and stop at finish line.

Does this make things any clearer?

const int speedr=50;
const int speedl=50;

enum direction {Forward, Left, Right, Reverse};

void motor_control(int mode)
{          
  switch (mode) {
  
  case Forward:
    digitalWrite(1,LOW);
    digitalWrite(2,HIGH);
    digitalWrite(12,HIGH);
    digitalWrite(13,LOW);
  break;  
 
  case Left:
    digitalWrite(1,LOW);
    digitalWrite(2,HIGH);
    digitalWrite(12,LOW);
    digitalWrite(13,HIGH);
  break;  
 
  case Right:
    digitalWrite(1,HIGH);
    digitalWrite(2,LOW);
    digitalWrite(12,HIGH);
    digitalWrite(13,LOW);
  break;
    
  case Reverse:
    digitalWrite(1,HIGH);
    digitalWrite(2,LOW);
    digitalWrite(12,LOW);
    digitalWrite(13,HIGH);
  break;
    
  default:
    digitalWrite(1,LOW);
    digitalWrite(2,LOW);
    digitalWrite(12,LOW);
    digitalWrite(13,LOW);
  }
}

void setup()
{
  pinMode(1,OUTPUT);
  pinMode(2,OUTPUT);
  pinMode(12,OUTPUT);
  pinMode(13,OUTPUT);
  analogWrite(3,speedr);
  analogWrite(11,speedl);
}

void loop()
{
  int distance = analogRead(0);
  if(distance<500){
    motor_control(Forward);
    delay(50);
  }else{
    motor_control(Left);
    delay(1000);
  }
}

I’m not sure on how your rangefinder is set up, but driving forward as the range appears to reduce doesn’t sound like a good strategy to me.
YMMV.

(I’d concentrate next on giving those output pins with some meaningful names)

tq for the code :slight_smile:
i want to make it turn right too,so what i have to do?

void loop()
{
  int distance = analogRead(0);
  if(distance<500){
    motor_control(Forward);
    delay(50);
  }else{
    motor_control(Left);
    delay(1000);
  }
}

I've labelled all the actions. You have to decide what condition has to exist for you to want to turn right.

how about this?

void loop()
{
  int distance = analogRead(0);
  if(distance<500){
    motor_control(Forward);
    delay(50);
    
  }else if(distance>500){
    motor_control(Left);
    delay(1000);
    
  }else if(distance>500){
    motor_control(Right);
    delay(1000);  
  }
}

"else" clauses don't really work with exactly the same condition.

yeah,i tried it but it only turn left.so if it same condition,what type of command i should use?

i really appreciate for your help.

How can it be the same condition? You can't simultaneously turn right AND turn left.

ok,i get your point there.i attach the wall`s picture that i should avoid.any idea what i can do with the code?

IMG_0291[1].JPG|1600x1200

OK, no scale, but it lloks to me like you need to:

Set "Forward" while (distance > x); // keep going forward. rotate 90 degrees left; set "forward"; while (distance > x); // keep going forward. rotate 90 degrees right; Set "forward";

Of course, turning 90 degrees without some feedback is going to be tricky, but you'll probably just have to time it.

Look at your picture. You know where the robot starts. At the first place where it can not continue forward, you need to turn one way. At the next place, you need to turn the other way. You can count how many turns you have made, so you turn the right way each time.

If the robot is supposed to figure out for itself which way to go, you need more sensors, pointing left and right, or you need to move the one you have, and take readings to the left, center, and right.

Or, just run into the walls...