Created
November 30, 2014 06:18
-
-
Save nzkarit/44e8c1ce4510343f75f8 to your computer and use it in GitHub Desktop.
Money Boxes with motion sensing
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
#include <Adafruit_NeoPixel.h> | |
const int xInput = A0; | |
const int yInput = A1; | |
const int zInput = A2; | |
int xBase = 0; | |
int yBase = 0; | |
int zBase = 0; | |
#define PIXELPIN 6 | |
#define NUMPIXELS 8 | |
#define TAMPERWAIT 1 | |
//Easy | |
#define TAMPERVALUE 25 | |
//Mediumn | |
//#define TAMPERVALUE 20 | |
//Hard | |
//#define TAMPERVALUE 15 | |
#define ALARMFLASH 500 | |
#define ALARMTIME 5000 | |
Adafruit_NeoPixel strip = Adafruit_NeoPixel(NUMPIXELS, PIXELPIN, NEO_GRB + NEO_KHZ800); | |
// Take multiple samples to reduce noise | |
const int sampleSize = 10; | |
void setup(){ | |
analogReference(EXTERNAL); | |
xBase = ReadAxis(xInput); | |
yBase = ReadAxis(yInput); | |
zBase = ReadAxis(zInput); | |
strip.begin(); | |
strip.show(); // Initialize all pixels to 'off' | |
} | |
void loop() { | |
int step = 0; | |
int direction = 0; | |
int xDiff = xBase - ReadAxis(xInput); | |
int yDiff = yBase - ReadAxis(yInput); | |
int zDiff = zBase - ReadAxis(zInput); | |
int diff = abs(xDiff) + abs(yDiff) + abs(zDiff); | |
if (diff > TAMPERVALUE){ | |
alarm(); | |
} else { | |
float percentage = (float)diff / (float)TAMPERVALUE; | |
float numLED = percentage * NUMPIXELS; | |
blank(); | |
int red = (int)(255*percentage); | |
int green = (int)(255*(1-percentage)); | |
for(int i = 0; i < numLED; i++){ | |
strip.setPixelColor(i, red, green, 0); | |
} | |
if(1 > numLED){ | |
strip.setPixelColor(0, 0, 0, 255); | |
} | |
strip.show(); | |
delay(TAMPERWAIT); | |
} | |
} | |
// | |
// Read "sampleSize" samples and report the average | |
// | |
int ReadAxis(int axisPin){ | |
long reading = 0; | |
analogRead(axisPin); | |
delay(1); | |
for (int i = 0; i < sampleSize; i++){ | |
reading += analogRead(axisPin); | |
} | |
return reading/sampleSize; | |
} | |
void alarm(){ | |
int iterations = ALARMTIME/ALARMFLASH/2; | |
for(int i = 0; i < iterations; i++){ | |
for(uint16_t j=0; j<strip.numPixels(); j++) { | |
strip.setPixelColor(j, strip.Color(255, 0, 0)); | |
strip.show(); | |
} | |
delay(ALARMFLASH); | |
for(uint16_t j=0; j<strip.numPixels(); j++) { | |
strip.setPixelColor(j, 0); | |
strip.show(); | |
} | |
delay(ALARMFLASH); | |
} | |
analogReference(EXTERNAL); | |
xBase = ReadAxis(xInput); | |
yBase = ReadAxis(yInput); | |
zBase = ReadAxis(zInput); | |
} | |
void blank(){ | |
for(int i=0; i<strip.numPixels(); i++) { | |
strip.setPixelColor(i, 0); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment