Obstacle avoiding car

Hi, I've made an obstacle avoiding car using ultrasound sensor glued to servo motor. My problem is - the sensor seems to work right but the car is not going forward it is just adjusting itself all the time. Also I power it with 9V battery and it seems to struggle, when connected to PC via USB it runs much better.

9v 'smoke detector' batteries are unsuitable for any application involving motors.
They are decent at providing a few mAh for electronics.
They are horrible when you try to pull the amount of current that motors demand.
Get a better power source. AA batteries are often a good choice.

As for the behavior of your code, we cannot help unless you post the code. Use code tags.

Here is the code //ARDUINO OBSTACLE AVOIDING CAR//// Before uploading the code you have to inst - Pastebin.com

Could the problem be that I'm using SFR05 sensor instead of SFR04?

nero_:
Here is the code //ARDUINO OBSTACLE AVOIDING CAR//// Before uploading the code you have to inst - Pastebin.com

Could the problem be that I'm using SFR05 sensor instead of SFR04?

vinceherman:
As for the behavior of your code, we cannot help unless you post the code. Use code tags.

Please.

#include <AFMotor.h>  
#include <NewPing.h>
#include <Servo.h> 

#define TRIG_PIN A0 
#define ECHO_PIN A1 
#define MAX_DISTANCE 200 
#define MAX_SPEED 190 // sets speed of DC  motors
#define MAX_SPEED_OFFSET 20

NewPing sonar(TRIG_PIN, ECHO_PIN, MAX_DISTANCE); 

AF_DCMotor motor1(1, MOTOR12_1KHZ); 
AF_DCMotor motor2(2, MOTOR12_1KHZ);
AF_DCMotor motor3(3, MOTOR34_1KHZ);
AF_DCMotor motor4(4, MOTOR34_1KHZ);
Servo myservo;   

boolean goesForward=false;
int distance = 100;
int speedSet = 0;

void setup() {

 myservo.attach(10);  
 myservo.write(115); 
 delay(2000);
 distance = readPing();
 delay(100);
 distance = readPing();
 delay(100);
 distance = readPing();
 delay(100);
 distance = readPing();
 delay(100);
}

void loop() {
int distanceR = 0;
int distanceL =  0;
delay(40);

if(distance<=15)
{
 moveStop();
 delay(100);
 moveBackward();
 delay(300);
 moveStop();
 delay(200);
 distanceR = lookRight();
 delay(200);
 distanceL = lookLeft();
 delay(200);

 if(distanceR>=distanceL)
 {
   turnRight();
   moveStop();
 }else
 {
   turnLeft();
   moveStop();
 }
}else
{
 moveForward();
}
distance = readPing();
}

int lookRight()
{
   myservo.write(50); 
   delay(500);
   int distance = readPing();
   delay(100);
   myservo.write(115); 
   return distance;
}

int lookLeft()
{
   myservo.write(170); 
   delay(500);
   int distance = readPing();
   delay(100);
   myservo.write(115); 
   return distance;
   delay(100);
}

int readPing() { 
 delay(70);
 int cm = sonar.ping_cm();
 if(cm==0)
 {
   cm = 250;
 }
 return cm;
}

void moveStop() {
 motor1.run(RELEASE); 
 motor2.run(RELEASE);
 motor3.run(RELEASE);
 motor4.run(RELEASE);
 } 
 
void moveForward() {

if(!goesForward)
 {
   goesForward=true;
   motor1.run(FORWARD);      
   motor2.run(FORWARD);
   motor3.run(FORWARD); 
   motor4.run(FORWARD);     
  for (speedSet = 0; speedSet < MAX_SPEED; speedSet +=2) // slowly bring the speed up to avoid loading down the batteries too quickly
  {
   motor1.setSpeed(speedSet);
   motor2.setSpeed(speedSet);
   motor3.setSpeed(speedSet);
   motor4.setSpeed(speedSet);
   delay(5);
  }
 }
}

void moveBackward() {
   goesForward=false;
   motor1.run(BACKWARD);      
   motor2.run(BACKWARD);
   motor3.run(BACKWARD);
   motor4.run(BACKWARD);  
 for (speedSet = 0; speedSet < MAX_SPEED; speedSet +=2) // slowly bring the speed up to avoid loading down the batteries too quickly
 {
   motor1.setSpeed(speedSet);
   motor2.setSpeed(speedSet);
   motor3.setSpeed(speedSet);
   motor4.setSpeed(speedSet);
   delay(5);
 }
}  

void turnRight() {
 motor1.run(FORWARD);
 motor2.run(FORWARD);
 motor3.run(BACKWARD);
 motor4.run(BACKWARD);     
 delay(500);
 motor1.run(FORWARD);      
 motor2.run(FORWARD);
 motor3.run(FORWARD);
 motor4.run(FORWARD);      
} 

void turnLeft() {
 motor1.run(BACKWARD);     
 motor2.run(BACKWARD);  
 motor3.run(FORWARD);
 motor4.run(FORWARD);   
 delay(500);
 motor1.run(FORWARD);     
 motor2.run(FORWARD);
 motor3.run(FORWARD);
 motor4.run(FORWARD);
}

Assuming that you have resolved your power source issues and your robot is still not functioning correctly, I would break the process down into a couple of isolated things to check:

  • Are the readPing() values in the range that you expect? Are there occasional “noisy” readings that are throwing off the operation?
  • Do the motor control functions (moveForward/backward, turnRight/Left) operate properly? If you make a simple program in setup() that runs a known sequence of forward, right, forward, left, forward, back, including stop, does it work as expected?
  • When nothing is near the robot, does it drive straight? You could change the first delay in your (distance <= 15) from 100 to 5000 to make it obvious if it enters this portion of your code when you don’t expect it to.
  • Test the robot with serial output by plugging it in to your computer, elevate the robot so that the wheels are off the ground and facing away from your computer (to ensure nothing nearby the front and sides). Add serial print statements in each of your IF statement sections. Use your hands to mimic various distances in front and to the side. What step / condition in the program is not operating as expected?
  • Increase your delays by 10x to see if each of the steps in your distance <15 clause are being performed properly.