CEASER:THE SMART ROBOT||OBSTACLE AVOIDANCE ROBOT || BLUETOOTH MODE & AUTONOMOUS MODE



DC MOTORS :- D5,D6,D9,D10
SERVOS :- D3,D4



OBSTACLE AVOIDANCE ROBOT:-

#include <Servo.h>

Servo LEFT_SERVO;
Servo RIGHT_SERVO;

int LMT1  = 5;//IN1
int LMT2  = 6;//IN2
int RMT1  = 9;//IN3
int RMT2  = 10;//IN4


#define left_echo A5//LEFT SENSORS
#define left_trig A4

#define center_echo A3//CENTER SENSORS
#define center_trig A2

#define right_echo A1//RIGHT SENSORS
#define right_trig A0


long center_distance;
long left_distance;
long right_distance;

long center_duration;
long left_duration;
long right_duration;


void setup() {
Serial.begin(9600);
  
  pinMode(LMT1, OUTPUT);
  pinMode(LMT2, OUTPUT);
  pinMode(RMT1, OUTPUT);
  pinMode(RMT2, OUTPUT);
    
pinMode(center_trig, OUTPUT);//CENTER SENSORS
pinMode(center_echo, INPUT);

pinMode(left_trig, OUTPUT);//LEFT SENSORS
pinMode(left_echo, INPUT);

pinMode(right_trig, OUTPUT);//RIGHT SENSORS
pinMode(right_echo, INPUT);

  LEFT_SERVO.attach(3);
  RIGHT_SERVO.attach(4);

  LEFT_SERVO.write(90);
  RIGHT_SERVO.write(90);
  delay(500);
  LEFT_SERVO.write(20);
  RIGHT_SERVO.write(150);
  delay(500);
  LEFT_SERVO.write(90);
  RIGHT_SERVO.write(90);
  delay(500);
  LEFT_SERVO.write(20);
  RIGHT_SERVO.write(150);
}

void loop() { 
center_ultrasonic();  
left_ultrasonic();
right_ultrasonic();

//Serial.println("******MKINVENTIONS***********************");
//Serial.print("Left");
//Serial.println(left_distance);
//Serial.print("Center");
//Serial.println(center_distance);
//Serial.print("Right");
//Serial.println(right_distance);
//delay(1000);

  
if(left_distance < 30){
right();
delay(500);
forward();
}
else if(right_distance < 30){
left();
delay(500);
forward();
}
else if(center_distance < 30){
stp();
delay(500);
backward();
delay(1000);
left();
delay(500); 
}

else if(center_distance < 30 && left_distance < 30){
forwardright();
delay(500);
forward();
}
else if(center_distance < 30 && right_distance < 30){
forwardleft();
delay(500);
forward();
}
if(center_distance < 30 && right_distance < 30 && left_distance < 30){
stp();
delay(500);
backward();
delay(1000);
left();
delay(500);
}
else{
 forward();
}

 }



void center_ultrasonic(){//center SENSORS
  digitalWrite(center_trig, LOW);
  delay(2);
  digitalWrite(center_trig, HIGH);
  delay(10);
  digitalWrite(center_trig, LOW);

  center_duration = pulseIn(center_echo, HIGH);
  center_distance = center_duration * 0.034/2;
}

void left_ultrasonic(){//LEFT SENSORS
  digitalWrite(left_trig, LOW);
  delay(2);
  digitalWrite(left_trig, HIGH);
  delay(10);
  digitalWrite(left_trig, LOW);

  left_duration = pulseIn(left_echo, HIGH);
  left_distance = left_duration * 0.034/2;
}

void right_ultrasonic(){//RIGHT SENSORS
  digitalWrite(right_trig, LOW);
  delay(2);
  digitalWrite(right_trig, HIGH);
  delay(10);
  digitalWrite(right_trig, LOW);

  right_duration = pulseIn(right_echo, HIGH);
  right_distance = right_duration * 0.034/2;
}


void forward(){
  analogWrite(LMT1, 255);
  analogWrite(LMT2, 0);
  analogWrite(RMT1, 255);
  analogWrite(RMT2, 0);
}



void backward(){
  analogWrite(LMT1, 0);
  analogWrite(LMT2, 255);
  analogWrite(RMT1, 0);
  analogWrite(RMT2, 255);
}


void left(){
  analogWrite(LMT1, 0);
  analogWrite(LMT2, 255);
  analogWrite(RMT1, 255);
  analogWrite(RMT2, 0);
}

