Robot works but only decides to go left.

Hello. I am still new to programming. Have successfully programmed this bot. The idea is to move forward till too close to something.(standard) then backs up, turns whole body right, scan, turn left, scan, then decide what to do. He always seems to only decide to go left. being the last command. I feel it is the syntax or the way things are placed in the code. I will copy code to this post cause I dislike downloading more code from others. Using the NewPing.h library. Goal is to only use one sensor, and preferably no servo keeping robot low to the ground.Thank you in advance. peace.
boards arduino uno seed studio motor board and small breadboard and one HC-SR04

// code.
//obstacle avoidance, look in three direction and deciding which is the clearest way to go.
#include <NewPing.h>
#define trigPin 5
#define echoPin 6 
#define MAX_DISTANCE 100
int motorPin = 12; //right motor forward
int motorPin2 = 8; //left motor forward
int motorPin3 = 13; //right motor backward
int motorPin4 = 11; //left motor backward
NewPing sonar(trigPin, echoPin, MAX_DISTANCE);
unsigned int time;                
int distance;                     
int triggerDistance = 30;    
int fDistance;                    
int lDistance;
int rDistance;



//
void setup()
{
 Serial.begin (9600);
 pinMode(motorPin, OUTPUT); //motor outputs.
 pinMode(motorPin2, OUTPUT);
 pinMode(motorPin3, OUTPUT);
 pinMode(motorPin4, OUTPUT);
}
 
 //
 void loop()
 {
   scan();                                    
 fDistance = distance;                      
 if(fDistance < triggerDistance){    
       backward();                          
       delay(1000);
       turnRight();                             
       delay(500);
       moveStop();                              
       scan();                                 
       rDistance = distance;                    
       turnLeft();
       delay(1000);                             
       moveStop();                             
       scan();                                 
       lDistance = distance;                   
       if(lDistance < rDistance){               
         turnRight();                          
         delay(200);
         Forward();                        
       }
       else{
         Forward();                         
       }
 }
       else{
       Forward();                          
    }
 }
 

//function
void scan(){
 time = sonar.ping();                      
 distance = time / US_ROUNDTRIP_CM;   
 if(distance == 0){                        
       distance = 100;                         
 }
 delay(10);
}
void Forward()
{
  digitalWrite(motorPin, HIGH);
  digitalWrite(motorPin2, HIGH);
  analogWrite(10, 190);
  digitalWrite(motorPin3, LOW);
  digitalWrite(motorPin4, LOW);
  analogWrite(9, 180);

}

void backward() 
{
  digitalWrite(motorPin, LOW);
  digitalWrite(motorPin2, LOW);
  analogWrite(10, 190);
  digitalWrite(motorPin3, HIGH);
  digitalWrite(motorPin4, HIGH);
  analogWrite(9, 180);
} 

void turnLeft() 
{
  digitalWrite(motorPin, HIGH);
  digitalWrite(motorPin2, LOW);
  analogWrite(10, 150);
  digitalWrite(motorPin3, LOW);
  digitalWrite(motorPin4, HIGH);
  analogWrite(9, 150);
} 

void turnRight() 
{
  digitalWrite(motorPin, LOW);
  digitalWrite(motorPin2, HIGH);
  analogWrite(10, 150);
  digitalWrite(motorPin3, HIGH);
  digitalWrite(motorPin4, LOW);
  analogWrite(9, 160);
}

void moveStop()
{
 digitalWrite(motorPin, LOW);
  digitalWrite(motorPin2, LOW);
  analogWrite(10, 190);
  digitalWrite(motorPin3, LOW);
  digitalWrite(motorPin4, LOW);
  analogWrite(9, 190);
}

Hi,

Can you please post a copy of your sketch, using code tags?
They are made with the </> icon in the reply Menu.
See section 7 http://forum.arduino.cc/index.php/topic,148850.0.html

What boards are you using, please tell us all your hardware, please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?

Tom... :slight_smile:

  scan();                                   
  fDistance = distance;

This is stupid. The scan() function should NOT diddle with global variables. It SHOULD return a value.

  delay(10);

The scan() function should NOT being doing this. If there is some VALID reason to delay() at all, it should NOT be done in the scan() function.

PaulS:

  scan();                                   

fDistance = distance;



This is stupid. The scan() function should NOT diddle with global variables. It SHOULD return a value.



delay(10);



The scan() function should NOT being doing this. If there is some VALID reason to delay() at all, it should NOT be done in the scan() function.

It does return a value pretty well and as for the delay it is to give the robot time to move from side to side. I am looking for people that will respectfully respond to my post and not call things stupid as was stated, new to programming i am not taking classes. This is a hobby I have gotten into. Thank you for looking.

It does return a value pretty well

A void function by definition does not return a value

 Serial.begin (9600);

This looks hopeful.
Perhaps you can get your sketch to tell you what it is doing, instead of having to infer it.

If you are driving down the street, do you close your eyes, opening them only once every 10 milliseconds? Of course not. So, why is your robot blind so much of the time? Why is there a delay() at all?

