'readPing' was not declared in this scope in my Arduino code

Hi! Can you please help me with my code? I attach a copy below.

type or paste code here#include <AFMotor.h>  // AFMotor Library https://learn.adafruit.com/adafruit-motor-shield/library-install 
#include <NewPing.h>  // NewPing Library https://github.com/livetronic/Arduino-NewPing
#include <Servo.h>// Servo Library https://github.com/arduino-libraries/Servo.git
#include "pitches.h"

#define TRIG_PIN A0
#define ECHO_PIN A1
#define MAX_DISTANCE 200
#define MAX_SPEED 150
#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);
Servo myservo;

boolean goesForward = false;
int distance = 100;
int speedSet = 0;
int melody[] = {NOTE_C4, NOTE_G3, NOTE_A3, NOTE_G3, 0 , NOTE_B3, NOTE_C4};
int noteDurations[] = {4, 8, 8, 4, 4, 4, 4, 4};
int pirSensor = 2;

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);
 pinMode(pirSensor, INPUT);
}

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

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

 for (int thisNote = 0; thisNote < 13; thisNote++) {

   int noteDuration = 1000 / noteDuration [thisNote];
   tone(13, melody [thisNote], noteDuration);

   int pauseBetweenNotes = noteDuration * 1.30;
   delay(pauseBetweenNotes);

   noTone(13);

   if (distanceR >= distanceL)
   {
     turnRight();
     moveStop();
   } else
   {
     turnLeft();
     moveStop();
   }
 }
 {
   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);
}

void moveForward() {

 if (!goesForward)
 {
   goesForward = true;
   motor1.run(FORWARD);
   motor2.run(FORWARD);
   for (speedSet = 0; speedSet < MAX_SPEED; speedSet += 2)
   {
     motor1.setSpeed(speedSet);
     motor2.setSpeed(speedSet);
     delay(5);
   }
 }
}

void moveBackward() {
 goesForward = false;
 motor1.run(BACKWARD);
 motor2.run(BACKWARD);
 for (speedSet = 0; speedSet < MAX_SPEED; speedSet += 2)
 {
   motor1.setSpeed(speedSet);
   motor2.setSpeed(speedSet);
   delay(5);
 }
}

void turnRight() {
 motor1.run(FORWARD);
 motor2.run(BACKWARD);
 delay(450);
 motor1.run(FORWARD);
 motor2.run(FORWARD);
}

void turnLeft() {
 motor1.run(BACKWARD);
 motor2.run(FORWARD);
 delay(450);
 motor1.run(FORWARD);
 motor2.run(FORWARD);

}
I got these error messages:

type or paste code here

'readPing' was not declared in this scope

I and new to Arduino and still learning. Can someone please edit the code to make it work. Any help appreciated. Thanks.
-Eli

Check all the "{ }" and see if they match up.
Paul

Hi @compacked

There's an extra "}" on line 89.

RV mineirin

Adding to above replies:

Use tools -> autoformat in the IDE; it will give proper indentations and it makes it easier to spot errors. After the autoformat, loop() looks like below.

void loop() {
  ...
  ...
}
}

You will notice the two } at the end that both start at the beginning of the line; that indicates your problem. The first one is the end of loop(), the second one is too much :wink:

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.