-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsonar_people_counter.ino
435 lines (337 loc) · 10.8 KB
/
sonar_people_counter.ino
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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
/*
Sonar People Counter with LCD Feedback
v.0.8
July 13, 2018 5:33 PM
DONE:
* Add back LED trigger light
*
* Pin assignments:
*
* D2 Sonar Trigger
* D3 Sonar Echo
* D4-10 connected to LCD
* D11 PWM for LED Strip
* D12 pause button
* D13 LED feedback
* A3 Digits Latch (blue)
* A4 Digits Clock (Green)
* A5 Digits Data (Yellow)
*
*/
// LIBRARIES
// -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
// include LCD library
// LiquidCrystal Using Sparkfun LCD Button shield
// https://www.sparkfun.com/products/13293
#include <LiquidCrystal.h>
// include the NewPing sonar library:
#include <NewPing.h>
// SONAR STUFF
// -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
#define TRIGGER_PIN 2
#define ECHO_PIN 3
#define MAX_DISTANCE 400
// Ping frequency (in milliseconds), fastest we should ping is about 35ms per sensor
#define pingSpeed 100 // was 125
// Sensor Yes: trigger pin, echo pin, maximum distance in cm
NewPing sonarYes(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE);// 120cm = ~48in
// stores when the next pingTimer will run
unsigned long pingTimer1;
// Global variables
// -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
#define LED_PIN 13 // onboard LED for feedback
#define LED_STRIP_PIN 11 // LED strip in doorway
#define PAUSE_PIN 12 // our Pause Button
int triggerDistance = 30; // This is the short distance, in inches
int IN1 = 0; // Global inches variable for sonar
int peopleCount = 0; // count of people who have walked by
boolean walkpastSwitch = false; // is someone walking by?
boolean pwalkpastSwitch = true; // was someone just walking by?
#define FADESPEED 1 // LED Strip make this higher to slow down
// -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
// LCD BUTTON variables
// -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
// array setup for button feedback
char* buttons[] = {"Left", "Up", "Down", "Right", "Select", "Undefined"};
#define LEFT_BUTTON 0
#define UP_BUTTON 1
#define DN_BUTTON 2
#define RIGHT_BUTTON 3
#define SEL_BUTTON 4
#define UNDEFINED 5
boolean downButton = false; // is the left button pressed?
boolean pauseButton = true; // is pause button NOT pressed?
int buttonindex; // what button is being pressed?
int pbuttonindex = 5; // what button just pressed?
// initialize the library with the numbers of the interface pins
// for the sparkfun LCD shield
LiquidCrystal lcd(8, 9, 4, 5, 6, 7);
// -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
// GPIO declarations for Large Digits
// -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
byte segmentClock = A4; // green
byte segmentLatch = A3; // blue
byte segmentData = A5; // yellow
// -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
// SETUP
// -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
void setup()
{
// // set up the LCD's number of columns and rows:
lcd.begin(16, 2);
// Open serial monitor at 9600 baud to see ping results
Serial.begin(9600); //for debugging
Serial.println("People Counter - setup begins!");
lcd.print("BING BONG");
// Sensor 1 fires after 100ms (or whatever pingSpeed is)
pingTimer1 = millis() + pingSpeed;
// set up pins an ardunio
pinMode(LED_PIN, OUTPUT);
pinMode(PAUSE_PIN, INPUT_PULLUP);
pinMode(segmentClock, OUTPUT);
pinMode(segmentData, OUTPUT);
pinMode(segmentLatch, OUTPUT);
// not needed?
// pinMode(A1, OUTPUT);
// pinMode(A2, INPUT);
//start with everything low
digitalWrite(segmentClock, LOW);
digitalWrite(segmentData, LOW);
digitalWrite(segmentLatch, LOW);
// start at 00
postNumber('00', false);
showNumber(peopleCount);
Serial.println("People Counter - setup complete");
}
// -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
// FUNCTION: Large-Digit - Show Number
// -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
// Takes a number and displays 2 numbers. Displays absolute value (no negatives)
void showNumber(float value)
{
int number = abs(value); //Remove negative signs and any decimals
//Serial.print("number: ");
//Serial.println(number);
for (byte x = 0 ; x < 3 ; x++)
{
int remainder = number % 10;
postNumber(remainder, false);
number = number / 10;
}
// Latch the current segment data
digitalWrite(segmentLatch, LOW);
digitalWrite(segmentLatch, HIGH); //Register moves storage register on the rising edge of RCK
}
// -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
// FUNCTION: Large-Digit - Post Number
// -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
void postNumber(byte number, boolean decimal){
//Given a number, or '-', shifts it out to the display
// - A
// / / F/B
// - G
// / / E/C
// -. D/DP
#define a 1<<0
#define b 1<<6
#define c 1<<5
#define d 1<<4
#define e 1<<3
#define f 1<<1
#define g 1<<2
#define dp 1<<7
byte segments;
switch (number)
{
case 1: segments = b | c; break;
case 2: segments = a | b | d | e | g; break;
case 3: segments = a | b | c | d | g; break;
case 4: segments = f | g | b | c; break;
case 5: segments = a | f | g | c | d; break;
case 6: segments = a | f | g | e | c | d; break;
case 7: segments = a | b | c; break;
case 8: segments = a | b | c | d | e | f | g; break;
case 9: segments = a | b | c | d | f | g; break;
case 0: segments = a | b | c | d | e | f; break;
case ' ': segments = 0; break;
case 'c': segments = g | e | d; break;
case '-': segments = g; break;
}
if (decimal) segments |= dp;
//Clock these bits out to the drivers
for (byte x = 0 ; x < 8 ; x++)
{
digitalWrite(segmentClock, LOW);
digitalWrite(segmentData, segments & 1 << (7 - x));
digitalWrite(segmentClock, HIGH); //Data transfers to the register on the rising edge of SRCK
}
}
// -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
// FUNCTION: Reads buttons and determines what's being pressed
// -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
void buttonCheck() {
int buttonValue = 1023;
buttonValue = analogRead(A0);
if (buttonValue > 845 && buttonValue < 865) // left button
{
buttonindex = LEFT_BUTTON;
}
else if (buttonValue > 915 && buttonValue < 949) // UP button
{
buttonindex = UP_BUTTON;
}
else if (buttonValue > 895 && buttonValue < 910) // down button
{
buttonindex = DN_BUTTON;
}
else if (buttonValue > 810 && buttonValue < 820) // right button
{
buttonindex = RIGHT_BUTTON;
}
else if (buttonValue > 605 && buttonValue < 620) // select button
{
buttonindex = SEL_BUTTON;
}
else {
buttonindex = UNDEFINED;
}
// lcd.clear();
// lcd.print(buttons[buttonindex]);
// lcd.print(" pressed");
}
// -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
// Function: fire the ping and listen for echo
// -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
void pingMachine(){
// fire the ping
if (millis() >= pingTimer1) {
pingTimer1 += pingSpeed; // Make sensor 1 fire again 100ms later (pingSpeed)
int in1 = sonarYes.ping_in();
if (in1 != 0) { // throw out 0 readings
IN1 = in1;
lcd.setCursor(15, 2);
lcd.print(" ");
} else {
lcd.setCursor(15, 2);
lcd.print("*");
}
}
}
// -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
// Function: Check Pause Button and run ping
// -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
void pauseCheck(){
// are we paused?
pauseButton = digitalRead(PAUSE_PIN);
if (pauseButton == false){
lcd.setCursor(0, 2);
lcd.print("PAUSED ");
pauseButton = digitalRead(PAUSE_PIN);
} else {
pingMachine(); // run the Sonar Ping function
lcd.setCursor(0, 2);
lcd.print("ACTIVE ");
}
// Serial.print("Ping1: ");
// Serial.print(IN1);
// Serial.println("in");
// lcd.clear(); // clear the screen
}
//
// Function: Flash LEDStrip
// -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
void flashLEDStrip(){
int r;
// for (int x = 0; x < 10; x++) {
// fade from dark to light
for (r = 0; r < 256; r++) {
analogWrite(LED_STRIP_PIN, r);
delay(FADESPEED);
}
analogWrite(LED_STRIP_PIN, 255);
delay(FADESPEED * 50);
// fade from light to dark
for (r = 255; r >= 0; r--) {
analogWrite(LED_STRIP_PIN, r);
delay(FADESPEED);
}
// }
// for (int x = 0; x < 100; x++) {
// analogWrite(LED_STRIP_PIN, 255);
// delay(FADESPEED * 25);
// analogWrite(LED_STRIP_PIN, 0);
// delay(FADESPEED * 25);
// }
}
// -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
//
// BRING ON THE LOOP
// -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
void loop(){
pauseCheck(); // runs the sonar ping
buttonCheck();
// display our info on the LCD
lcd.home(); // move cursor home
lcd.print("Dist:");
lcd.print(IN1);
lcd.print("in ");
lcd.setCursor(12,0);
lcd.print("[");
lcd.print(triggerDistance);
lcd.print("]");
// below checks if someone is walking by
// (tests the distance of the object in front of sonar)
if (IN1 < triggerDistance && IN1 > 1){
/* If the sonar is triggered
- true - someone is walking past
- light up the LED
*/
walkpastSwitch = true;
digitalWrite(LED_PIN, HIGH);
// if someone wasn't walking past before, and now someone is...
if(pwalkpastSwitch == false && walkpastSwitch == true){
peopleCount = peopleCount + 1; // add a person
}
// send the peopleCount on the large digits
showNumber(peopleCount);
flashLEDStrip();
} else {
/* If the sonar is triggered
* turn off the LED
* false - someone is walking past
*/
digitalWrite(LED_PIN, LOW);
walkpastSwitch = false;
}
// BUTTON ADJUSTMENTS
// UP = more people
if (pbuttonindex == UP_BUTTON && buttonindex == UNDEFINED){
peopleCount = peopleCount + 1;
}
// DOWN = less people
if (pbuttonindex == DN_BUTTON && buttonindex == UNDEFINED){
peopleCount = peopleCount - 1;
}
// LEFT = less trigger distance
if (pbuttonindex == LEFT_BUTTON && buttonindex == UNDEFINED){
triggerDistance = triggerDistance - 1;
}
// RIGHT = more trigger distance
if (pbuttonindex == RIGHT_BUTTON && buttonindex == UNDEFINED){
triggerDistance = triggerDistance + 1;
}
// save the current state to previous state variables
pwalkpastSwitch = walkpastSwitch;
pbuttonindex = buttonindex;
// send the peopleCount on the large digits
// in case anything has changed (like the numbers)
showNumber(peopleCount);
// Troubleshooting area...
// Serial.print("downButton: ");
// Serial.println(downButton);
// Serial.print("triggerDistance: ");
// Serial.println(triggerDistance);
// Serial.print("Ping: ");
// Serial.print(sonarYes.ping_cm());
// Serial.println("cm");
}