So I have deleted the delay seems to have no affect. I have attempted to add Serial.print with no avail to helping me. Now if anyone has some kind of constructive criticism would be greatly appreciated. people who don't can just ignore this post. Thank you.

I have attempted to add Serial.print with no avail to helping me.

Maybe you printed the wrong thing, or put it in the wrong place.
But only you know that. (hint)

This is the updated code if anyone cares to take a look. print the distance now and removed the a forward movement. What do you think of changing the void scan to a unsigned scan? all works a little better but still only going left.

//obstacle avoidance, look in three direction and deciding which is the clearest way to go.
#include <NewPing.h>
#define trigPin 5
#define echoPin 6 
#define MAX_DISTANCE 100
int motorPin = 12; //right motor forward
int motorPin2 = 8; //left motor forward
int motorPin3 = 13; //right motor backward
int motorPin4 = 11; //left motor backward
NewPing sonar(trigPin, echoPin, MAX_DISTANCE);
unsigned int time;                
int distance;                     
int triggerDistance = 30;    
int fDistance;                    
int lDistance;
int rDistance;
int in1,in2,in3;


//
void setup()
{
  Serial.begin (9600);
  pinMode(motorPin, OUTPUT); //motor outputs.
  pinMode(motorPin2, OUTPUT);
  pinMode(motorPin3, OUTPUT);
  pinMode(motorPin4, OUTPUT);
}
  
  //
  void loop()
  {
     scan();                                    
  fDistance = distance;                      
  if(fDistance < triggerDistance){  
  Serial.print(fDistance);
  Serial.print(" ");  
        Backward();                          
        delay(1000);
        turnRight();                             
        delay(500);
        moveStop();                              
        scan();  

        rDistance = distance; 
Serial.print(rDistance);
  Serial.println(" ");        
        turnLeft();
        delay(1000);                             
        moveStop();                             
        scan();                                 
        lDistance = distance;   
Serial.print(lDistance);
  Serial.print(" ");        
        if(lDistance < rDistance){               
          turnRight();                          
          delay(200);
          Forward();                        
        }
        
  }
  else{
        Forward();                          
  }
}

   

//function
unsigned scan(){
  time = sonar.ping();                      
  distance = time / US_ROUNDTRIP_CM;   
  if(distance == 0){                        
        distance = 100;                         
  }
}
void Forward()
{
   digitalWrite(motorPin, HIGH);
   digitalWrite(motorPin2, HIGH);
   analogWrite(10, 190);
   digitalWrite(motorPin3, LOW);
   digitalWrite(motorPin4, LOW);
   analogWrite(9, 180);

 }

 void Backward() 
 {
   digitalWrite(motorPin, LOW);
   digitalWrite(motorPin2, LOW);
   analogWrite(10, 190);
   digitalWrite(motorPin3, HIGH);
   digitalWrite(motorPin4, HIGH);
   analogWrite(9, 180);
} 

 void turnLeft() 
 {
   digitalWrite(motorPin, HIGH);
   digitalWrite(motorPin2, LOW);
   analogWrite(10, 150);
   digitalWrite(motorPin3, LOW);
   digitalWrite(motorPin4, HIGH);
   analogWrite(9, 150);
} 

 void turnRight() 
 {
   digitalWrite(motorPin, LOW);
   digitalWrite(motorPin2, HIGH);
   analogWrite(10, 150);
   digitalWrite(motorPin3, HIGH);
   digitalWrite(motorPin4, LOW);
   analogWrite(9, 160);
}

void moveStop()
{
  digitalWrite(motorPin, LOW);
   digitalWrite(motorPin2, LOW);
   analogWrite(10, 190);
   digitalWrite(motorPin3, LOW);
   digitalWrite(motorPin4, LOW);
   analogWrite(9, 190);
}

What do you think of changing the void scan to a unsigned scan?

Utterly pointless if it doesn't return the value you promised the compiler it would.

What do the prints tell you (and us. Hint)?

AWOL:
Utterly pointless if it doesn't return the value you pro is ed the compiler it would.

What do the prints tell you (and us. Hint)?

the prints show a value in the serial monitor proving that is returning something.

besides if you do not put a scan type function, in how can you call upon it later when you need to find a direction to go?

the prints show a value in the serial monitor proving that is returning something

Your concept of proof and mine differ enormously.

Can you see a return statement in your scan function?

besides if you do not put a scan type function, in how can you call upon it later when you need to find a direction to go?

Is that a non sequitur?

correct with the bot stationary (ie not rotating) I can see all three measurements. with an object in front of the sensor i get the same readings if you move the object after getting one reading you get a new measurement.

Again, please, this time with punctuation.

Apologies. I had gloves on. So with the Bot Stationary, unable to turn left or right, You get the fDistance reading (lets say 18cm). Then when adding print to lDistance and rDistance you receive 2 more reading. Looks like this, 18 18 18 in a row, then removing the object blocking the path, object removed during turning delays you will receive a new line of distance. Like this 18 29 100, depending on your surrounding here you can see the least obstructed path is to the left.

Lets start with fixing the scan() function so it properly returns a value:

