Hi all
Not sure why i can only post the this area but here goes
I am not sure why I get this message
'readPing' was not declared in this scope
see attached code. I have used this code before.
Any help would be great, I am still new to this.
//Start for Skid Steer Tank project
#include <Wire.h>
#include <Adafruit_MotorShield.h>
#include <NewPing.h>
#include <Servo.h>
#define TRIG_PIN A1
#define ECHO_PIN A2
#define MAX_DISTANCE 100
const int FLIGHT_Pin = A3;
const int RLIGHT_Pin = A4;
const int LLIGHT_Pin = A5;
Adafruit_MotorShield AFMS = Adafruit_MotorShield(); // Create the Motor shield object with the default I2C address
Adafruit_DCMotor *myRightMotor = AFMS.getMotor(1); // Select which 'port' M1, M2, M3 or M4. In this case, M1
Adafruit_DCMotor *myLeftMotor = AFMS.getMotor(4);
NewPing sonar(TRIG_PIN, ECHO_PIN, MAX_DISTANCE);
Servo myservo;
int distance = 200;
// int readPing();
void setup() {
AFMS.begin(); // create with the default frequency 1.6KHz
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() {
myRightMotor->run(FORWARD);
{
myRightMotor->setSpeed(200);
delay(1000);
}
myRightMotor->run(BACKWARD);
{
myRightMotor->setSpeed(150);
delay(1000);
}
myRightMotor->run(RELEASE);
{
delay(1000);
}
// Left Motor Test
myLeftMotor->run(FORWARD);
{
myLeftMotor->setSpeed(200);
delay(1000);
}
myLeftMotor->run(BACKWARD);
{
myLeftMotor->setSpeed(150);
delay(1000);
}
myLeftMotor->run(RELEASE);
{
delay(1000);
}
}
There's a forward definition of the function here, but it's commented out. If you uncomment that, it will fix the error you have, but create another error because you will have a forward definition of a function but no full definition of that function.
I guess you lost part of it when you copied it over to this code.
Ok
now have uncommented " int readPing(); " above Setup and now get this error
" C:\Users\User\AppData\Local\Temp\ccG2ZC0q.ltrans0.ltrans.o: In function setup': C:\Users\User\Documents\Arduino\Skid_Steer_Tank_Start_2/Skid_Steer_Tank_Start_2.ino:34: undefined reference to readPing()'
collect2.exe: error: ld returned 1 exit status
Multiple libraries were found for "Servo.h"
Used: C:\Users\User\Documents\Arduino\libraries\Servo
Not used: C:\Users\User\AppData\Local\Arduino15\libraries\Servo
exit status 1
Compilation error: exit status 1 "
How do I fix " undifined reference to "readPing"
Sorry I am still trying to learn this coding.
What model Arduino are you using?
What do you expect;
readPing();
to do.
This is the NewPing example,
// ---------------------------------------------------------------------------
// Example NewPing library sketch that does a ping about 20 times per second.
// ---------------------------------------------------------------------------
#include <NewPing.h>
#define TRIGGER_PIN 12 // Arduino pin tied to trigger pin on the ultrasonic sensor.
#define ECHO_PIN 11 // Arduino pin tied to echo pin on the ultrasonic sensor.
#define MAX_DISTANCE 200 // Maximum distance we want to ping for (in centimeters). Maximum sensor distance is rated at 400-500cm.
NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE); // NewPing setup of pins and maximum distance.
void setup() {
Serial.begin(115200); // Open serial monitor at 115200 baud to see ping results.
}
void loop() {
delay(50); // Wait 50ms between pings (about 20 pings/sec). 29ms should be the shortest delay between pings.
Serial.print("Ping: ");
Serial.print(sonar.ping_cm()); // Send ping, get distance in cm and print result (0 = outside set distance range)
Serial.println("cm");
}
Hi Tom
this bit of code was used in an " Obstacle Avoiding Vehicle " I built and just copied all it over to the new code
I can not see any reference to " readPing " except in void Setup().
I am trying to read the distance info from the HC-SR04
I can still compile that code.
My IDE version is 2.3.2
hope this helps
Ian
This is the original code but was using an old V1 Motor board
This was for a 4 motor car I am now trying to build a 2 motor skidsteer tank.
This code still compiles on all my computers.
I can not see any other references to " readPing " anywhere.
Any more ideas
Thank you
#include <AFMotor.h>
#include <NewPing.h>
#include <Servo.h>
#define TRIG_PIN A1
#define ECHO_PIN A2
#define MAX_DISTANCE 100
#define MAX_SPEED 150 // sets speed of DC motors
#define RMAX_SPEED 150 // try to set Bacward speed higher to make it move back further
#define MAX_SPEED_OFFSET 20
const int FLIGHT_Pin = A3;
const int RLIGHT_Pin = A4;
const int LLIGHT_Pin = A5;
NewPing sonar(TRIG_PIN, ECHO_PIN, MAX_DISTANCE);
AF_DCMotor motor1(1, MOTOR12_64KHZ);
AF_DCMotor motor2(2, MOTOR12_64KHZ);
AF_DCMotor motor3(3, MOTOR34_64KHZ);
AF_DCMotor motor4(4, MOTOR34_64KHZ);
Servo myservo;
boolean goesForward=false;
int distance = 200; // change from 100 to 200 did not see much difference
int speedSet = 0;
void setup() {
pinMode(FLIGHT_Pin, OUTPUT);
pinMode (RLIGHT_Pin, OUTPUT);
pinMode(LLIGHT_Pin, OUTPUT);
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<=50) // change from 25 to 50 this makes it stop further away
{
moveStop();
delay(100);
moveBackward();
delay(400); // increase from 200 to 400 to try and get to move back further
moveStop();
delay(200);
distanceR = lookRight();
delay(200);
distanceL = lookLeft();
delay(200);
if(distanceR>=distanceL)
{
turnRight();
moveStop();
if(distanceR>=distanceL) // second look Rt makes it turn 180
{
turnRight();
moveStop();
}
}
else
{
turnLeft();
moveStop();
}
if(distanceL>=distanceR) // added second ckeck on turn Left
{
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(100);
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;
digitalWrite(A3, HIGH); // Turn ON Forward LED
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);
// digitalWrite(A3, LOW); // Turn OFF Forward LED
}
}
}
void moveBackward() {
goesForward=false;
motor1.run(BACKWARD);
motor2.run(BACKWARD);
motor3.run(BACKWARD);
motor4.run(BACKWARD);
for (speedSet = 0; speedSet < RMAX_SPEED; speedSet +=2) // Useing RMAX_SPEED slowly bring the speed up to avoid loading down the batteries too quickly works better
{
motor1.setSpeed(speedSet);
motor2.setSpeed(speedSet);
motor3.setSpeed(speedSet);
motor4.setSpeed(speedSet);
delay(5);
}
}
void turnRight() {
digitalWrite(A3, LOW);
digitalWrite(A4, HIGH); // Turn ON Right LED
motor1.run(FORWARD); // CHANGED TO FORWARD FOR STEERING TEST
motor2.run(BACKWARD);
motor3.run(FORWARD); // changed to FORWARD to try and make it move better
motor4.run(BACKWARD);
delay(500);
motor1.run(FORWARD);
motor2.run(FORWARD);
motor3.run(FORWARD);
motor4.run(FORWARD);
digitalWrite(A4, LOW); // Turn OFF Right LED
}
void turnLeft() {
digitalWrite(A3, LOW);
digitalWrite(A5, HIGH); // turn ON Left LED
motor1.run(BACKWARD); // CHANGED TO BACKWARD FOR STEERING TEST
motor2.run(FORWARD);
motor3.run(BACKWARD); // changed to BACKWARD to try and make it move better
motor4.run(FORWARD);
delay(500);
motor1.run(FORWARD);
motor2.run(FORWARD);
motor3.run(FORWARD);
motor4.run(FORWARD);
digitalWrite(A5, LOW); // turn OFF Left LED
}