Robojax

Elegoo Robot Car Video #4 Avoid obstacle and stop. Follow obstacle in stratight path

دروس آردوینو به فارسی

Elegoo Robot Car Video #4 Avoid obstacle and stop. Follow obstacle in stratight path

In this video we learn how move Elegoo Robot Car to Stop when approaching obstacle. 2nd Code is to follow osbstacle in straigh path.

We have 3 sketchs

Resources for this sketch

Code #1 measure distance.

Open the sketch by clicking on File->Examples->New Ping->NewPing Example

Code #2 When Robot car approaches obstacle, Stops


 /**************
 *  Elegoo Smart Robot Car 
 *  
 *  This is the sketch to stop the car when reached obstacle at front of car
 *  
 *  Code updated by Ahmad Shamshiri on Friday Oct Nov30, 2019, in Ajax, Ontario, Canada
 *  
 * Watch video instruction for this code :https://youtu.be/wtyw-Q_AlB0
 * 
 * Original code can be obtained from: www.elegoo.com
 * 
 *  
 * Get this code and other Arduino codes from Robojax.com
Learn Arduino step by step in structured course with all material, wiring diagram and library
all in once place. Purchase My course on Udemy.com http://robojax.com/L/?id=62

****************************
Get early access to my videos via Patreon and have  your name mentioned at end of very 
videos I publish on YouTube here: http://robojax.com/L/?id=63 (watch until end of this video to list of my Patrons)
****************************

or make donation using PayPal http://robojax.com/L/?id=64

 *  * This code is "AS IS" without warranty or liability. Free to be used as long as you keep this note intact.* 
 * This code has been download from Robojax.com
    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <https://www.gnu.org/licenses/>.
 */


//*** start of ultrasonic sensor settings
#include <NewPing.h>
int Echo = A4;  
int Trig = A5; 
#define MAX_DISTANCE 200
const int distanceToAvoid = 20;//in cm
int rightDistance = 0, leftDistance = 0, middleDistance = 0;
NewPing sonar(Trig, Echo, MAX_DISTANCE); // NewPing setup of pins and maximum distance.
//***** dend of ultrasonic sensor settings

#include <Servo.h>  //servo library
Servo myservo;      // create servo object to control servo

#define ENA 5
#define ENB 6
#define IN1 7
#define IN2 8
#define IN3 9
#define IN4 11
#define carSpeed 100


void forward(){ 
  analogWrite(ENA, carSpeed);
  analogWrite(ENB, carSpeed);
  digitalWrite(IN1, HIGH);
  digitalWrite(IN2, LOW);
  digitalWrite(IN3, LOW);
  digitalWrite(IN4, HIGH);
  Serial.println("Forward");
}

void back() {
  analogWrite(ENA, carSpeed);
  analogWrite(ENB, carSpeed);
  digitalWrite(IN1, LOW);
  digitalWrite(IN2, HIGH);
  digitalWrite(IN3, HIGH);
  digitalWrite(IN4, LOW);
  Serial.println("Back");
}

void stop() {
  digitalWrite(ENA, LOW);
  digitalWrite(ENB, LOW);
  Serial.println("Stop!");
} 



void setup() { 
  myservo.attach(3);  // attach servo on pin 3 to servo object
  myservo.write(80);  //setservo position according to scaled value  
  Serial.begin(9600);     
  pinMode(IN1, OUTPUT);
  pinMode(IN2, OUTPUT);
  pinMode(IN3, OUTPUT);
  pinMode(IN4, OUTPUT);
  pinMode(ENA, OUTPUT);
  pinMode(ENB, OUTPUT);
  stop();
} 

void loop() { 
 
    middleDistance = getDistance();
      Serial.print("Distance:"); 
      Serial.print(middleDistance);
      Serial.println("cm");  
    if(middleDistance <= distanceToAvoid) {     
      stop();
      delay(2000);                               
    }
    else {
        forward();
    }                    
}//loop end

/*
 * gets corrected distance using NewPin library
 * the library returns 0 when distance higher than MAX_DISTANCE
 * so fixing it to never return zero becuase even if obstacle is
 * set in contact with sensor, it doesn't return zero.
 * Written by Ahmad Shamshiri on Nov 16, 2019 at 16:33 in Ajax, Ontario, Canada
 * www.Robojax.com
 */