int scan()
{
  unsigned long echoTime = sonar.ping();                      
  int howFar = echoTime / US_ROUNDTRIP_CM;   
  if(howFar == 0)
  {                        
        howFar = 100;                         
  }
  return howFar;
}

NO global variables, except the sonar object, are used in this function. So, the time and distance global variables can be deleted.

     scan();                                    
  fDistance = distance;

should then be:

   fDistance = scan();

(with no stupid spaces following the ;).

Print anonymous data is stupid.

Serial.print("fDistance = ");
Serial.println(fDistance);

conveys so much more information.

Now, having made these changes (and the related ones for the other places scan is called), show us your new code (AFTER using Tools + Auto Format), your serial output, and explain what the code is doing that you don't want, or not doing that you do want. WITHOUT GLOVES!

Thank you PaulS for you response. After I finish working on this car and then I can repair my code accordingly. Again thank you.

Well changed the code as PaulS suggested. Now I do not return any values only 0 in the monitor. And now he only goes backwards and turns right and left then backwards again. I have not had a chance to play with the code and find any adjustments. This is just an update.

//obstacle avoidance, look in three direction and deciding which is the clearest way to go.
#include <NewPing.h>
#define trigPin 5
#define echoPin 6 
#define MAX_DISTANCE 100
int motorPin = 12; //right motor forward
int motorPin2 = 8; //left motor forward
int motorPin3 = 13; //right motor backward
int motorPin4 = 11; //left motor backward
NewPing sonar(trigPin, echoPin, MAX_DISTANCE);
unsigned int time;                
int triggerDistance = 30;    
int fDistance;                    
int lDistance;
int rDistance;
int scan(){
  unsigned long echoTime = sonar.ping();                      
  int howFar = echoTime / US_ROUNDTRIP_CM;   
  if(howFar == 0){                        
  }
}

//
void setup()
{
  Serial.begin (9600);
  pinMode(motorPin, OUTPUT); //motor outputs.
  pinMode(motorPin2, OUTPUT);
  pinMode(motorPin3, OUTPUT);
  pinMode(motorPin4, OUTPUT);
}
  
  //
  void loop()
  {
  fDistance = scan();                      
  if(fDistance < triggerDistance){  
  Serial.print("fDistance = ");
  Serial.println(fDistance);  
        Backward();                          
        delay(1000);
        turnRight();                             
        delay(500);
        moveStop();                              
        rDistance = scan(); 
Serial.print("rDistance = ");
  Serial.println(fDistance);        
        turnLeft();
        delay(1000);                             
        moveStop();                             
        lDistance = scan();   
Serial.print("lDistance = ");
  Serial.println(lDistance);        
        if(lDistance < rDistance){               
          turnRight();                          
          delay(200);
          Forward();                        
        }
       else{
        Forward(); 
  }
  }
  else{
        Forward();                          
  }
}

   

//function

void Forward()
{
   digitalWrite(motorPin, HIGH);
   digitalWrite(motorPin2, HIGH);
   analogWrite(10, 190);
   digitalWrite(motorPin3, LOW);
   digitalWrite(motorPin4, LOW);
   analogWrite(9, 180);

 }

 void Backward() 
 {
   digitalWrite(motorPin, LOW);
   digitalWrite(motorPin2, LOW);
   analogWrite(10, 190);
   digitalWrite(motorPin3, HIGH);
   digitalWrite(motorPin4, HIGH);
   analogWrite(9, 180);
} 

 void turnLeft() 
 {
   digitalWrite(motorPin, HIGH);
   digitalWrite(motorPin2, LOW);
   analogWrite(10, 150);
   digitalWrite(motorPin3, LOW);
   digitalWrite(motorPin4, HIGH);
   analogWrite(9, 150);
} 

 void turnRight() 
 {
   digitalWrite(motorPin, LOW);
   digitalWrite(motorPin2, HIGH);
   analogWrite(10, 150);
   digitalWrite(motorPin3, HIGH);
   digitalWrite(motorPin4, LOW);
   analogWrite(9, 160);
}

void moveStop()
{
  digitalWrite(motorPin, LOW);
   digitalWrite(motorPin2, LOW);
   analogWrite(10, 190);
   digitalWrite(motorPin3, LOW);
   digitalWrite(motorPin4, LOW);
   analogWrite(9, 190);
}
int scan()
{
  unsigned long echoTime = sonar.ping();                     
  int howFar = echoTime / US_ROUNDTRIP_CM;   
  if(howFar == 0){                       
  }
}

Again, you've promised the compiler that you're going to return a value, and then you've broken that promise.

Well changed the code as PaulS suggested.

No, you didn't. Read it again

AWOL:

int scan()

{
  unsigned long echoTime = sonar.ping();                   
  int howFar = echoTime / US_ROUNDTRIP_CM; 
  if(howFar == 0){                     
  }
}



Again, you've promised the compiler that you're going to return a value, end then you've broken that promise.
No, you didn't. Read it again

Thank you I see what I missed. AWOL is there any other more in depth books out there about programming Arduino. I have read the beginners books and blogs but I don't feel like I have found anything that is truly creditable. After starting this post I am starting to understand where I was going wrong.