#include <Servo.h>          //Servo motor library. This is standard library
#include <NewPing.h>        //Ultrasonic sensor function library. You must install this library

int vale;
int flag=0; 
int valeStop=0;


//our L298N control pins
const int LeftMotorForward = 7;
const int LeftMotorBackward = 6;
const int RightMotorForward = 4;
const int RightMotorBackward = 5;



//sensor pins
#define trig_pin A1 //analog input 1
#define echo_pin A2 //analog input 2
// defines variables
 long duration;
 int dist;


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

NewPing sonar(trig_pin, echo_pin, maximum_distance); //sensor function
Servo servo_motor; //our servo name












void setup(){


pinMode(trig_pin, OUTPUT); // Sets the trigPin as an Output
pinMode(echo_pin, INPUT); // Sets the echoPin as an Input
Serial.begin(9600); // Starts the serial communication


  pinMode(RightMotorForward, OUTPUT);
  pinMode(LeftMotorForward, OUTPUT);
  pinMode(LeftMotorBackward, OUTPUT);
  pinMode(RightMotorBackward, OUTPUT);

  servo_motor.attach(10); //our servo pin

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

void loop(){





  if(Serial.available()){  //id data is available to read

    char val = Serial.read();

    if (val == 'F') {

        digitalWrite(RightMotorForward, HIGH);
        digitalWrite(RightMotorBackward, LOW);

        digitalWrite(LeftMotorBackward, LOW);
        digitalWrite(LeftMotorForward, HIGH);
        if(flag == 0){
          Serial.println("Go Forward!");
          flag=1;
        }
    }

    // if the vale is 'R' the motor will turn left
    else if (val == 'R') {
        digitalWrite(LeftMotorBackward, HIGH);
        digitalWrite(RightMotorForward, HIGH);

        digitalWrite(LeftMotorForward, LOW);
        digitalWrite(RightMotorBackward, LOW);
        if(flag == 0){
          Serial.println("Turn LEFT");
          flag=1;
        }
        delay(1500);
        vale=3;
        valeStop=1;
    }
    // if the vale is 'S' the motor will Stop
    else if (val == 'S' || valeStop == 1) {
        digitalWrite(RightMotorForward, LOW); 
        digitalWrite(RightMotorBackward, LOW); 
        digitalWrite(LeftMotorForward, LOW);
        digitalWrite(LeftMotorBackward, LOW);
        if(flag == 0){
          Serial.println("STOP!");
          flag=1;
        }
        valeStop=0;
    }
    // if the vale is 'L' the motor will turn right
    else if (val == 'L') {

        digitalWrite(LeftMotorForward, HIGH);
        digitalWrite(RightMotorBackward, HIGH);

        digitalWrite(LeftMotorBackward, LOW);
        digitalWrite(RightMotorForward, LOW);
        if(flag == 0){
          Serial.println("Turn RIGHT");
          flag=1;
        }
        delay(1500);
        vale=3;
        valeStop=1;
    }
    // if the vale is 'B' the motor will Reverse
    else if (val == 'B') {

        digitalWrite(LeftMotorBackward, HIGH);
        digitalWrite(RightMotorBackward, HIGH);

        digitalWrite(LeftMotorForward, LOW);
        digitalWrite(RightMotorForward, LOW);
        if(flag == 0){
          Serial.println("Reverse!");
          flag=1;
        }

    }


// Clears the trigPin
digitalWrite(trig_pin, LOW);
delayMicroseconds(2);
// Sets the trigPin on HIGH state for 10 micro seconds
digitalWrite( trig_pin, HIGH);
delayMicroseconds(10);
digitalWrite(trig_pin, LOW);
// Reads the echoPin, returns the sound wave travel time in microseconds
duration = pulseIn(echo_pin, HIGH);
// Calculating the distance
dist= duration*0.034/2;
// Prints the distance on the Serial Monitor
Serial.print("Distance: ");
Serial.println(dist);

    }
                                                                }




  else{

  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{
      turnLeft();
      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(RightMotorForward, LOW);
  digitalWrite(LeftMotorForward, LOW);
  digitalWrite(RightMotorBackward, LOW);
  digitalWrite(LeftMotorBackward, LOW);
}

void moveForward(){

  if(!goesForward){

    goesForward=true;

    digitalWrite(LeftMotorForward, HIGH);
    digitalWrite(RightMotorForward, HIGH);

    digitalWrite(LeftMotorBackward, LOW);
    digitalWrite(RightMotorBackward, LOW); 
  }
}

void moveBackward(){

  goesForward=false;

  digitalWrite(LeftMotorBackward, HIGH);
  digitalWrite(RightMotorBackward, HIGH);

  digitalWrite(LeftMotorForward, LOW);
  digitalWrite(RightMotorForward, LOW);

}

void turnRight(){

  digitalWrite(LeftMotorForward, HIGH);
  digitalWrite(RightMotorBackward, HIGH);

  digitalWrite(LeftMotorBackward, LOW);
  digitalWrite(RightMotorForward, LOW);

  delay(500);

  digitalWrite(LeftMotorForward, HIGH);
  digitalWrite(RightMotorForward, HIGH);

  digitalWrite(LeftMotorBackward, LOW);
  digitalWrite(RightMotorBackward, LOW);



}

void turnLeft(){

  digitalWrite(LeftMotorBackward, HIGH);
  digitalWrite(RightMotorForward, HIGH);

  digitalWrite(LeftMotorForward, LOW);
  digitalWrite(RightMotorBackward, LOW);

  delay(500);

  digitalWrite(LeftMotorForward, HIGH);
  digitalWrite(RightMotorForward, HIGH);

  digitalWrite(LeftMotorBackward, LOW);
  digitalWrite(RightMotorBackward, LOW);
}

 }

closed as unclear what you're asking by Greenonline, per1234, Juraj, MatsK, sempaiscuba Dec 4 at 10:55

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

  • Welcome to Arduino:SE. What is your question? Did you install the Ultrasonic sensor function library? You might find it helpful to review the site tour and Help Centre and, in particular, How to Ask. Additionally, this post on our Meta site may help you when you edit your question: How to ask a good question for Arduino Stack Exchange. – sempaiscuba Dec 4 at 0:54
  • It's exactly, what it says. You haven't defined the function readping(). Use sonar.ping_cm() instead – chrisl Dec 4 at 6:47
  • 1
    As with so many errors of this type: If you fix your indenting you will see where the problem lies. – Majenko Dec 4 at 11:00

Browse other questions tagged or ask your own question.