newbie asking.. master plz help me

i'm sorry for this stupid question but i'm really dying to know how to build an obstacle avoider robot,, i'm already build it and try few program but its all didn't work or work but not like it should do,,
here's my diagram
i'm using ARDUINO UNO R3, L298 shield DFR Robot

My first piece of advice would be to stop using those 9V batteries if they are really what you are using and not just in the diagram for illustration only. They are not capable of running the Arduino for more than a few minutes and they will certainly not run motors or the servo sensibly because they cannot deliver enough current.

The best way to start is to get each element of your project working independently then combine them together. Have tried the example programs for the ultrasonic sensor, the servo and the motor shield ? If not, what have you tried ?

Post any code that you have tried, making sure that you read, understand and follow the advice in the stickies at the top of this forum about how to do it.

off course i'm already tried each one of those component,, so what whould you suggest for me to use power supply that i need,,, here the program : ( i attach the program below)

btw thaks for reply :slight_smile:

test.ino.txt (9.25 KB)

For testing purposes 4 AA batteries in series will suffice. If the robot is to be used for extended periods then you need to consider using rechargeable batteries such as NiCads, NMh or LiPo batteries.

There are several places in your code where the comments do not seem to match the code. For instance

AF_DCMotor motor1(1, MOTOR12_1KHZ); // create motor #1 using M1 output on Motor Drive Shield, set to 1kHz PWM frequency
AF_DCMotor motor2(4, MOTOR12_1KHZ); // create motor #2, using M2 output, set to 1kHz PWM frequency

I am not familiar with the library that you are using but the second line above looks inconsistent with the first one.

  myservo.attach(10);  // attaches the servo on pin 9 (SERVO_2 on the Motor Drive Shield to the servo object 
  myservo.write(45); // tells the servo to position at 90-degrees ie. facing forward.
  delay(2000); // delay for two seconds
  checkPath(); // run the CheckPath routine to find the best path to begin travel
  motorSet = "FORWARD"; // set the director indicator variable to FORWARD
  myservo.write(90); // make sure servo is still facing forward
  moveForward(); // run function to make robot move forward
}

In this section of code you attach a servo to pin 10 but say that you are using pin 9. Which is it ?
Writing 45 to the servo tells the servo to face forward but so apparently does writing 90 to it. Which is it ?

void checkForward() 
{ 
  if (motorSet=="FORWARD") 
  {
    motor1.run(FORWARD); 
    motor2.run(FORWARD); 
  } 
}     // make sure motors are going forward

Despite its name this function and the backwards one does no checking.

In checkPath() you call checkForward() to "check the robot is still moving forward". Why wouldn't it be still moving forward ? Even if is not you force it to so why check anything ?

  if (curDist > curDist) When will this be true ?

ooh i'm sorry i'm already change some of my program but i forgot to change the sentence //(comment)
to be honest i got this program from internet hehe,, i try to change it a bit,, and here is the original program :

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

#define TRIG_PIN A4 // Pin A4 on the Motor Drive Shield soldered to the ultrasonic sensor
#define ECHO_PIN A5 // Pin A5 on the Motor Drive Shield soldered to the ultrasonic sensor
#define MAX_DISTANCE 200 // sets maximum useable sensor measuring distance to 200cm
#define MAX_SPEED 180 // sets speed of DC traction motors to 180/256 or about 70% of full speed - to get power drain down.
#define MAX_SPEED_OFFSET 40 // this sets offset to allow for differences between the two DC traction motors ****** from 20
#define COLL_DIST 10 // sets distance at which robot stops and reverses to 10cm
#define TURN_DIST COLL_DIST+10 // sets distance at which robot veers away from object (not reverse) to 20cm (10+10)
NewPing sonar(TRIG_PIN, ECHO_PIN, MAX_DISTANCE); // sets up sensor library to use the correct pins to measure distance.

AF_DCMotor motor1(1, MOTOR12_1KHZ); // create motor #1 using M1 output on Motor Drive Shield, set to 1kHz PWM frequency
AF_DCMotor motor2(2, MOTOR12_1KHZ); // create motor #2, using M2 output, set to 1kHz PWM frequency
Servo myservo;  // create servo object to control a servo

int leftDistance, rightDistance; //distances on either side
int curDist = 0;
String motorSet = "";
int speedSet = 0;

