-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
4cbc674
commit 4edfe23
Showing
1 changed file
with
313 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,313 @@ | ||
/* | ||
6-13-2011 | ||
Spark Fun Electronics 2011 | ||
Nathan Seidle | ||
This code is public domain but you buy me a beer if you use this and we meet | ||
someday (Beerware license). | ||
4 digit 7 segment display: | ||
http://www.sparkfun.com/products/9483 | ||
Datasheet: | ||
http://www.sparkfun.com/datasheets/Components/LED/7-Segment/YSD-439AR6B-35.pdf | ||
This is an example of how to drive a 7 segment LED display from an ATmega | ||
without the use of current limiting resistors. This technique is very common | ||
but requires some knowledge of electronics - you do run the risk of dumping | ||
too much current through the segments and burning out parts of the display. | ||
If you use the stock code you should be ok, but be careful editing the | ||
brightness values. | ||
This code should work with all colors (red, blue, yellow, green) but the | ||
brightness will vary from one color to the next because the forward voltage | ||
drop of each color is different. This code was written and calibrated for the | ||
red color. | ||
This code will work with most Arduinos but you may want to re-route some of | ||
the pins. | ||
7 segments | ||
4 digits | ||
1 colon | ||
= | ||
12 pins required for full control | ||
*/ | ||
|
||
/* | ||
People counter sketch modified by Frollard on October 2 2012 | ||
checks an analog input, when it reaches a threshhold it increments a counter, and won't | ||
increment that counter again until another lower reset threshhold has been hit. | ||
*/ | ||
|
||
//global: | ||
int peopleCount = 0; //total count of people, init zero | ||
bool currentState = 1; //are we even listening for new hits? | ||
#define LDRPin A2 //pin to read LDR | ||
#define ledPin 13 //output led pin | ||
int light_sensitivity = 700; //This is the approx value of light surrounding your LDR | ||
int countTrigger = light_sensitivity + 75; //value the LDR exceeds to count someone | ||
int resetTrigger = light_sensitivity + 60; //value at which to start counting again | ||
//it wouldn't be a bad idea in void loop to keep averaging the current value and | ||
//adjusting all these values in case the lighting changes. | ||
|
||
//**********digit declares | ||
int segA = A1; //Display pin 14 | ||
int segB = 3; //Display pin 16 | ||
int segC = 4; //Display pin 13 | ||
int segD = 5; //Display pin 3 | ||
int segE = A0; //Display pin 5 | ||
int segF = 7; //Display pin 11 | ||
int segG = 8; //Display pin 15 | ||
|
||
int digit1 = 11; //PWM Display pin 1 | ||
int digit2 = 10; //PWM Display pin 2 | ||
int digit3 = 9; //PWM Display pin 6 | ||
int digit4 = 6; //PWM Display pin 8 | ||
//***********end digit declares | ||
|
||
void setup() { | ||
pinMode(LDRPin, INPUT); // declare the LDR as an INPUT | ||
pinMode(ledPin, OUTPUT); // declare the ledPin as an OUTPUT | ||
|
||
pinMode(segA, OUTPUT); | ||
pinMode(segB, OUTPUT); | ||
pinMode(segC, OUTPUT); | ||
pinMode(segD, OUTPUT); | ||
pinMode(segE, OUTPUT); | ||
pinMode(segF, OUTPUT); | ||
pinMode(segG, OUTPUT); | ||
|
||
pinMode(digit1, OUTPUT); | ||
pinMode(digit2, OUTPUT); | ||
pinMode(digit3, OUTPUT); | ||
pinMode(digit4, OUTPUT); | ||
Serial.begin(9600); | ||
} | ||
|
||
void loop() | ||
{ | ||
checkInput(); //reads the ldr and counts if necessary. | ||
displayNumber(peopleCount); | ||
|
||
} | ||
//******Averages the LDR reading, then acts upon it, counting, and toggling the led. | ||
void checkInput() | ||
{ | ||
int LDRValue = 0; | ||
//remove noise by taking 5 readings then averaging | ||
for (int i = 0; i<10; i++){ | ||
delay(1); //may not be necessary but analogRead() can be messy when next to pwm | ||
LDRValue += analogRead(LDRPin); | ||
} | ||
LDRValue = LDRValue / 10; | ||
|
||
|
||
if (currentState == 1) //actively looking for a person | ||
{ //if true, act on the LDR value to attempt to count | ||
if (LDRValue >= countTrigger){ //if ldr goes above trigger we found someone | ||
currentState = 0; //stop listening for new counts until it gets bright again | ||
peopleCount++; //increment the count | ||
digitalWrite(ledPin,HIGH); //led on | ||
|
||
} | ||
} | ||
else //currentstate was false, don't check for people, check for reset | ||
{ | ||
if (LDRValue <= resetTrigger){ //if value was below the reset threshhold | ||
currentState = 1; //start listening again | ||
digitalWrite(ledPin,LOW); //led off | ||
} | ||
|
||
} | ||
|
||
} | ||
|
||
//Given a number, we display 10:22 | ||
//After running through the 4 numbers, the display is left turned off | ||
|
||
//Display brightness | ||
//Each digit is on for a certain amount of microseconds | ||
//Then it is off until we have reached a total of 20ms for the function call | ||
//Let's assume each digit is on for 1000us | ||
//Each digit is on for 1ms, there are 4 digits, so the display is off for 16ms. | ||
//That's a ratio of 1ms to 16ms or 6.25% on time (PWM). | ||
//Let's define a variable called brightness that varies from: | ||
//5000 blindingly bright (15.7mA current draw per digit) | ||
//2000 shockingly bright (11.4mA current draw per digit) | ||
//1000 pretty bright (5.9mA) | ||
//500 normal (3mA) | ||
//200 dim but readable (1.4mA) | ||
//50 dim but readable (0.56mA) | ||
//5 dim but readable (0.31mA) | ||
//1 dim but readable in dark (0.28mA) | ||
|
||
void displayNumber(int toDisplay) { | ||
#define DISPLAY_BRIGHTNESS 500 | ||
|
||
#define DIGIT_ON HIGH | ||
#define DIGIT_OFF LOW | ||
|
||
long beginTime = millis(); | ||
|
||
int initialToDisplay = toDisplay; | ||
|
||
for(int digit = 4 ; digit > 0 ; digit--) { | ||
|
||
//Turn on a digit for a short amount of time | ||
switch(digit) { | ||
case 1: | ||
if (initialToDisplay>999) digitalWrite(digit1, DIGIT_ON); | ||
break; | ||
case 2: | ||
if (initialToDisplay>99) digitalWrite(digit2, DIGIT_ON); | ||
break; | ||
case 3: | ||
if (initialToDisplay>9) digitalWrite(digit3, DIGIT_ON); | ||
break; | ||
case 4: | ||
digitalWrite(digit4, DIGIT_ON); | ||
break; | ||
} | ||
|
||
//Turn on the right segments for this digit | ||
lightNumber(toDisplay % 10); | ||
toDisplay /= 10; | ||
|
||
delayMicroseconds(DISPLAY_BRIGHTNESS); | ||
//Display digit for fraction of a second (1us to 5000us, 500 is pretty good) | ||
|
||
//Turn off all segments | ||
lightNumber(10); | ||
|
||
//Turn off all digits | ||
digitalWrite(digit1, DIGIT_OFF); | ||
digitalWrite(digit2, DIGIT_OFF); | ||
digitalWrite(digit3, DIGIT_OFF); | ||
digitalWrite(digit4, DIGIT_OFF); | ||
} | ||
|
||
while( (millis() - beginTime) < 10) ; | ||
//Wait for 20ms to pass before we paint the display again | ||
} | ||
|
||
//Given a number, turns on those segments | ||
//If number == 10, then turn off number | ||
void lightNumber(int numberToDisplay) { | ||
|
||
#define SEGMENT_ON LOW | ||
#define SEGMENT_OFF HIGH | ||
|
||
switch (numberToDisplay){ | ||
|
||
case 0: | ||
digitalWrite(segA, SEGMENT_ON); | ||
digitalWrite(segB, SEGMENT_ON); | ||
digitalWrite(segC, SEGMENT_ON); | ||
digitalWrite(segD, SEGMENT_ON); | ||
digitalWrite(segE, SEGMENT_ON); | ||
digitalWrite(segF, SEGMENT_ON); | ||
digitalWrite(segG, SEGMENT_OFF); | ||
break; | ||
|
||
case 1: | ||
digitalWrite(segA, SEGMENT_OFF); | ||
digitalWrite(segB, SEGMENT_ON); | ||
digitalWrite(segC, SEGMENT_ON); | ||
digitalWrite(segD, SEGMENT_OFF); | ||
digitalWrite(segE, SEGMENT_OFF); | ||
digitalWrite(segF, SEGMENT_OFF); | ||
digitalWrite(segG, SEGMENT_OFF); | ||
break; | ||
|
||
case 2: | ||
digitalWrite(segA, SEGMENT_ON); | ||
digitalWrite(segB, SEGMENT_ON); | ||
digitalWrite(segC, SEGMENT_OFF); | ||
digitalWrite(segD, SEGMENT_ON); | ||
digitalWrite(segE, SEGMENT_ON); | ||
digitalWrite(segF, SEGMENT_OFF); | ||
digitalWrite(segG, SEGMENT_ON); | ||
break; | ||
|
||
case 3: | ||
digitalWrite(segA, SEGMENT_ON); | ||
digitalWrite(segB, SEGMENT_ON); | ||
digitalWrite(segC, SEGMENT_ON); | ||
digitalWrite(segD, SEGMENT_ON); | ||
digitalWrite(segE, SEGMENT_OFF); | ||
digitalWrite(segF, SEGMENT_OFF); | ||
digitalWrite(segG, SEGMENT_ON); | ||
break; | ||
|
||
case 4: | ||
digitalWrite(segA, SEGMENT_OFF); | ||
digitalWrite(segB, SEGMENT_ON); | ||
digitalWrite(segC, SEGMENT_ON); | ||
digitalWrite(segD, SEGMENT_OFF); | ||
digitalWrite(segE, SEGMENT_OFF); | ||
digitalWrite(segF, SEGMENT_ON); | ||
digitalWrite(segG, SEGMENT_ON); | ||
break; | ||
|
||
case 5: | ||
digitalWrite(segA, SEGMENT_ON); | ||
digitalWrite(segB, SEGMENT_OFF); | ||
digitalWrite(segC, SEGMENT_ON); | ||
digitalWrite(segD, SEGMENT_ON); | ||
digitalWrite(segE, SEGMENT_OFF); | ||
digitalWrite(segF, SEGMENT_ON); | ||
digitalWrite(segG, SEGMENT_ON); | ||
break; | ||
|
||
case 6: | ||
digitalWrite(segA, SEGMENT_ON); | ||
digitalWrite(segB, SEGMENT_OFF); | ||
digitalWrite(segC, SEGMENT_ON); | ||
digitalWrite(segD, SEGMENT_ON); | ||
digitalWrite(segE, SEGMENT_ON); | ||
digitalWrite(segF, SEGMENT_ON); | ||
digitalWrite(segG, SEGMENT_ON); | ||
break; | ||
|
||
case 7: | ||
digitalWrite(segA, SEGMENT_ON); | ||
digitalWrite(segB, SEGMENT_ON); | ||
digitalWrite(segC, SEGMENT_ON); | ||
digitalWrite(segD, SEGMENT_OFF); | ||
digitalWrite(segE, SEGMENT_OFF); | ||
digitalWrite(segF, SEGMENT_OFF); | ||
digitalWrite(segG, SEGMENT_OFF); | ||
break; | ||
|
||
case 8: | ||
digitalWrite(segA, SEGMENT_ON); | ||
digitalWrite(segB, SEGMENT_ON); | ||
digitalWrite(segC, SEGMENT_ON); | ||
digitalWrite(segD, SEGMENT_ON); | ||
digitalWrite(segE, SEGMENT_ON); | ||
digitalWrite(segF, SEGMENT_ON); | ||
digitalWrite(segG, SEGMENT_ON); | ||
break; | ||
|
||
case 9: | ||
digitalWrite(segA, SEGMENT_ON); | ||
digitalWrite(segB, SEGMENT_ON); | ||
digitalWrite(segC, SEGMENT_ON); | ||
digitalWrite(segD, SEGMENT_ON); | ||
digitalWrite(segE, SEGMENT_OFF); | ||
digitalWrite(segF, SEGMENT_ON); | ||
digitalWrite(segG, SEGMENT_ON); | ||
break; | ||
|
||
case 10: | ||
digitalWrite(segA, SEGMENT_OFF); | ||
digitalWrite(segB, SEGMENT_OFF); | ||
digitalWrite(segC, SEGMENT_OFF); | ||
digitalWrite(segD, SEGMENT_OFF); | ||
digitalWrite(segE, SEGMENT_OFF); | ||
digitalWrite(segF, SEGMENT_OFF); | ||
digitalWrite(segG, SEGMENT_OFF); | ||
break; | ||
} | ||
} |