Project showcase
Obstacle Avoiding Robot Car

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.

  • 4,003 views
  • 3 comments
  • 15 respects

Components and supplies

About this project

This project is based on a smart obstacle avoiding car with as low a cost as possible. Here I do not use an Arduino motor shield (Adafruit motor shield), instead of this I make my own motor driver with a shift register and l293d. If you want to learn how to build this, then watch this video.

Code

Obstacle avoiding robo car codeC/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);
      
      
  }
}

Comments

Similar projects you might like

Project 1: 2WD Obstacle Avoiding Robot

Project showcase by HDA Robotics

  • 7,528 views
  • 6 comments
  • 17 respects

Obstacle Avoiding Car

Project showcase by Team Adam

  • 5,744 views
  • 1 comment
  • 18 respects

Ground-Based Obstacle Avoiding Robot

Project showcase by Xavier Tan

  • 5,312 views
  • 1 comment
  • 20 respects

How to Make Obstacle Avoiding Robot

Project tutorial by Darwin Dela Cruz

  • 25,343 views
  • 9 comments
  • 37 respects

Bluetooth Controlled & Obstacle Avoiding Arduino Robot

Project showcase by KureBas Robotics

  • 8,435 views
  • 11 comments
  • 20 respects
Add projectSign up / Login