Greetings,
You didn't create a variable that is named distance.
Try writing [quote="kishan1234_arduino, post:20, topic:857329, full:true"]
[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);
int distance; // You have to declarethe variable type before you use it.
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]type or paste code here
[/quote]
i did not understannd what should i do again please reply
Hi,
You need to create your "distance" variable.
Add to your code;
long distance ;
Put this BEFORE setup(), it tells the controller to make a memory space called "distance".
If you don't, when you use "distance" like in your code, the controller does not know what or where "distance" is stored in memory.
Tom...
thanks but another error came
[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:
}
[distance = readPing here there is an error
here/code]type or paste code here
readPing was not declared in this scope
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...
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
See also FAQ - Arduino Forum for general rules on forum behaviour and etiquette.
Hello,
Welcome to the Arduino Forum.
This guide explains how to get the best out of this forum. Please read and follow the instructions below.
Being new here you might think this is having rules for the sake of rules, but that is not the case. If you don’t follow the guidelines all that happens is there is a long exchange of posts while we try to get you to tell us what we need in order to help you, which is fru…
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 ?
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