void right(){
  analogWrite(LMT1, 255);
  analogWrite(LMT2, 0);
  analogWrite(RMT1, 0);
  analogWrite(RMT2, 255);
}

void stp(){
  analogWrite(LMT1, 0);
  analogWrite(LMT2, 0);
  analogWrite(RMT1, 0);
  analogWrite(RMT2, 0);
}

void forwardright(){
  analogWrite(LMT1, 255);
  analogWrite(LMT2, 0);
  analogWrite(RMT1, 0);
  analogWrite(RMT2, 0);
}

void forwardleft(){
  analogWrite(LMT1, 0);
  analogWrite(LMT2, 0);
  analogWrite(RMT1, 255);
  analogWrite(RMT2, 0);
}


BLUETOOTH CONTROL ROBOT:-

//BLUETOOTH CONTROL CAR WITH L298N MOTOR DRIVER
///MKINVENTIONS MADHAN KUMAR CHIRUGURI
///FOL0 ME ON FACEBOOK AND INSTAGRAM
//MADHAN KUMAR CHIRUGURI

#include <Servo.h>

Servo LEFT_SERVO;
Servo RIGHT_SERVO;

int LMT1  = 5;//IN1
int LMT2  = 6;//IN2
int RMT1  = 9;//IN3
int RMT2  = 10;//IN4


char command; 

void setup() 
{      
  Serial.begin(9600);
   
  pinMode(LMT1, OUTPUT);
  pinMode(LMT2, OUTPUT);
  pinMode(RMT1, OUTPUT);
  pinMode(RMT2, OUTPUT);

  
  digitalWrite(LMT1, 0);
  digitalWrite(LMT2, 0);
  digitalWrite(RMT1, 0);
  digitalWrite(RMT2, 0);

  LEFT_SERVO.attach(3);
  RIGHT_SERVO.attach(4);
  
  LEFT_SERVO.write(20);
  RIGHT_SERVO.write(150);
}

void loop(){
 if(Serial.available() > 0){ 
    command = Serial.read(); 
    Serial.println(command); 

//FORWARD..............
if(command == 'F' ){
  forward();
}
//BACKWARD.............. 
else if(command == 'B' ){
  backward();
}
//LEFT..............
else if(command == 'L' ){
  left();
}
//RIGHT..............
else if(command == 'R' ){
  right();
}

//STOP..............
else{
  stp();
}



if(command == 'X' ){
  LEFT_SERVO.write(20);
  RIGHT_SERVO.write(150);
}
if(command == 'x' ){
  LEFT_SERVO.write(90);
  RIGHT_SERVO.write(90);
}

if(command == 'Y' ){
LEFT_SERVO.write(20);
}
if(command == 'y' ){
LEFT_SERVO.write(90);
}


if(command == 'Z' ){
 RIGHT_SERVO.write(150);
}
if(command == 'z' ){
RIGHT_SERVO.write(90);
}



 }

//forward();
//delay(2000);
//backward();
//delay(2000);
//left();
//delay(2000);
//right();
//delay(2000);
//stp();
//delay(2000);

}







void forward(){
  digitalWrite(LMT1, 255);
  digitalWrite(LMT2, 0);
  digitalWrite(RMT1, 255);
  digitalWrite(RMT2, 0);
}



void backward(){
  digitalWrite(LMT1, 0);
  digitalWrite(LMT2, 150);
  digitalWrite(RMT1, 0);
  digitalWrite(RMT2, 150);
}


void left(){
  digitalWrite(LMT1, 0);
  digitalWrite(LMT2, 255);
  digitalWrite(RMT1, 255);
  digitalWrite(RMT2, 0);
}

void right(){
  digitalWrite(LMT1, 255);
  digitalWrite(LMT2, 0);
  digitalWrite(RMT1, 0);
  digitalWrite(RMT2, 255);
}

void stp(){
  digitalWrite(LMT1, 0);
  digitalWrite(LMT2, 0);
  digitalWrite(RMT1, 0);
  digitalWrite(RMT2, 0);
}

void forwardright(){
  digitalWrite(LMT1, 255);
  digitalWrite(LMT2, 0);
  digitalWrite(RMT1, 0);
  digitalWrite(RMT2, 0);
}

void forwardleft(){
  digitalWrite(LMT1, 0);
  digitalWrite(LMT2, 0);
  digitalWrite(RMT1, 255);
  digitalWrite(RMT2, 0);
}



Post a Comment

0 Comments