//-------------------------------------------- SETUP LOOP ----------------------------------------------------------------------------
void setup() {
  myservo.attach(9);  // attaches the servo on pin 9 (SERVO_2 on the Motor Drive Shield to the servo object
  myservo.write(90); // tells the servo to position at 90-degrees ie. facing forward.
  delay(1000); // delay for one seconds
 }
//------------------------------------------------------------------------------------------------------------------------------------

//---------------------------------------------MAIN LOOP ------------------------------------------------------------------------------
void loop() {
  myservo.write(90);  // move eyes forward
  delay(90);
  curDist = readPing();   // read distance
  if (curDist < COLL_DIST) {changePath();}  // if forward is blocked change direction
  moveForward();  // move forward for 1/2 second
  delay(500);
 }
//-------------------------------------------------------------------------------------------------------------------------------------

void changePath() {
  moveStop();   // stop forward movement
  myservo.write(36);  // check distance to the right
    delay(500);
    rightDistance = readPing(); //set right distance
    delay(500);
    myservo.write(144);  // check distace to the left
    delay(700);
    leftDistance = readPing(); //set left distance
    delay(500);
    myservo.write(90); //return to center
    delay(100);
    compareDistance();
  }

 
void compareDistance()   // find the longest distance
{
  if (leftDistance>rightDistance) //if left is less obstructed
  {
    turnLeft();
  }
  else if (rightDistance>leftDistance) //if right is less obstructed
  {
    turnRight();
  }
   else //if they are equally obstructed
  {
    turnAround();
  }
}


//-------------------------------------------------------------------------------------------------------------------------------------

int readPing() { // read the ultrasonic sensor distance
  delay(70);  
  unsigned int uS = sonar.ping();
  int cm = uS/US_ROUNDTRIP_CM;
  return cm;
}
//-------------------------------------------------------------------------------------------------------------------------------------
void moveStop() {motor1.run(RELEASE); motor2.run(RELEASE);}  // stop the motors.
//-------------------------------------------------------------------------------------------------------------------------------------
void moveForward() {
    motorSet = "FORWARD";
    motor1.run(FORWARD);      // turn it on going forward
    motor2.run(FORWARD);      // turn it on going 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+MAX_SPEED_OFFSET);
    delay(5);
  }
}
//-------------------------------------------------------------------------------------------------------------------------------------
void moveBackward() {
    motorSet = "BACKWARD";
    motor1.run(BACKWARD);      // turn it on going forward
    motor2.run(BACKWARD);     // turn it on going 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+MAX_SPEED_OFFSET);
    delay(5);
  }
} 
//-------------------------------------------------------------------------------------------------------------------------------------
void turnRight() {
  motorSet = "RIGHT";
  motor1.run(FORWARD);      // turn motor 1 forward
  motor2.run(BACKWARD);     // turn motor 2 backward
  delay(400); // run motors this way for 400ms
  motorSet = "FORWARD";
  motor1.run(FORWARD);      // set both motors back to forward
  motor2.run(FORWARD);     
} 
//-------------------------------------------------------------------------------------------------------------------------------------
void turnLeft() {
  motorSet = "LEFT";
  motor1.run(BACKWARD);     // turn motor 1 backward
  motor2.run(FORWARD);      // turn motor 2 forward
  delay(400); // run motors this way for 400ms
  motorSet = "FORWARD";
  motor1.run(FORWARD);      // turn it on going forward
  motor2.run(FORWARD);      // turn it on going forward
} 
//-------------------------------------------------------------------------------------------------------------------------------------
void turnAround() {
  motorSet = "RIGHT";
  motor1.run(FORWARD);      // turn motor 1 forward
  motor2.run(BACKWARD);     // turn motor 2 backward
  delay(800); // run motors this way for 800ms
  motorSet = "FORWARD";
  motor1.run(FORWARD);      // set both motors back to forward
  motor2.run(FORWARD);     
}

with this program i think the servo and ping sensor works good, when i block ultrasonic sensor its move to another path and thats means my servo its work as well
but the problem is my dc motor won't work at all >,<

AF_DCMotor motor1(1, MOTOR12_1KHZ); // create motor #1 using M1 output on Motor Drive Shield, set to 1kHz PWM frequency
AF_DCMotor motor2(4, MOTOR12_1KHZ); // create motor #2, using M2 output, set to 1kHz PWM frequency

and same as you too i'm not familiar with this program for dc motor,, i've tried many dc motor program such a PLL or PWM mode (in the example program arduino AFmotor) and i tried to look some dc motor program in web too,, many of them using that program, but i'm really don't understand which is mean T_T