Chandran N
Published © GPL3+

Beach cleaning Bot

A simple 4WD robot capable of picking up garbage littered on the beach.

IntermediateWork in progress98
Beach cleaning Bot

Things used in this project

Hardware components

Arduino UNO & Genuino UNO
Arduino UNO & Genuino UNO
×2
Ultrasonic Sensor - HC-SR04 (Generic)
Ultrasonic Sensor - HC-SR04 (Generic)
×3
Jumper wires (generic)
Jumper wires (generic)
×30
Breadboard (generic)
Breadboard (generic)
×1
SG90 Micro-servo motor
SG90 Micro-servo motor
×1

Story

Read more

Code

Arduino code

Arduino
#include<AFMotor.h>
#include <Servo.h>
//#include<Wire.h>
#define trigPin1 12
#define echoPin1 10
#define trigPin2 8
#define echoPin2 6
#define trigPin3 4
#define echoPin3 2
//Servo myservo;
int pos = 0,f=0;
AF_DCMotor motor1(1);
AF_DCMotor motor2(2);
AF_DCMotor motor3(3);
AF_DCMotor motor4(4);
void setup() {
  //Wire.begin(); 
  Serial.begin(9600);
  
  motor1.setSpeed(200);
  motor2.setSpeed(200);
  motor3.setSpeed(200);
  motor4.setSpeed(200);
  pinMode(trigPin1, OUTPUT);
  pinMode(echoPin1, INPUT);
  pinMode(trigPin2, OUTPUT);
  pinMode(echoPin2, INPUT);
  pinMode(trigPin3, OUTPUT);
  pinMode(echoPin3, INPUT);
  //myservo.attach(9);
}

void loop() {
  
long duration1, distance1;
  digitalWrite(trigPin1, LOW);  // Added this line
  delayMicroseconds(2); // Added this line
  digitalWrite(trigPin1, HIGH);
//  delayMicroseconds(1000); - Removed this line
  delayMicroseconds(10); // Added this line
  digitalWrite(trigPin1, LOW);
  duration1 = pulseIn(echoPin1, HIGH);
  distance1 = (duration1/2) / 29.1;
  
  long duration2, distance2;
  digitalWrite(trigPin2, LOW);  // Added this line
  delayMicroseconds(2); // Added this line
  digitalWrite(trigPin2, HIGH);
//  delayMicroseconds(1000); - Removed this line
  delayMicroseconds(10); // Added this line
  digitalWrite(trigPin2, LOW);
  duration2 = pulseIn(echoPin2, HIGH);
  distance2 = (duration2/2) / 29.1;

  long duration3, distance3;
  digitalWrite(trigPin3, LOW);  // Added this line
  delayMicroseconds(2); // Added this line
  digitalWrite(trigPin3, HIGH);
//  delayMicroseconds(1000); - Removed this line
  delayMicroseconds(10); // Added this line
  digitalWrite(trigPin3, LOW);
  duration3 = pulseIn(echoPin3, HIGH);
  distance3 = (duration3/2) / 29.1;
  Serial.print(distance1);
  Serial.print(",");
  Serial.print(distance2);
  Serial.print(",");
  Serial.println(distance3);
  if(distance1>15 && distance2>15 && distance3>15)//In the event of no obstacle being detected by any of the sensors
  {   
      motor1.run(FORWARD);
      motor2.run(FORWARD);
      motor3.run(FORWARD);
      motor4.run(FORWARD);
  }
  else if(distance1<15 && distance2>15 && distance3>15 )//If the centre sensor is near an obstacle
  {   
      motor1.run(BACKWARD);//First move back for 1 second
      motor2.run(BACKWARD);
      motor3.run(BACKWARD);
      motor4.run(BACKWARD);
      delay(1000);
      motor1.run(FORWARD);//Then turn left for 1.5 seconds
      motor2.run(RELEASE);
      motor3.run(FORWARD);
      motor4.run(RELEASE);
      delay(1500);
  }
  else if(distance1>15 && distance2<15 && distance3>15)//If the left sensor is near an obstacle
  {   
      motor1.run(BACKWARD);//First move back for 1 second
      motor2.run(BACKWARD);
      motor3.run(BACKWARD);
      motor4.run(BACKWARD);
      delay(1000);
      motor2.run(FORWARD);//Then turn right for 1.5 seconds
      motor1.run(RELEASE);
      motor4.run(FORWARD);
      motor3.run(RELEASE);
      delay(1500);
  }
  else if(distance1<15 && distance2>15 && distance3>15)//If the middle sensor is near an obstacle
  {   
      motor1.run(BACKWARD);//First move back for 2 seconds
      motor2.run(BACKWARD);
      motor3.run(BACKWARD);
      motor4.run(BACKWARD);
      delay(2000);
       motor2.run(RELEASE);//Then turn right(arbitrary,you could go left too)
      motor1.run(FORWARD);//For 1.5 seconds
      motor3.run(FORWARD);
      motor4.run(RELEASE);
      delay(1500);
      
  }
  else//If its none of the above,just move backwards for 2 seconds
  {
    motor1.run(BACKWARD);
    motor2.run(BACKWARD);
    delay(2000);
  }
  delay(100);

    
}

Credits

Chandran N

Chandran N

5 projects • 15 followers
I am a student at NITK Surathkal pursuing Mechanical Engineering.I am interested in Robotics,Arduino,IoT,Automobiles and Astronomy.

Comments