Arduino simple code for Obstacle Avoiding Robot & Mobile Control Robot

Obstacle Avoiding Robot

An obstacle avoiding robot is a fully autonomous robot which can be able to avoid any obstacle which it face when it move. Simply, when it met an obstacle while it moving forward, automatically stop moving forward and makes a step back.

Source Code :

float d,t;
void setup() {
Serial.begin(9600);
pinMode(2,OUTPUT);
pinMode(3,OUTPUT);
pinMode(4,OUTPUT);
pinMode(5,OUTPUT);
pinMode(A0,OUTPUT);
pinMode(A1,INPUT);
  
  
}

void loop() {
  distance();
  
  
//forward();
//backward();
//right();
//left();
  

}
void distance(){


  digitalWrite(A0,HIGH);
  delay(10);
  digitalWrite(A0,LOW);
  t=pulseIn(A1,HIGH);
  d=0.0343*(t/2);
  Serial.println(d);

  if(d>30){

    forward();
    
  }
  else if(d<30 && d>20){

    right();
  }
  else if(d<20 && d>10){

    left();
  }
  else if (d<10){

    backward();
  }
  else{
    forward();  
  }
  
}

void forward(){

  digitalWrite(2,HIGH);
 digitalWrite(3,LOW);
 digitalWrite(4,HIGH);
 digitalWrite(5,LOW); 
}
void backward(){
  digitalWrite(2, LOW);
 digitalWrite(3,HIGH);
 digitalWrite(4,LOW);
 digitalWrite(5,HIGH); 
  
  
}
void right(){
  digitalWrite(2, LOW);
 digitalWrite(3,LOW);
 digitalWrite(4,HIGH);
 digitalWrite(5,LOW); 
}
void left(){
digitalWrite(2, HIGH);
 digitalWrite(3,LOW);
 digitalWrite(4,LOW);
 digitalWrite(5,LOW); 
  
}

Mobile Control Robot

Android controlled robot project makes use of an Android mobile phone for robotic control with the help of Bluetooth technology. This is a simple robotics project using a microcontroller.

Source Code :

//float d,t;
char b;
void setup() {
Serial.begin(9600);
Serial1.begin(9600);
pinMode(2,OUTPUT);
pinMode(3,OUTPUT);
pinMode(4,OUTPUT);
pinMode(5,OUTPUT);
pinMode(A0,OUTPUT);
pinMode(A1,INPUT);
  
  
}

void loop() {
  
  mc();

}

void mc(){
  if(Serial1.available()){
    b=Serial1.read();
    Serial.println(b);
    if(b=='F'){

      forward();
    }
    else if(b=='R'){

      right();
    }
    else if(b=='L'){

      left();
    }
    else if(b=='B'){

      backward();
    }

    else{

      stop();
    }
  }}

  

 

void forward(){

  digitalWrite(2,HIGH);
 digitalWrite(3,LOW);
 digitalWrite(4,HIGH);
 digitalWrite(5,LOW); 
}
void backward(){
  digitalWrite(2, LOW);
 digitalWrite(3,HIGH);
 digitalWrite(4,LOW);
 digitalWrite(5,HIGH); 
  
  
}
void right(){
  digitalWrite(2, LOW);
 digitalWrite(3,LOW);
 digitalWrite(4,HIGH);
 digitalWrite(5,LOW); 
}
void left(){
digitalWrite(2, HIGH);
 digitalWrite(3,LOW);
 digitalWrite(4,LOW);
 digitalWrite(5,LOW); 
  

}
void stop(){
digitalWrite(2, LOW);
 digitalWrite(3,LOW);
 digitalWrite(4,LOW);
 digitalWrite(5,LOW); 
  

}

3 thoughts on “Arduino simple code for Obstacle Avoiding Robot & Mobile Control Robot”

Leave a Comment

Your email address will not be published. Required fields are marked *