Created
January 29, 2015 04:16
-
-
Save binaryatrocity/c7f54c3a958d004aa426 to your computer and use it in GitHub Desktop.
BirdCounter
This file contains hidden or 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
/* | |
Platform: Arduino Uno | |
Monitor an infrared breakbeam sensor (digital port 4) for interruptions | |
and increment a LED display with a counter. | |
*/ | |
#include <Wire.h> | |
#include "Adafruit_LEDBackpack.h" | |
#include "Adafruit_GFX.h" | |
Adafruit_AlphaNum4 alpha4 = Adafruit_AlphaNum4(); | |
#define LEDPIN 13 | |
#define SENSORPIN 4 | |
int sensorState = 0, lastState=0; | |
int birdCounter = 0; | |
char displaybuffer[4] = { | |
' ', ' ', ' ', ' '}; | |
void setup() { | |
// initialize the LED pin as an output: | |
pinMode(LEDPIN, OUTPUT); | |
// initialize the sensor pin as an input: | |
pinMode(SENSORPIN, INPUT); | |
digitalWrite(SENSORPIN, HIGH); // turn on the pullup | |
alpha4.begin(0x70); | |
alpha4.clear(); | |
alpha4.writeDisplay(); | |
} | |
void loop(){ | |
sensorState = digitalRead(SENSORPIN); | |
if (sensorState == LOW) { | |
digitalWrite(LEDPIN, HIGH); | |
} | |
else { | |
digitalWrite(LEDPIN, LOW); | |
} | |
if (!sensorState && lastState) { | |
// update our counter by one | |
birdCounter++; | |
// split our integer into digits | |
displaybuffer[0] = (char)(((int)'0')+(birdCounter%10)); | |
displaybuffer[1] = (char)(((int)'0')+((birdCounter/10)%10)); | |
displaybuffer[2] = (char)(((int)'0')+((birdCounter/100)%10)); | |
displaybuffer[3] = (char)(((int)'0')+(birdCounter/1000)); | |
} | |
lastState = sensorState; | |
// put the digits into buffer | |
alpha4.writeDigitAscii(0, displaybuffer[3]); | |
alpha4.writeDigitAscii(1, displaybuffer[2]); | |
alpha4.writeDigitAscii(2, displaybuffer[1]); | |
alpha4.writeDigitAscii(3, displaybuffer[0]); | |
// push our buffer to display | |
alpha4.writeDisplay(); | |
delay(500); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment