1#include <Servo.h>
2Servo servo;
3int trigPin = 5;
4int echoPin = 6;
5int servoPin = 7;
6int led= 10;
7long duration, dist, average;
8long aver[3];
9
10
11void setup() {
12 Serial.begin(9600);
13 servo.attach(servoPin);
14 pinMode(trigPin, OUTPUT);
15 pinMode(echoPin, INPUT);
16 servo.write(0);
17 delay(100);
18 servo.detach();
19}
20
21void measure() {
22 digitalWrite(10,HIGH);
23digitalWrite(trigPin, LOW);
24delayMicroseconds(5);
25digitalWrite(trigPin, HIGH);
26delayMicroseconds(15);
27digitalWrite(trigPin, LOW);
28pinMode(echoPin, INPUT);
29duration = pulseIn(echoPin, HIGH);
30dist = (duration/2) / 29.1;
31}
32void loop() {
33 for (int i=0;i<=2;i++) {
34 measure();
35 aver[i]=dist;
36 delay(10);
37 }
38 dist=(aver[0]+aver[1]+aver[2])/3;
39
40if ( dist<50 ) {
41
42 servo.attach(servoPin);
43 delay(1);
44 servo.write(0);
45 delay(3000);
46 servo.write(150);
47 delay(1000);
48 servo.detach();
49}
50Serial.print(dist);
51}
varshak3021
3 years ago
Need of the hour... excellent idea and execution. We need fresh vision like this👍