Skip to content

Commit 4edfe23

Browse files
committed
Create Arduino_People_Counter.ino
1 parent 4cbc674 commit 4edfe23

File tree

1 file changed

+313
-0
lines changed

1 file changed

+313
-0
lines changed
Lines changed: 313 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,313 @@
1+
/*
2+
6-13-2011
3+
Spark Fun Electronics 2011
4+
Nathan Seidle
5+
6+
This code is public domain but you buy me a beer if you use this and we meet
7+
someday (Beerware license).
8+
9+
4 digit 7 segment display:
10+
http://www.sparkfun.com/products/9483
11+
Datasheet:
12+
http://www.sparkfun.com/datasheets/Components/LED/7-Segment/YSD-439AR6B-35.pdf
13+
14+
This is an example of how to drive a 7 segment LED display from an ATmega
15+
without the use of current limiting resistors. This technique is very common
16+
but requires some knowledge of electronics - you do run the risk of dumping
17+
too much current through the segments and burning out parts of the display.
18+
If you use the stock code you should be ok, but be careful editing the
19+
brightness values.
20+
21+
This code should work with all colors (red, blue, yellow, green) but the
22+
brightness will vary from one color to the next because the forward voltage
23+
drop of each color is different. This code was written and calibrated for the
24+
red color.
25+
26+
This code will work with most Arduinos but you may want to re-route some of
27+
the pins.
28+
29+
7 segments
30+
4 digits
31+
1 colon
32+
=
33+
12 pins required for full control
34+
35+
*/
36+
37+
/*
38+
People counter sketch modified by Frollard on October 2 2012
39+
checks an analog input, when it reaches a threshhold it increments a counter, and won't
40+
increment that counter again until another lower reset threshhold has been hit.
41+
*/
42+
43+
//global:
44+
int peopleCount = 0; //total count of people, init zero
45+
bool currentState = 1; //are we even listening for new hits?
46+
#define LDRPin A2 //pin to read LDR
47+
#define ledPin 13 //output led pin
48+
int light_sensitivity = 700; //This is the approx value of light surrounding your LDR
49+
int countTrigger = light_sensitivity + 75; //value the LDR exceeds to count someone
50+
int resetTrigger = light_sensitivity + 60; //value at which to start counting again
51+
//it wouldn't be a bad idea in void loop to keep averaging the current value and
52+
//adjusting all these values in case the lighting changes.
53+
54+
//**********digit declares
55+
int segA = A1; //Display pin 14
56+
int segB = 3; //Display pin 16
57+
int segC = 4; //Display pin 13
58+
int segD = 5; //Display pin 3
59+
int segE = A0; //Display pin 5
60+
int segF = 7; //Display pin 11
61+
int segG = 8; //Display pin 15
62+
63+
int digit1 = 11; //PWM Display pin 1
64+
int digit2 = 10; //PWM Display pin 2
65+
int digit3 = 9; //PWM Display pin 6
66+
int digit4 = 6; //PWM Display pin 8
67+
//***********end digit declares
68+
69+
void setup() {
70+
pinMode(LDRPin, INPUT); // declare the LDR as an INPUT
71+
pinMode(ledPin, OUTPUT); // declare the ledPin as an OUTPUT
72+
73+
pinMode(segA, OUTPUT);
74+
pinMode(segB, OUTPUT);
75+
pinMode(segC, OUTPUT);
76+
pinMode(segD, OUTPUT);
77+
pinMode(segE, OUTPUT);
78+
pinMode(segF, OUTPUT);
79+
pinMode(segG, OUTPUT);
80+
81+
pinMode(digit1, OUTPUT);
82+
pinMode(digit2, OUTPUT);
83+
pinMode(digit3, OUTPUT);
84+
pinMode(digit4, OUTPUT);
85+
Serial.begin(9600);
86+
}
87+
88+
void loop()
89+
{
90+
checkInput(); //reads the ldr and counts if necessary.
91+
displayNumber(peopleCount);
92+
93+
}
94+
//******Averages the LDR reading, then acts upon it, counting, and toggling the led.
95+
void checkInput()
96+
{
97+
int LDRValue = 0;
98+
//remove noise by taking 5 readings then averaging
99+
for (int i = 0; i<10; i++){
100+
delay(1); //may not be necessary but analogRead() can be messy when next to pwm
101+
LDRValue += analogRead(LDRPin);
102+
}
103+
LDRValue = LDRValue / 10;
104+
105+
106+
if (currentState == 1) //actively looking for a person
107+
{ //if true, act on the LDR value to attempt to count
108+
if (LDRValue >= countTrigger){ //if ldr goes above trigger we found someone
109+
currentState = 0; //stop listening for new counts until it gets bright again
110+
peopleCount++; //increment the count
111+
digitalWrite(ledPin,HIGH); //led on
112+
113+
}
114+
}
115+
else //currentstate was false, don't check for people, check for reset
116+
{
117+
if (LDRValue <= resetTrigger){ //if value was below the reset threshhold
118+
currentState = 1; //start listening again
119+
digitalWrite(ledPin,LOW); //led off
120+
}
121+
122+
}
123+
124+
}
125+
126+
//Given a number, we display 10:22
127+
//After running through the 4 numbers, the display is left turned off
128+
129+
//Display brightness
130+
//Each digit is on for a certain amount of microseconds
131+
//Then it is off until we have reached a total of 20ms for the function call
132+
//Let's assume each digit is on for 1000us
133+
//Each digit is on for 1ms, there are 4 digits, so the display is off for 16ms.
134+
//That's a ratio of 1ms to 16ms or 6.25% on time (PWM).
135+
//Let's define a variable called brightness that varies from:
136+
//5000 blindingly bright (15.7mA current draw per digit)
137+
//2000 shockingly bright (11.4mA current draw per digit)
138+
//1000 pretty bright (5.9mA)
139+
//500 normal (3mA)
140+
//200 dim but readable (1.4mA)
141+
//50 dim but readable (0.56mA)
142+
//5 dim but readable (0.31mA)
143+
//1 dim but readable in dark (0.28mA)
144+
145+
void displayNumber(int toDisplay) {
146+
#define DISPLAY_BRIGHTNESS 500
147+
148+
#define DIGIT_ON HIGH
149+
#define DIGIT_OFF LOW
150+
151+
long beginTime = millis();
152+
153+
int initialToDisplay = toDisplay;
154+
155+
for(int digit = 4 ; digit > 0 ; digit--) {
156+
157+
//Turn on a digit for a short amount of time
158+
switch(digit) {
159+
case 1:
160+
if (initialToDisplay>999) digitalWrite(digit1, DIGIT_ON);
161+
break;
162+
case 2:
163+
if (initialToDisplay>99) digitalWrite(digit2, DIGIT_ON);
164+
break;
165+
case 3:
166+
if (initialToDisplay>9) digitalWrite(digit3, DIGIT_ON);
167+
break;
168+
case 4:
169+
digitalWrite(digit4, DIGIT_ON);
170+
break;
171+
}
172+
173+
//Turn on the right segments for this digit
174+
lightNumber(toDisplay % 10);
175+
toDisplay /= 10;
176+
177+
delayMicroseconds(DISPLAY_BRIGHTNESS);
178+
//Display digit for fraction of a second (1us to 5000us, 500 is pretty good)
179+
180+
//Turn off all segments
181+
lightNumber(10);
182+
183+
//Turn off all digits
184+
digitalWrite(digit1, DIGIT_OFF);
185+
digitalWrite(digit2, DIGIT_OFF);
186+
digitalWrite(digit3, DIGIT_OFF);
187+
digitalWrite(digit4, DIGIT_OFF);
188+
}
189+
190+
while( (millis() - beginTime) < 10) ;
191+
//Wait for 20ms to pass before we paint the display again
192+
}
193+
194+
//Given a number, turns on those segments
195+
//If number == 10, then turn off number
196+
void lightNumber(int numberToDisplay) {
197+
198+
#define SEGMENT_ON LOW
199+
#define SEGMENT_OFF HIGH
200+
201+
switch (numberToDisplay){
202+
203+
case 0:
204+
digitalWrite(segA, SEGMENT_ON);
205+
digitalWrite(segB, SEGMENT_ON);
206+
digitalWrite(segC, SEGMENT_ON);
207+
digitalWrite(segD, SEGMENT_ON);
208+
digitalWrite(segE, SEGMENT_ON);
209+
digitalWrite(segF, SEGMENT_ON);
210+
digitalWrite(segG, SEGMENT_OFF);
211+
break;
212+
213+
case 1:
214+
digitalWrite(segA, SEGMENT_OFF);
215+
digitalWrite(segB, SEGMENT_ON);
216+
digitalWrite(segC, SEGMENT_ON);
217+
digitalWrite(segD, SEGMENT_OFF);
218+
digitalWrite(segE, SEGMENT_OFF);
219+
digitalWrite(segF, SEGMENT_OFF);
220+
digitalWrite(segG, SEGMENT_OFF);
221+
break;
222+
223+
case 2:
224+
digitalWrite(segA, SEGMENT_ON);
225+
digitalWrite(segB, SEGMENT_ON);
226+
digitalWrite(segC, SEGMENT_OFF);
227+
digitalWrite(segD, SEGMENT_ON);
228+
digitalWrite(segE, SEGMENT_ON);
229+
digitalWrite(segF, SEGMENT_OFF);
230+
digitalWrite(segG, SEGMENT_ON);
231+
break;
232+
233+
case 3:
234+
digitalWrite(segA, SEGMENT_ON);
235+
digitalWrite(segB, SEGMENT_ON);
236+
digitalWrite(segC, SEGMENT_ON);
237+
digitalWrite(segD, SEGMENT_ON);
238+
digitalWrite(segE, SEGMENT_OFF);
239+
digitalWrite(segF, SEGMENT_OFF);
240+
digitalWrite(segG, SEGMENT_ON);
241+
break;
242+
243+
case 4:
244+
digitalWrite(segA, SEGMENT_OFF);
245+
digitalWrite(segB, SEGMENT_ON);
246+
digitalWrite(segC, SEGMENT_ON);
247+
digitalWrite(segD, SEGMENT_OFF);
248+
digitalWrite(segE, SEGMENT_OFF);
249+
digitalWrite(segF, SEGMENT_ON);
250+
digitalWrite(segG, SEGMENT_ON);
251+
break;
252+
253+
case 5:
254+
digitalWrite(segA, SEGMENT_ON);
255+
digitalWrite(segB, SEGMENT_OFF);
256+
digitalWrite(segC, SEGMENT_ON);
257+
digitalWrite(segD, SEGMENT_ON);
258+
digitalWrite(segE, SEGMENT_OFF);
259+
digitalWrite(segF, SEGMENT_ON);
260+
digitalWrite(segG, SEGMENT_ON);
261+
break;
262+
263+
case 6:
264+
digitalWrite(segA, SEGMENT_ON);
265+
digitalWrite(segB, SEGMENT_OFF);
266+
digitalWrite(segC, SEGMENT_ON);
267+
digitalWrite(segD, SEGMENT_ON);
268+
digitalWrite(segE, SEGMENT_ON);
269+
digitalWrite(segF, SEGMENT_ON);
270+
digitalWrite(segG, SEGMENT_ON);
271+
break;
272+
273+
case 7:
274+
digitalWrite(segA, SEGMENT_ON);
275+
digitalWrite(segB, SEGMENT_ON);
276+
digitalWrite(segC, SEGMENT_ON);
277+
digitalWrite(segD, SEGMENT_OFF);
278+
digitalWrite(segE, SEGMENT_OFF);
279+
digitalWrite(segF, SEGMENT_OFF);
280+
digitalWrite(segG, SEGMENT_OFF);
281+
break;
282+
283+
case 8:
284+
digitalWrite(segA, SEGMENT_ON);
285+
digitalWrite(segB, SEGMENT_ON);
286+
digitalWrite(segC, SEGMENT_ON);
287+
digitalWrite(segD, SEGMENT_ON);
288+
digitalWrite(segE, SEGMENT_ON);
289+
digitalWrite(segF, SEGMENT_ON);
290+
digitalWrite(segG, SEGMENT_ON);
291+
break;
292+
293+
case 9:
294+
digitalWrite(segA, SEGMENT_ON);
295+
digitalWrite(segB, SEGMENT_ON);
296+
digitalWrite(segC, SEGMENT_ON);
297+
digitalWrite(segD, SEGMENT_ON);
298+
digitalWrite(segE, SEGMENT_OFF);
299+
digitalWrite(segF, SEGMENT_ON);
300+
digitalWrite(segG, SEGMENT_ON);
301+
break;
302+
303+
case 10:
304+
digitalWrite(segA, SEGMENT_OFF);
305+
digitalWrite(segB, SEGMENT_OFF);
306+
digitalWrite(segC, SEGMENT_OFF);
307+
digitalWrite(segD, SEGMENT_OFF);
308+
digitalWrite(segE, SEGMENT_OFF);
309+
digitalWrite(segF, SEGMENT_OFF);
310+
digitalWrite(segG, SEGMENT_OFF);
311+
break;
312+
}
313+
}

0 commit comments

Comments
 (0)