Devices & Components
Arduino Uno Rev3
USB Cable For Arduino Uno
SG90 Micro-servo motor
A Carton
Jumper wires (generic)
A Basket
9V battery (generic)
Ultrasonic Sensor - HC-SR04 (Generic)
Project description
Code
Code
c_cpp
Here is the project code.
1#include <Servo.h> 2 3Servo servo; 4 5int trigPin = 5; 6int echoPin = 6; 7int servoPin = 7; 8long duration, dist, average; 9long aver[3]; 10 11void setup(){ 12 13 14 Serial.begin(9600); 15 servo.attach(servoPin); 16 pinMode(trigPin, OUTPUT); 17 pinMode(echoPin, INPUT); 18 servo.write(0); 19 delay(100); 20 servo.attach(); 21 22 23} 24 25 26void measure(){ 27 28 digitalWrite(trigPin, LOW); 29 delayMicroseconds(5); 30 digitalWrite(trigPin, HIGH); 31 delayMicroseconds(15); 32 digitalWrite(trigPin, LOW); 33 pinMode(echoPin,INPUT); 34 duration = pulseIn(echoPin, HIGH); 35 dist = (duration/2)/ 29.1; 36 37} 38 39void loop(){ 40 41 42 for(int i=0; i<=2; i++){ 43 measure(); 44 aver[i]=dist; 45 delay(10); 46 } 47 48 dist = (aver[0]+aver[1]+[2])/3; 49 50 if(dist<50){ 51 52 servo.attach(servoPin); 53 delay(1); 54 servo.write(0); 55 delay(3000); 56 servo.write(150); 57 delay(1000); 58 servo.detach(); 59 60 } 61 Serial.print(dist); 62} 63
Downloadable files
Power Supply
Here is the power supply system.
Power Supply

Power Supply
Here is the power supply system.
Power Supply

Comments
Only logged in users can leave comments