-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtwo ultrasonic sensor code
121 lines (97 loc) · 2.48 KB
/
two ultrasonic sensor code
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
#define trigPin1 6
#define echoPin1 7
#define trigPin2 8
#define echoPin2 9
int hit1 = 0, hit2 = 0;
int numEntries = 0, numExits = 0;
// include the library code:
#include <LiquidCrystal.h>
// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
void setup() {
Serial.begin(9600);
// set up the LCD's number of columns and rows:
lcd.begin(16, 2);
//Print a message to the LCD.
lcd.print("Entity Count:");
//sensors
pinMode(trigPin1, OUTPUT);
pinMode(echoPin1, INPUT);
pinMode(trigPin2, OUTPUT);
pinMode(echoPin2, INPUT);
}
void loop() {
// set the cursor to column 0, line 1
lcd.setCursor(0, 1);
//check sensor1
int duration, distance;
digitalWrite(trigPin1, LOW);
delayMicroseconds (2);
digitalWrite (trigPin1, HIGH);
delayMicroseconds (5);
digitalWrite (trigPin1, LOW);
duration = pulseIn (echoPin1, HIGH);
distance = (duration/2) / 29.1;
if(distance < 15){
Serial.println("Entering...");
hit1 = 1;
}
//check sensor2
int duration2, distance2;
digitalWrite(trigPin2, LOW);
delayMicroseconds (2);
digitalWrite (trigPin2, HIGH);
delayMicroseconds (5);
digitalWrite (trigPin2, LOW);
duration2 = pulseIn (echoPin2, HIGH);
distance2 = (duration2 / 2) / 29.1;
if(distance2 < 15){
Serial.println("Exitting...");
hit2 = 1;
}
//entry was triggered, scan exit until exit is hit
while(hit1 == 1 && hit2 == 0){
//check sensor2
int dur, dist;
digitalWrite(trigPin2, LOW);
delayMicroseconds (2);
digitalWrite (trigPin2, HIGH);
delayMicroseconds (5);
digitalWrite (trigPin2, LOW);
dur = pulseIn (echoPin2, HIGH);
dist = (dur / 2) / 29.1;
if(dist < 15){
//reset the two values
hit1 = 0;
hit2 = 0;
numEntries++;
Serial.println("\tENTERED");
delay(250);
break;
}
}
//exit was triggered, scan entry until entry is hit
while(hit1 == 0 && hit2 == 1){
//check sensor2
int dur, dist;
digitalWrite(trigPin1, LOW);
delayMicroseconds (2);
digitalWrite (trigPin1, HIGH);
delayMicroseconds (5);
digitalWrite (trigPin1, LOW);
dur = pulseIn (echoPin1, HIGH);
dist = (dur / 2) / 29.1;
if(dist < 15){
//reset the two values
hit1 = 0;
hit2 = 0;
numExits++;
Serial.println("\tEXITED");
delay(250);
break;
}
}
// print the number of seconds since reset:
lcd.print(numEntries - numExits);
delay(500);
}