readPing error showing in code. there are no extra {}

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

int IN1 = 8;
int IN2 = 9; 
int IN3 = 10;
int IN4 = 11;

//sensor pins
#define trig_pin A1 
#define echo_pin A0

#define maximum_distance 5
boolean goesForward = false;  
int distance = 100; 

NewPing sonar(trig_pin, echo_pin, maximum_distance) ;
Servo servo_motor; 
void setup() {
  pinMode(IN3, OUTPUT);
  pinMode(IN1, OUTPUT);
  pinMode(IN2, OUTPUT);
  pinMode(IN4, OUTPUT);

  servo_motor.attach(7);

  servo_motor.write(115);
  delay(2000);
  distance = readPing()
  delay(100);
  distance = readPing()
  delay(100);
  distance = readPing()
  delay(100);
  distance = readPing()
  delay(100);
}

void loop() {
  int distanceRight = 0;
  int distanceLeft = 0;
  delay(50);

  if (distance <= 20) {
    moveStop();
    delay(300);
    moveBackward();
    delay(400);
    moveStop();
    delay(300);
    distanceRight = lookRight();
    delay(300);
    distanceLeft =lookLeft();
    delay(300)

    if (distance >= distanceLeft) {
      turnRight();
      moveStop();
    }
    else{
      moveForward();
    }
      distance = readPing();
      
  }

  int lookRight() {
    servo_motor.write(50);
    delay(500);
    int distance = readPing();
    delay(100);
    servo_motor.write(115);
    return distance; 
  }
  int lookLeft(){
    servo_motor.write(170);
    delay(500);
    int distance = readPing();
    delay(100);
    servo_motor.write(115)
    return. distance;
    delay(100);
  }

  int readPing() {
    delay(70);
    int cm = sonar.ping_cm();
    if (cm==0) {
      cm=250;
    }
    return cm;
  }
  void moveStop() {

    digitalWrite(IN3, LOW);
    digitalWrite(IN1, LOW);
    digitalWrite(IN4, LOW);
    digitalWrite(IN2, LOW);

  }
  void moveForward() {
    if(!goesForward) {

      goesForward=true;

      digitalWrite(IN1, HIGH);
      digitalWrite(IN3, HIGH);
      
      digitalWrite(IN2, LOW);
      digitalWrite(IN4, LOW)
    }
  }
  void moveBackward() {
    
    goesForward=false;

    digitalWrite(IN2, HIGH);
    digitalWrite(IN4, HIGH);

    digitalWrite(IN1, LOW);
    digitalWrite(IN3, LOW);
  }

  void turnRight() {

    digitalWrite(IN1, HIGH);
    digitalWrite(IN4, HIGH);

    digitalWrite(IN2, LOW);
    digitalWrite(IN3, LOW);

  }

  void turnLeft() {
    digitalWrite(IN2, HIGH);
    digitalWrite(IN3, HIGH);

    digitalWrite(IN1, LOW);
    digitalWrite(IN4, LOW);

    delay(500);
    digitalWrite(IN1, HIGH);
    digitalWrite(IN3, HIGH);

    digitalWrite(IN2, LOW);
    digitalWrite(IN4, LOW);
    

  }


}

Welcome to the forum, @syed_umer.

Please share the error you are getting, or tell us what is happening that should not, or isn't that should.

We not mind readers over here.

a7

Welcome to the forum

Where is the brace at end of the loop() function ?

And this

  distance = readPing()
             delay(100);
  distance = readPing()
             delay(100);
  distance = readPing()
             delay(100);
  distance = readPing()

Missing semicolons ?

and again

    delay(300)

Once more, with feeling

  servo_motor.write(115)

Something different for a change
What is that full stop doing there ?

  return. distance;

An old friend

    digitalWrite(IN4, LOW)

Use tools -> autoformat and you will see what is wrong.

After autoformat, you can see that int lookRight() does not start at the beginng of a line. That indicates that something is wrong before that; as @UKHeliBob indicates, missing a } at the end of loop().

Function declarations / definitions should always start at the beginning of a line after an autoformat.

After you fix that, you will have one } too many at the end of your sketch.

Tools -> autoformat will also show you the missing semicolons mentioned above; e.g.

    delay(300)

      if (distance >= distanceLeft)

