Please corect this code

There is no readPing() function in the latest code you posted

please tell me what change i should do in my code

[code]
#include <AFMotor.h>
#include <NewPing.h>
#include <Servo.h>



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

AF_DCMotor motor1(1, MOTOR12_1KHZ);
AF_DCMotor motor2(3, MOTOR12_1KHZ);
long distance ;

Servo myservo;
void setup() {
  myservo.attach(9);
  myservo.write(115);
  delay(2000);
  distance = readPing();
  delay(100);
  distance = readPing();
  delay(100);
  distance = readPing();
  delay(100);
  distance = readPing();
  delay(100); // put your setup code here, to run once:

}

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();

}// put your main code here, to run repeatedly:

}
[/code]

Hi,

You need to look at the NewPing examples in the IDE.
Select “file”, then “Examples”, then “NewPing”, there are some basic examples there for you to look and try.

Is this a school/college/university project/assignment?

Tom… :grinning: :+1: :coffee: :australia:

school

Greetings,

 #include <NewPing.h>

 #define TRIGGER_PIN 12

 #define ECHO_PIN 11

 #define MAX_DISTANCE 200

 NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE);

 void setup() {

 Serial.begin(115200);

 }

 void loop() {

 delay(50);

 Serial.print("Ping: ");

 Serial.print(sonar.ping_cm());

 Serial.println("cm");

 }

Maybe this example will help you understanding the newPing library.

I don’t know how you copied that code or where from but you seem to have included line numbers

Please follow the advice given in the link below when posting code

what is line numbers

please tell me what change i have to do in my code

@ Burakimportant
Thanks for updating your post using code tags

Do you see how much better it is ?

Thanks for the advice,

It is more readable now.

what should i do to my code please tell me

Greetings,
You have to create an object before void setup.
NewPing sonar(TRIG_PIN, ECHO_PIN , MAX_DISTANCE );

Then you should change all “readPing()” with “sonar.ping_cm()”.

thanks another error came

Greetings,

Can you post the error?

[code]
#include <AFMotor.h>
#include <NewPing.h>
#include <Servo.h>



#define TRIG_PIN A4 
#define ECHO_PIN A5 
#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(3, MOTOR12_1KHZ);
long distance ;
Servo myservo;
void setup() {
 myservo.attach(9);  
  myservo.write(115); 
  delay(2000);
  distance = sonar.ping_cm();
  delay(100);
  distance = sonar.ping_cm();
  delay(100);
  distance = sonar.ping_cm();
  delay(100);
  distance = sonar.ping_cm();
  delay(100); // put your setup code here, to run once:

}

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 = sonar.ping_cm();
}// put your main code here, to run repeatedly:

}
[/code]

i put like this where the error is. it says ‘moveStop’ was not declared in this scope

Greetings

Please look at the example of “AFMotor” library. Library examples are great don’t try to create your own functions. :slight_smile:

i checked i downloaded afmotor library

please tell me what i should change in my code

[code]
#include <AFMotor.h>
#include <NewPing.h>
#include <Servo.h>



#define TRIG_PIN A4 
#define ECHO_PIN A5 
#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(3, MOTOR12_1KHZ);
long distance ;
Servo myservo;
void setup() {
 myservo.attach(9);  
  myservo.write(115); 
  delay(2000);
  distance = sonar.ping_cm();
  delay(100);
  distance = sonar.ping_cm();
  delay(100);
  distance = sonar.ping_cm();
  delay(100);
  distance = sonar.ping_cm();
  delay(100); // put your setup code here, to run once:

}

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 = sonar.ping_cm();
}// put your main code here, to run repeatedly:

}
[/code]type or paste code here

Greetings,

// Adafruit Motor shield library
// copyright Adafruit Industries LLC, 2009
// this code is public domain, enjoy!

#include <AFMotor.h>

AF_DCMotor motor(4);

void setup() {
  Serial.begin(9600);           // set up Serial library at 9600 bps
  Serial.println("Motor test!");

  // turn on motor
  motor.setSpeed(200);
 
  motor.run(RELEASE);
}

void loop() {
  uint8_t i;
  
  Serial.print("tick");
  
  motor.run(FORWARD);
  for (i=0; i<255; i++) {
    motor.setSpeed(i);  
    delay(10);
 }
 
  for (i=255; i!=0; i--) {
    motor.setSpeed(i);  
    delay(10);
 }
  
  Serial.print("tock");

  motor.run(BACKWARD);
  for (i=0; i<255; i++) {
    motor.setSpeed(i);  
    delay(10);
 }
 
  for (i=255; i!=0; i--) {
    motor.setSpeed(i);  
    delay(10);
 }
  

  Serial.print("tech");
  motor.run(RELEASE);
  delay(1000);
}

Maybe this will help.