-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSmart_Automatic_Gate.ino
More file actions
54 lines (39 loc) · 856 Bytes
/
Copy pathSmart_Automatic_Gate.ino
File metadata and controls
54 lines (39 loc) · 856 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#include <Servo.h>
Servo gateServo;
const int trigPin = 7;
const int echoPin = 6;
const int servoPin = 9;
long duration;
int distance;
bool gateOpen = false;
void setup() {
gateServo.attach(servoPin);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
gateServo.write(0);
Serial.begin(9600);
}
void loop() {
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = duration * 0.034 / 2;
Serial.print("Distance: ");
Serial.print(distance);
Serial.println(" cm");
if (distance < 100 && gateOpen == false) {
gateServo.write(90);
gateOpen = true;
Serial.println("Gate Opened");
delay(3000);
}
else if (distance >= 100 && gateOpen == true) {
gateServo.write(0);
gateOpen = false;
Serial.println("Gate Closed");
}
delay(200);
}