1const int trigPin = 12;
2const int echoPin = 13;
3
4const int R_LED = A0;
5const int G_LED = A1;
6const int Relay = A2;
7
8
9int duration = 0;
10int distance = 0;
11
12void setup()
13{
14 pinMode(trigPin , OUTPUT);
15 pinMode(echoPin , INPUT);
16
17 pinMode(R_LED , OUTPUT);
18 pinMode(G_LED , OUTPUT);
19 pinMode(Relay , OUTPUT);
20
21
22 Serial.begin(9600);
23
24}
25
26void loop()
27{
28 digitalWrite(trigPin, LOW);
29 delayMicroseconds(2);
30 digitalWrite(trigPin, HIGH);
31 delayMicroseconds(10);
32 digitalWrite(trigPin, LOW);
33 duration = pulseIn(echoPin, HIGH);
34 distance = duration/58.2;
35
36 if ( distance <= 4 )
37 {
38 digitalWrite(R_LED, HIGH);
39 digitalWrite(G_LED, LOW);
40 digitalWrite(Relay, LOW);
41 }
42 else if ( distance >= 7 )
43 {
44 digitalWrite(R_LED, LOW);
45 digitalWrite(G_LED, HIGH);
46 digitalWrite(Relay, HIGH);
47 }
48
49 delay(500);
50}
51