roboattic Lab
Published © GPL3+

How to Make Table Edge Avoiding Robot || #MadeWithArduino

When the car reaches the end of the table i.e. the edge of the table the ultrasonic sensor detects its depth and it makes the car go back.

IntermediateFull instructions provided5 hours591

Things used in this project

Story

Read more

Code

Code For Table Edge Avoiding Car

C/C++
//Arduino edge Avoidaing Robot
//Created By DIY Burner
//Contact me here https://www.instagram.com/diy_burner/
//You need to include AF Motor.h library before uploading the sketch, otherwise you'll get compilation error message.

#include <AFMotor.h>
const int trigPin = A1 ; //Servo trig pin to D10
const int echoPin = A0; // Servo echo pin to D11



AF_DCMotor motor1(1, MOTOR12_1KHZ); 
AF_DCMotor motor2(2, MOTOR12_1KHZ); 
AF_DCMotor motor3(3, MOTOR34_1KHZ);
AF_DCMotor motor4(4, MOTOR34_1KHZ);


char command; 

void setup() 
{   
  Serial.begin(9600);  //Set the baud rate to your Bluetooth module.
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
  
}

long duration;
int distance;


void loop(){
  
  /*Serial.print("Right");
    Serial.println(Right);
    Serial.print("Left");
    Serial.println(Left);*/

  digitalWrite(trigPin , HIGH);
  delayMicroseconds(1000);
  digitalWrite(trigPin , LOW);

  duration = pulseIn(echoPin , HIGH);
  distance = (duration/2) / 28.5;

  if(Serial.available() > 0){ 
    command = Serial.read(); 
    Stop(); 
    
    Serial.println(command);
    
    if(distance <= 0 || distance <= 20)
    {
    switch(command){
    case 'F':  
      forward();
      break;
    case 'B':  
       back();
      break;
    case 'L':  
      left();
      break;
    case 'R':
      right();
      break;
    }
    }
    else
    {
      Stop();
      delay(15);
      back();
      delay(30);
      Stop();
    }
    
 }
}

void forward()
{
  motor1.setSpeed(150); //Define maximum Speed
  motor1.run(FORWARD); //rotate the motor clockwise
  motor2.setSpeed(150); //Define maximum Speed
  motor2.run(FORWARD); //rotate the motor clockwise
  motor3.setSpeed(150);//Define maximum Speed
  motor3.run(FORWARD); //rotate the motor clockwise
  motor4.setSpeed(150);//Define maximum Speed
  motor4.run(FORWARD); //rotate the motor clockwise
}

void back()
{
  motor1.setSpeed(150); //Define maximum Speed
  motor1.run(BACKWARD); //rotate the motor anti-clockwise
  motor2.setSpeed(150); //Define maximum Speed
  motor2.run(BACKWARD); //rotate the motor anti-clockwise
  motor3.setSpeed(150); //Define maximum Speed
  motor3.run(BACKWARD); //rotate the motor anti-clockwise
  motor4.setSpeed(150); //Define maximum Speed
  motor4.run(BACKWARD); //rotate the motor anti-clockwise
}

void left()
{
  motor1.setSpeed(200); //Define maximum Speed
  motor1.run(BACKWARD); //rotate the motor anti-clockwise
  motor2.setSpeed(200); //Define maximum Speed
  motor2.run(BACKWARD); //rotate the motor anti-clockwise
  motor3.setSpeed(200); //Define maximum Speed
  motor3.run(FORWARD);  //rotate the motor clockwise
  motor4.setSpeed(200); //Define maximum Speed
  motor4.run(FORWARD);  //rotate the motor clockwise
}

void right()
{
  motor1.setSpeed(200); //Define maximum Speed
  motor1.run(FORWARD); //rotate the motor clockwise
  motor2.setSpeed(200); //Define maximum Speed
  motor2.run(FORWARD); //rotate the motor clockwise
  motor3.setSpeed(200); //Define maximum Speed
  motor3.run(BACKWARD); //rotate the motor anti-clockwise
  motor4.setSpeed(200); //Define maximum Speed
  motor4.run(BACKWARD); //rotate the motor anti-clockwise
} 

void Stop()
{
  motor1.setSpeed(0); //Define minimum Speed
  motor1.run(RELEASE); //stop the motor when release the button
  motor2.setSpeed(0); //Define minimum Speed
  motor2.run(RELEASE); //rotate the motor clockwise
  motor3.setSpeed(0); //Define minimum Speed
  motor3.run(RELEASE); //stop the motor when release the button
  motor4.setSpeed(0); //Define minimum Speed
  motor4.run(RELEASE); //stop the motor when release the button
}

Credits

roboattic Lab

roboattic Lab

10 projects • 9 followers
YouTube Content Creator Robotics Enthusiast

Comments