Aritra_B
Published

Obstacle Avoiding Robot Car

This smart car avoids obstacles: when the distance is less than 15 cm from itself, it will stop and make a decision where to move.

BeginnerShowcase (no instructions)14,708
Obstacle Avoiding Robot Car

Things used in this project

Story

Read more

Code

Obstacle avoiding robo car code

C/C++
understand the code and watch the video how it is works. Then modify this code and build a new one.
#include<Servo.h>
Servo myservo; // create a servo object
int redled=13; // declare the pins
int greenled=12;


int servopin=9;
int trigpin=6;
int echopin=5;
float distance;
float duration;
int datapin=7;
int clockpin=8;
int latchpin=3;
void setup()
{
 Serial.begin(9600); 
  pinMode(redled,OUTPUT); // declare the pinmode
  pinMode(greenled,OUTPUT);
  
  pinMode(servopin,OUTPUT);
  pinMode(trigpin,OUTPUT);
  pinMode(echopin,INPUT);
  pinMode(datapin,OUTPUT);
  pinMode(clockpin,OUTPUT);
  pinMode(latchpin,OUTPUT);
  myservo.attach(servopin); // declare servo object is connected to servo pin
  
}
int ping() // create a function
{
   digitalWrite(trigpin,LOW);
  delayMicroseconds(10);
  
  digitalWrite(trigpin,HIGH);
  delayMicroseconds(20);
 digitalWrite(trigpin,LOW);

   duration= pulseIn(echopin,HIGH);
   distance= duration*0.034/2; // distance is in cm
   return(distance);
   
 
}

void loop()
{ 
  myservo.write(90); // set servo at 90 degree angle
    float value;
    value=ping(); // call the ping function
Serial.println(value);
delay(100);
    if(value>20)
    {
      digitalWrite(redled,LOW);
      digitalWrite(greenled,HIGH);
      
      digitalWrite(latchpin,LOW);
      shiftOut(datapin,clockpin,LSBFIRST,B01010000); // move the car in forward direction
      digitalWrite(latchpin,HIGH);
    }
    if(value<20&& value>15)
    {
      digitalWrite(redled,HIGH);
      digitalWrite(greenled,HIGH);
      
      digitalWrite(latchpin,LOW);
      shiftOut(datapin,clockpin,LSBFIRST,B01010000);
      digitalWrite(latchpin,HIGH);
    }
    
    if(value<15 )
    {
       digitalWrite(latchpin,LOW);
      shiftOut(datapin,clockpin,LSBFIRST,B00000000); // stop the car
      digitalWrite(latchpin,HIGH);
      delay(1000);
       digitalWrite(latchpin,LOW);
      shiftOut(datapin,clockpin,LSBFIRST,B00101000); // first back the robo car
      digitalWrite(latchpin,HIGH);
      delay(1000);
      digitalWrite(latchpin,LOW);
      shiftOut(datapin,clockpin,LSBFIRST,B00000000); // then again stop 
      digitalWrite(latchpin,HIGH);
      delay(100);
      
      go(); // call go function
      delay(2000);
      
    }
    myservo.write(90);
}
void go()
{
  int a;
  int b;
  myservo.write(0);
  delay(1000);
  a=ping();
  myservo.write(180);
  delay(1000);
  b=ping();
  if(a>b)
  {
   digitalWrite(redled,HIGH);
      digitalWrite(greenled,LOW);
      
      digitalWrite(latchpin,LOW);
      shiftOut(datapin,clockpin,LSBFIRST,B00010000); // move to the right side
      digitalWrite(latchpin,HIGH);
     
      
  }
  else if(a<b)
  {
    digitalWrite(redled,HIGH);
      digitalWrite(greenled,LOW);
     
      digitalWrite(latchpin,LOW);
      shiftOut(datapin,clockpin,LSBFIRST,B01000000); // move to the left side
      digitalWrite(latchpin,HIGH);
      
  }
  else if(a=b)
  {
    digitalWrite(redled,HIGH);
      digitalWrite(greenled,LOW);
      
      digitalWrite(latchpin,LOW);
      shiftOut(datapin,clockpin,LSBFIRST,B00101000); // first back the robo car
      digitalWrite(latchpin,HIGH);
      delay(4000); // wait for 4 seconds
       digitalWrite(latchpin,LOW);
      shiftOut(datapin,clockpin,LSBFIRST,B01000000); // then move to right direction
      digitalWrite(latchpin,HIGH);
      delay(2000);
      
      
  }
}

Credits

Aritra_B
2 projects • 12 followers

Comments