loading

Whats wrong with the sketch??

This arduino robo, there is no servo, this runs only with 2 geared motors, i made the sketch according to the needs bt it doent seem to making those turns even after sensing the obstacle. The ping sensor is functional and connected properly and thel293d ic is also functional..Pls tell me whats wrong with dis sketch..........

 #include <NewPing.h>


#define LeftMotorForward 11
#define LeftMotorBackward 10
#define RightMotorForward 9
#define RightMotorBackward 8
#define USTrigger 4
#define USEcho 5
#define MaxDistance 100

NewPing sonar(USTrigger, USEcho, MaxDistance);


unsigned int duration;
unsigned int distance;
unsigned int FrontDistance;
unsigned int LeftDistance;
unsigned int RightDistance;
void setup() 
{
 
  pinMode(LeftMotorForward, OUTPUT);
  pinMode(LeftMotorBackward, OUTPUT);
  pinMode(RightMotorForward, OUTPUT);
  pinMode(RightMotorBackward, OUTPUT);

}   
  void loop()                                          
{                                         
  FrontDistance = distance;                          
 
  if(FrontDistance > 40 || FrontDistance == 0)       
  {
   moveForward();                                   
  }
  else                                               
  {
    moveBackward();
   
    moveStop();                                      
    navigate();
  }
}

void moveForward()                                   
{

  digitalWrite(LeftMotorBackward, LOW);
  digitalWrite(LeftMotorForward, HIGH);
  digitalWrite(RightMotorBackward, LOW);
  digitalWrite(RightMotorForward, HIGH);
}

void moveBackward()                                
{
 
  digitalWrite(LeftMotorForward, LOW);
  digitalWrite(LeftMotorBackward, HIGH);
  digitalWrite(RightMotorForward, LOW);
  digitalWrite(RightMotorBackward, HIGH);
}

void moveLeft()                                    
{
 
  digitalWrite(LeftMotorForward, LOW);
  digitalWrite(LeftMotorBackward, HIGH);
  digitalWrite(RightMotorBackward, LOW);
  digitalWrite(RightMotorForward, HIGH);
 
}

void moveRight()                                  
{
    digitalWrite(LeftMotorBackward, LOW);
  digitalWrite(LeftMotorForward, HIGH);
  digitalWrite(RightMotorForward, LOW);
  digitalWrite(RightMotorBackward, HIGH);
}

void moveStop()                                   
{

  digitalWrite(LeftMotorBackward, LOW);
  digitalWrite(LeftMotorForward, LOW);
  digitalWrite(RightMotorForward, LOW);
  digitalWrite(RightMotorBackward, LOW);
}
void scan()                                      
{
 
  distance = sonar.ping_cm();
  delay(500);
}
void navigate()
{
    moveLeft();
    delay(1000);
    scan();                                          
    LeftDistance = distance;                         
 
    moveRight();                                 
    delay(1000);
    scan();
   
  RightDistance = distance;                        
  
   
    if(RightDistance < LeftDistance)                 
    {
     moveLeft();                                     
     delay(200);                                     
    }
    else if(LeftDistance < RightDistance)            
    {
     moveRight();                                    
     delay(200);                                     
    }
}

sort by: active | newest | oldest

I'm not the world greatest coder, but to me it doesn't look like you are actually calling your scan or navigate functions from your main loop. To debug you could cut and paste your scan function and put it at the start of your main loop.

Correction: After looking at your code again I see where you are calling navigate(); but not scan();

Try this:

void loop()

{

scan();

FrontDistance = distance;

.........

He does call it its a bit hard to see.

void scan()
{

distance = sonar.ping_cm();
delay(500);
}

not sure if distance = sonar.ping_cm (); is right it must be some command from the library

mrandle2 years ago

I think it may have something to do with how you are calculating distance. No matter what you do it is always going to move forward because it can't get to 0, which will depend on your sensor. Maybe make it greater than 0 like 5 or 10? The way this is set up it will move till it hits something and THEN back up, so won't avoid anything.

Run the robot with some debugging serial print lines and run around with a cable to log what is actually gping through its mind/.