Devices & Components
Arduino Uno Rev3
Ultrasonic Sensor - HC-SR04 (Generic)
Jumper wires (generic)
RC servo MG996R
Hardware & Tools
Hot glue gun (generic)
Drill, Screwdriver
Software & Tools
Arduino IDE
Project description
Code
Automatic hanitizer Code
Automatic hanitizer code
c_cpp
1#include <Servo.h> 2int echoPin = 12; 3int trigPin = 11; 4int servoPin = 10; 5int duration; //micro second 6float d_cm; 7int pos01=0; 8int pos02 = 100; 9Servo myServo; 10void setup() { 11 12// initialize serial communication: 13 Serial.begin(9600); 14 pinMode(trigPin,OUTPUT); 15 pinMode(echoPin,INPUT); 16 myServo.attach(servoPin); 17 myServo.write(pos01); 18} 19 20void loop() { 21 // generate pusle trigger 22 digitalWrite(trigPin,LOW); 23 delayMicroseconds(2); 24 digitalWrite(trigPin,HIGH); 25 delayMicroseconds(10); 26 digitalWrite(trigPin,LOW); 27 // receive 28 duration = pulseIn(echoPin,HIGH); 29 d_cm = duration * 0.034/2.; 30 //servo 31 if(d_cm>=11 & d_cm<=14){ 32 for (int i=0;i<=pos02; i+=10){ 33 myServo.write(i); 34 delay(15); 35 } 36 delay(400); 37 myServo.write(pos01); 38 delay(1000); 39 } 40 //print 41 Serial.print("Distance:"); 42 Serial.print(d_cm); 43 Serial.println("cm"); 44} 45
Automatic hanitizer code
c_cpp
1#include <Servo.h> 2int echoPin = 12; 3int trigPin = 11; 4int servoPin 5 = 10; 6int duration; //micro second 7float d_cm; 8int pos01=0; 9int pos02 10 = 100; 11Servo myServo; 12void setup() { 13 14// initialize serial communication: 15 16 Serial.begin(9600); 17 pinMode(trigPin,OUTPUT); 18 pinMode(echoPin,INPUT); 19 20 myServo.attach(servoPin); 21 myServo.write(pos01); 22} 23 24void loop() { 25 26 // generate pusle trigger 27 digitalWrite(trigPin,LOW); 28 delayMicroseconds(2); 29 30 digitalWrite(trigPin,HIGH); 31 delayMicroseconds(10); 32 digitalWrite(trigPin,LOW); 33 34 // receive 35 duration = pulseIn(echoPin,HIGH); 36 d_cm = duration * 0.034/2.; 37 38 //servo 39 if(d_cm>=11 & d_cm<=14){ 40 for (int i=0;i<=pos02; i+=10){ 41 42 myServo.write(i); 43 delay(15); 44 } 45 delay(400); 46 myServo.write(pos01); 47 48 delay(1000); 49 } 50 //print 51 Serial.print("Distance:"); 52 Serial.print(d_cm); 53 54 Serial.println("cm"); 55} 56
Automatic hanitizer Code
Downloadable files
Automatic hanitizer Schematics
Automatic hanitizer Schematics
Comments
Only logged in users can leave comments