Devices & Components
Arduino Uno Rev3
UTSOURCE Electronic Parts
Ultrasonic Sensor - HC-SR04 (Generic)
Servos (Tower Pro MG996R)
Software & Tools
Arduino IDE
Project description
Code
arduino trash-bot code
c_cpp
1#include<Servo.h> 2Servo servo; 3int const trigPin = 6; 4int const echoPin = 5; 5void setup() 6{ 7pinMode(trigPin, OUTPUT); 8pinMode(echoPin, INPUT); 9 servo.attach(3); 10} 11void loop() 12{ int duration, distance; 13digitalWrite(trigPin, HIGH); 14delay(1); 15digitalWrite(trigPin, LOW); 16// Measure the pulse input in echo pin 17duration = pulseIn(echoPin, HIGH); 18// Distance is half the duration devided by 29.1 (from datasheet) 19distance = (duration/2) / 29.1; 20// if distance less than 0.5 meter and more than 0 (0 or less means over range) 21if (distance <= 50 && distance >= 0) { 22 servo.write(50); 23 delay(3000); 24} else { 25 26 servo.write(160); 27} 28// Waiting 60 ms won't hurt any one 29delay(60); 30}
arduino trash-bot code
c_cpp
1#include<Servo.h> 2Servo servo; 3int const trigPin = 6; 4int const echoPin = 5; 5void setup() 6{ 7pinMode(trigPin, OUTPUT); 8pinMode(echoPin, INPUT); 9 servo.attach(3); 10} 11void loop() 12{ int duration, distance; 13digitalWrite(trigPin, HIGH); 14delay(1); 15digitalWrite(trigPin, LOW); 16// Measure the pulse input in echo pin 17duration = pulseIn(echoPin, HIGH); 18// Distance is half the duration devided by 29.1 (from datasheet) 19distance = (duration/2) / 29.1; 20// if distance less than 0.5 meter and more than 0 (0 or less means over range) 21if (distance <= 50 && distance >= 0) { 22 servo.write(50); 23 delay(3000); 24} else { 25 26 servo.write(160); 27} 28// Waiting 60 ms won't hurt any one 29delay(60); 30}
Downloadable files
arduino trash-bot
arduino trash-bot
arduino trash-bot code
https://github.com/ashraf-minhaj/Trash-bot
arduino trash-bot
arduino trash-bot
Comments
Only logged in users can leave comments