//AKSHIT INNOVATION LAB
//https://www.youtube.com/channel/UCvqrugW02HjKbGUE2bw0muw
#include <Servo.h> //servo library
Servo servo;
int trigPin = 12;
int echoPin = 11;
int servoPin = 10;
long duration, dist, average;
long aver[3];
void setup() {
servo.attach(servoPin);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
servo.write(0);
delay(1000);
servo.detach();
}
void measure() {
digitalWrite(trigPin, LOW);
digitalWrite(12,HIGH);
delayMicroseconds(5);
digitalWrite(trigPin, HIGH);
delayMicroseconds(15);
digitalWrite(trigPin, LOW);
pinMode(echoPin, INPUT);
duration = pulseIn(echoPin, HIGH);
dist = (duration/2) / 29.1;
}
void loop() {
for (int i=0;i<=2;i++) {
measure();
aver[i]=dist;
delay(50);
}
dist=(aver[0]+aver[1]+aver[2])/3; //average distance by 3 measurements
if ( dist<30 ) {
//if hand on the distance 10...30 cm
servo.attach(servoPin);
delay(1);
servo.write(360);
servo.write(360);
servo.attach(servoPin);
delay(5000);
delay(1);
servo.write(360);
servo.write(360);
delay(5000); //wait 5 seconds
servo.write(0);
delay(1000);
servo.write(1);
digitalWrite(8,HIGH);
}
}
Comments