int getDistance()
{
  int d =  sonar.ping_cm();
  if(d ==0)
  {
    return MAX_DISTANCE;
  }else{
    return d;
  }
}//getDistance() end
   

Code #3 Robot Car Follows Obstacles forward and backwards (see video)


 /**************
 *  Elegoo Smart Robot Car 
 *  
 *  This is the sketch to stop the car when reached obstacle at front of car and
 *  if obstacle comes coloser to the robot, the robot should go backwards
 *  
 *  Written by Ahmad Shamshiri on Friday Oct Nov 30, 2019, in Ajax, Ontario, Canada
 *  
 * Watch video instruction for this code :https://youtu.be/wtyw-Q_AlB0
 * 
 * Original code can be obtained from: www.elegoo.com
 * 
 *  
 * Get this code and other Arduino codes from Robojax.com
Learn Arduino step by step in structured course with all material, wiring diagram and library
all in once place. Purchase My course on Udemy.com http://robojax.com/L/?id=62

****************************
Get early access to my videos via Patreon and have  your name mentioned at end of very 
videos I publish on YouTube here: http://robojax.com/L/?id=63 (watch until end of this video to list of my Patrons)
****************************

or make donation using PayPal http://robojax.com/L/?id=64

 *  * This code is "AS IS" without warranty or liability. Free to be used as long as you keep this note intact.* 
 * This code has been download from Robojax.com
    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <https://www.gnu.org/licenses/>.
 */


//*** start of ultrasonic sensor settings
#include <NewPing.h>
int Echo = A4;  
int Trig = A5; 
#define MAX_DISTANCE 200
const int distanceToAvoid = 20;//cm
const int distanceGoBack = 10;//cm
int rightDistance = 0, leftDistance = 0, middleDistance = 0;

NewPing sonar(Trig, Echo, MAX_DISTANCE); // NewPing setup of pins and maximum distance.
//***** dend of ultrasonic sensor settings

#include <Servo.h>  //servo library
Servo myservo;      // create servo object to control servo

#define ENA 5
#define ENB 6
#define IN1 7
#define IN2 8
#define IN3 9
#define IN4 11
#define carSpeed 100
int getDistance();


void forward(){ 
  analogWrite(ENA, carSpeed);
  analogWrite(ENB, carSpeed);
  digitalWrite(IN1, HIGH);
  digitalWrite(IN2, LOW);
  digitalWrite(IN3, LOW);
  digitalWrite(IN4, HIGH);
  Serial.println("Forward");
}

void back() {
  analogWrite(ENA, carSpeed);
  analogWrite(ENB, carSpeed);
  digitalWrite(IN1, LOW);
  digitalWrite(IN2, HIGH);
  digitalWrite(IN3, HIGH);
  digitalWrite(IN4, LOW);
  Serial.println("Back");
}

void stop() {
  digitalWrite(ENA, LOW);
  digitalWrite(ENB, LOW);
  Serial.println("Stop!");
} 



void setup() { 
  myservo.attach(3);  // attach servo on pin 3 to servo object
  myservo.write(80);  //setservo position according to scaled value    
  Serial.begin(9600);     
  pinMode(IN1, OUTPUT);
  pinMode(IN2, OUTPUT);
  pinMode(IN3, OUTPUT);
  pinMode(IN4, OUTPUT);
  pinMode(ENA, OUTPUT);
  pinMode(ENB, OUTPUT);
  stop();
} 

void loop() { 
  delay(100);
    middleDistance = getDistance();
      Serial.print("Distance:"); 
      Serial.print(middleDistance);
      Serial.println("cm");      
  if(middleDistance < distanceToAvoid && middleDistance > distanceGoBack ) 
  {  
      stop();
      delay(1000); 

  }else if(middleDistance < distanceGoBack)
  {
         back();    
  }
    else{

      forward();
  }   

                 
}

/*
 * gets corrected distance using NewPin library
 * the library returns 0 when distance higher than MAX_DISTANCE
 * so fixing it to never return zero becuase even if obstacle is
 * set in contact with sensor, it doesn't return zero.
 * Written by Ahmad Shamshiri on Nov 16, 2019 at 16:33 in Ajax, Ontario, Canada
 * www.Robojax.com
 */
int getDistance()
{
  int d =  sonar.ping_cm();
  if(d ==0)
  {
    return MAX_DISTANCE;
  }else{
    return d;
  }
}//getDistance() end