-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMotionSensorsAndKeypad
More file actions
220 lines (182 loc) · 5.24 KB
/
MotionSensorsAndKeypad
File metadata and controls
220 lines (182 loc) · 5.24 KB
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
#include <Arduino.h>
#include <Keypad.h>
#include <LiquidCrystal.h>
#define onboard 13
//define the pins for sensor
#define buzzerPin 25
#define trigPin1 11
#define echoPin1 10
#define trigPin2 13
#define echoPin2 12
#define greenLight 22
#define redLight 23
int timeoutCounter = 0;
int currentPeople = 0;
//can change the max capacity here
int maxCap = 2;
long duration, distance;
//four rows
const int ROW_NUM = 4;
//four columns
const int COLUMN_NUM = 4;
char keys[ROW_NUM][COLUMN_NUM] = {
{'1','2','3', 'A'},
{'4','5','6', 'B'},
{'7','8','9', 'C'},
{'*','0','#', 'D'}
};
//connect to rowout pinouts of the keypad
byte pin_rows[ROW_NUM] = {9, 8, 7, 6};
//connect to the column pinouts of the keypad
byte pin_column[COLUMN_NUM] = {5, 4, 3, 2};
Keypad keypad = Keypad(makeKeymap(keys), pin_rows, pin_column, ROW_NUM, COLUMN_NUM);
String inputPhoneNumber;
long inputInt;
//variables for ultrasonic sensor
int distance1;
int distance2;
int initialDistance1;
int initialDistance2;
String sequence = "";
//function to measure intial distance
int distanceMeasure(int trigPin, int echoPin){
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
int duration = pulseIn (echoPin, HIGH);
//divde by 2 to get 1 way time
return (duration/2)/29.1;
}
//initialize the library with the numbers of the interface pins
LiquidCrystal lcd(51, 49, 47, 45, 43, 41);
void setup() {
//set up the LCD's number of columns and rows:
lcd.begin(16, 2);
// Print a message to the LCD.
lcd.print("setting up...");
//maximum number of digit for a number is 10, change if needed
inputPhoneNumber.reserve(10);
//add buzzer stuff here
pinMode(buzzerPin,OUTPUT);
pinMode(trigPin1, OUTPUT);
pinMode(echoPin1, INPUT);
pinMode(trigPin2, OUTPUT);
pinMode(echoPin2, INPUT);
pinMode(greenLight, OUTPUT);
pinMode(redLight, OUTPUT);
//this sets the baud rate
Serial.begin(9600);
//get the initial distance of wall to sensor
initialDistance1 = distanceMeasure(trigPin1,echoPin1);
initialDistance2 = distanceMeasure(trigPin1,echoPin2);
} //end of void setup
int index = 0;
//function to get phone number
void getPhoneNumber(){
char key = keypad.getKey();
if (key) {
if(key != '#'){
Serial.print(key);
lcd.print(key);
}
else {
Serial.print(",");
lcd.setCursor(0,1) ;
lcd.print("#: ");
lcd.print(inputPhoneNumber);
delay(2000);
}
if (key >= '0' && key <= '9') { //only act on numeric keys
inputPhoneNumber += key; //append new character to input string
} else if (key == '#') {
digitalWrite(buzzerPin, HIGH);
delay(500);
digitalWrite(buzzerPin, LOW);
lcd.clear();
lcd.print("#: ");
lcd.print(inputPhoneNumber);
lcd.clear();
//clear the input string
if (inputPhoneNumber.length() > 0) {
inputInt = inputPhoneNumber.toInt(); //YOU GOT AN INTEGER NUMBER
inputPhoneNumber = ""; //clear input
}
}
}
} //end phone number function
void loop(){
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Welcome!");
lcd.setCursor(0,1) ;
lcd.print("current cap: ");
lcd.print(currentPeople);
//led display code?
// set the cursor to column 0, line 1
// (note: line 1 is the second row, since counting begins with 0):
// print the number of seconds since reset:
// lcd.print(millis() / 1000);
digitalWrite(greenLight, HIGH);
//reading will be taken after 200 miliseconds so device doesn't overuse itself
delay(200);
//waitForSerial();
//get distance of two sensor
distance1=distanceMeasure(trigPin1, echoPin1);
distance2=distanceMeasure(trigPin2, echoPin2);
if(distance1 < initialDistance1 && sequence.charAt(0) != '1') { //12 -> walked in
sequence += "1";
} else if(distance2 < initialDistance2 && sequence.charAt(0) != '2') { //21 -> walked out
sequence += "2";
}
if(sequence.equals("12")) { //12 -> walked in
currentPeople++;
sequence="";
delay(550);
}
else if(sequence.equals("21") && currentPeople > 0) { //21 -> walked out
currentPeople--;
sequence="";
delay(550);
}
//resets the sequence if it is invalid or timesouts
if(sequence.length() > 2 || sequence.equals("11") || sequence.equals("22") || timeoutCounter > 200) {
sequence = "";
}
if(sequence.length() == 1) {
timeoutCounter++;
}
else {
timeoutCounter = 0;
}
//Print values to serial (for testing
// Serial.print("Seq: ");
// Serial.println(sequence);
// Serial.print("d1: ");
// Serial.println(distance1);
// Serial.print("d2: ");
// Serial.println(distance2);
// Serial.print("Current people: ");
// Serial.println(currentPeople);
// if (currentPeople > maxCap){
// tone(buzzerPin, 1, 500);
// delay(500);
// }else{
// noTone(buzzerPin);
// }
if(currentPeople==maxCap) {
lcd.clear();
lcd.print("reached max cap!");
lcd.setCursor(0,1) ;
lcd.print("press # to continue");
//lcd.print(currentPeople);
digitalWrite(redLight, HIGH);
digitalWrite(buzzerPin, HIGH);
delay(500);
digitalWrite(buzzerPin, LOW);
}
while(currentPeople==maxCap){
digitalWrite(greenLight, LOW);
getPhoneNumber();
//break;
}
}