//Project developed by Alok Dubey
//Avaiable free to do the changes as per your need :)
//Date :16 June 2021 (v.13)
#include <Servo.h> // Include Servo Library
#include <NewPing.h> // Include Newping Library
// L298N Control Pins
const int LeftMotorForward = 4; // Black wire then Red Wire of Left Motor, combile black+black , Red+Red of Both Motor
const int LeftMotorBackward = 5;// Black wire then Red Wire of Left Motor, combile black+black , Red+Red of Both Motor
const int RightMotorForward = 6;// Red Wire then Black Wire of Right Motor, combile black+black , Red+Red of Both Motor
const int RightMotorBackward = 7;// Red Wire then Black Wire of Right Motor, combile black+black , Red+Red of Both Motor
const int Ena = 3; // To control Speed of Motor A
const int Enb = 11;// To control Speed of Motor B
const int buzzer = 10; //buzzer to arduino pin 10
const int BTState = 2; //STATE pin of Bluetooth (when bluetooth not connected it is LOW)
#define TRIGGER_PIN A1 // Arduino pin tied to trigger pin on the ultrasonic sensor.
#define ECHO_PIN A2 // Arduino pin tied to echo pin on the ultrasonic sensor.
#define MAX_DISTANCE 250 // Maximum distance we want to ping for (in centimeters). Maximum sensor distance is rated at 250cm.
#define obstacleF 12
#define backLED 13
#define obstaclePin 8 // IR Sensor OUT pin
Servo servo_motor; // Servo's name
NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE); // NewPing setup of pins and maximum distance.
int isObstacle = HIGH;//HIGH means No Obstacle
int isObstacleF = LOW;//LOW means CAR IS ON GROUND
int distance = 0 ;
int i, j = 0 ;
void setup()
{
// Set L298N Control Pins as Output
pinMode(RightMotorForward, OUTPUT);
pinMode(LeftMotorForward, OUTPUT);
pinMode(LeftMotorBackward, OUTPUT);
pinMode(RightMotorBackward, OUTPUT);
pinMode(Ena, OUTPUT);
pinMode(Enb, OUTPUT);
pinMode(buzzer, OUTPUT); // Set buzzer - pin 10 as an output
pinMode(obstacleF, INPUT);
pinMode(backLED, OUTPUT);
pinMode(BTState, INPUT);
pinMode(obstaclePin, INPUT);
// Keep Motor not moving initially.
digitalWrite(RightMotorForward, LOW);
digitalWrite(RightMotorBackward, LOW);
digitalWrite(LeftMotorForward, LOW);
digitalWrite(LeftMotorBackward, LOW);
digitalWrite(backLED, HIGH);
digitalWrite(buzzer, LOW);
analogWrite(Ena, 0);
analogWrite(Enb, 0);
servo_motor.attach(9); // Attachs the servo on pin 9 to servo object.
servo_motor.write(90); // Set at 90 degrees.
delay(2000); // Wait for 2000ms.
distance = readPing(); // Get Ping Distance.
delay(100); // Wait for 1ms.
distance = readPing();
delay(100);
Serial.begin(9600);
}
void loop()
{
if (digitalRead(BTState) == LOW)
{
int distanceRight = 0;
int distanceLeft = 0;
if (distance <= 45)//its in cm change as per your need
{
digitalWrite(buzzer, HIGH);
delay(100);
digitalWrite(buzzer, LOW);
moveStop();
moveBackward();
delay(300);
moveStop();
distanceRight = lookRight();
delay(500);
distanceLeft = lookLeft();
delay(500);
if (distanceRight > distanceLeft)
{
turnRight();
delay(600);
moveStop();
distance = readPing();
}
else if (distanceLeft > distanceRight)
{
turnLeft();
delay(600);
moveStop();
distance = readPing();
}
}
else
{
isObstacleF = digitalRead(obstacleF); // to avoide to fall where is no ground / or its farther then sensor set distance
if (isObstacleF == HIGH)
{
moveStop();
moveBackward();
delay(300);
if (distanceLeft > distanceRight)
{
turnLeft();
delay(500);
moveStop();
distance = readPing();
}
else
{
turnRight();
delay(500);
moveStop();
distance = readPing();
}
moveStop();
}
else
{
moveForward();
}
//reseting the variable after the operations
distance = readPing();
delay(100);
}
}
else
{
if (Serial.available())
{
moveStop();
char data = Serial.read();
Serial.println(data);
if (data == 'F')
{
distance = readPing(); // Get Ping Distance.
if (distance <= 45) //distance in cm change as per your need
{
moveStop();
moveBackward();
delay(100);
moveStop();
}
else
{
isObstacleF = digitalRead(obstacleF); //to avoide to fall where is no ground / or its farther then sensor set distance
if (isObstacleF == HIGH)
{
moveStop();
moveBackward();
delay(300);
moveStop();
}
else
{
moveForward();
}
}
}
if (data == 'B')
{
isObstacle = digitalRead(obstaclePin);
if (isObstacle == LOW)
{
moveStop();
moveForward();
delay(100);
moveStop();
}
else
{
moveBackward();
}
}
if (data == 'S')
{
moveStop();
}
if (data == 'L')
{
turnLeft();
}
if (data == 'R')
{
turnRight();
}
}
}
distance = readPing();
}
// Loop ends here
//--------------------Declaring Function below for Loop----------------------------------------------------------------------------------------------------
int lookRight() // Look Right Function for Servo Motor
{
servo_motor.write(180);
delay(500);
int distance = readPing();
delay(100);
servo_motor.write(90);
return distance;
}
int lookLeft() // Look Left Function for Servo Motor
{
servo_motor.write(0);
delay(500);
int distance = readPing();
delay(100);
servo_motor.write(90);
return distance;
}
int readPing() // Read Ping Function for Ultrasonic Sensor.
{
delay(40); // Wait 40ms between pings (about 20 pings/sec). 29ms should be the shortest delay between pings.
int cm = sonar.ping_cm(); //Send ping, get ping distance in centimeters (cm).
if (cm == 0)
{
cm = 250;
}
return cm;
}
void moveStop() // Move Stop Function for Motor Driver.
{
digitalWrite(RightMotorForward, LOW);
digitalWrite(RightMotorBackward, LOW);
digitalWrite(LeftMotorForward, LOW);
digitalWrite(LeftMotorBackward, LOW);
}
void moveForward() // Move Forward Function for Motor Driver.
{
for (i = 0; i <= 90; i ++)
{
analogWrite(Ena, i);
analogWrite(Enb, i);
digitalWrite(RightMotorForward, HIGH);
digitalWrite(RightMotorBackward, LOW);
digitalWrite(LeftMotorForward, HIGH);
digitalWrite(LeftMotorBackward, LOW);
}
}
void moveBackward() // Move Backward Function for Motor Driver.
{
for (j = 0; j <= 100; j ++)
{
analogWrite(Ena, j);
analogWrite(Enb, j);
digitalWrite(RightMotorForward, LOW);
digitalWrite(RightMotorBackward, HIGH);
digitalWrite(LeftMotorForward, LOW);
digitalWrite(LeftMotorBackward, HIGH);
}
}
void turnRight() // Turn Right Function for Motor Driver.
{
digitalWrite(RightMotorForward, LOW);
digitalWrite(RightMotorBackward, HIGH);
digitalWrite(LeftMotorForward, HIGH);
digitalWrite(LeftMotorBackward, LOW);
}
void turnLeft() // Turn Left Function for Motor Driver.
{
digitalWrite(RightMotorForward, HIGH);
digitalWrite(RightMotorBackward, LOW);
digitalWrite(LeftMotorForward, LOW);
digitalWrite(LeftMotorBackward, HIGH);
}
Comments