The additional indentation indicates that something is wrong.

Conclusion:
Always use tools -> autoformat to properly indent your code which in turn makes it easier to find issues.

the brace is at the end of the code. i fixed the semi colons but its still showing error

I am on macbook and there is no tools options showing for me, what can i do?

Show your fixed code.

If there are still two *}* at the end of the code, that is wrong.

This is for IDE2.0 on Windows, the Mac (regardless of IDE version) should show the same.
image

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

int IN1 = 8;
int IN2 = 9;
int IN3 = 10;
int IN4 = 11;

//sensor pins
#define trig_pin A1
#define echo_pin A0

#define maximum_distance 5
boolean goesForward = false;
int distance = 100;

NewPing sonar(trig_pin, echo_pin, maximum_distance) ;
Servo servo_motor;
void setup() {
pinMode(IN3, OUTPUT);
pinMode(IN1, OUTPUT);
pinMode(IN2, OUTPUT);
pinMode(IN4, OUTPUT);

servo_motor.attach(7);

servo_motor.write(115);
delay(2000);
distance = readPing() ;
delay(100);
distance = readPing() ;
delay(100);
distance = readPing() ;
delay(100);
distance = readPing() ;
delay(100);
}

void loop() {
int distanceRight = 0;
int distanceLeft = 0;
delay(50);

if (distance <= 20) {
moveStop();
delay(300);
moveBackward();
delay(400);
moveStop();
delay(300);
distanceRight = lookRight();
delay(300);
distanceLeft =lookLeft();
delay(300);

if (distance >= distanceLeft) {
  turnRight();
  moveStop();
}
else{
  moveForward();
}
  distance = readPing();

}

int lookRight() {
servo_motor.write(50);
delay(500);
int distance = readPing();
delay(100);
servo_motor.write(115);
return distance;
}
int lookLeft(){
servo_motor.write(170);
delay(500);
int distance = readPing();
delay(100);
servo_motor.write(115)
return distance;
delay(100);
}

int readPing() {
delay(70); }
int cm = sonar.ping_cm();
if (cm==0) {
cm=250;
}
return cm;
}
void moveStop() {

digitalWrite(IN3, LOW);
digitalWrite(IN1, LOW);
digitalWrite(IN4, LOW);
digitalWrite(IN2, LOW);

}
void moveForward() {
if(!goesForward) {

  goesForward=true;

  digitalWrite(IN1, HIGH);
  digitalWrite(IN3, HIGH);
  
  digitalWrite(IN2, LOW);
  digitalWrite(IN4, LOW);
}

}
void moveBackward() {

goesForward=false;

digitalWrite(IN2, HIGH);
digitalWrite(IN4, HIGH);

digitalWrite(IN1, LOW);
digitalWrite(IN3, LOW);

}

void turnRight() {

digitalWrite(IN1, HIGH);
digitalWrite(IN4, HIGH);

digitalWrite(IN2, LOW);
digitalWrite(IN3, LOW);

}

void turnLeft() {
digitalWrite(IN2, HIGH);
digitalWrite(IN3, HIGH);

digitalWrite(IN1, LOW);
digitalWrite(IN4, LOW);

delay(500);
digitalWrite(IN1, HIGH);
digitalWrite(IN3, HIGH);

digitalWrite(IN2, LOW);
digitalWrite(IN4, LOW);

}

}

Where are the code tags ??

void turnLeft()
{
  digitalWrite(IN2, HIGH);
  digitalWrite(IN3, HIGH);

  digitalWrite(IN1, LOW);
  digitalWrite(IN4, LOW);

  delay(500);
  digitalWrite(IN1, HIGH);
  digitalWrite(IN3, HIGH);

  digitalWrite(IN2, LOW);
  digitalWrite(IN4, LOW);
}
}

That last } does not belong there.

Other mistake

  int readPing()
  {
    delay(70);
  }

You're missing a } at the end of lookLeft(). And the } after delay(70) does not belong there.

Is it possible for you to correct all my mistakes and send it so i can test to see if it works? that would be a great help.

You have been given details of what is wrong. Why not fix them yourself ? If you still have problems afterwards then post your new code and the full error message here for more help

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