Created
March 30, 2012 20:57
-
-
Save mganucheau/2254923 to your computer and use it in GitHub Desktop.
LPD8806 PIR Interrupt
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 "LPD8806.h" | |
#include "SPI.h" | |
int dataPin = 2; | |
int clockPin = 3; | |
int pirPin = 7; //digital 7 | |
int stripSpeed = 10; | |
int pirVal = 0; // PIR sensor | |
LPD8806 strip = LPD8806(32, dataPin, clockPin); | |
void setup() { | |
Serial.begin(9600); | |
strip.begin(); | |
strip.show(); // write all the pixels out | |
pinMode(pirPin, INPUT); | |
} | |
void loop() { | |
pirVal = digitalRead(pirPin); | |
Serial.println(pirVal); | |
switch (pirVal) { | |
case LOW: | |
strip.show(); // write all the pixels out | |
rainbowCycle(stripSpeed); // make it go through the cycle fairly fast | |
break; | |
case HIGH: | |
strip.show(); // write all the pixels out | |
colorWipe(strip.Color(127,0,0), stripSpeed); // red | |
break; | |
} | |
} | |
//------------------------------------------- | |
void rainbowCycle(uint8_t wait) { | |
uint16_t i, j; | |
for (j=0; j < 384 * 5; j++) { // 5 cycles of all 384 colors in the wheel | |
for (i=0; i < strip.numPixels(); i++) { | |
// tricky math! we use each pixel as a fraction of the full 384-color wheel | |
// (thats the i / strip.numPixels() part) | |
// Then add in j which makes the colors go around per pixel | |
// the % 384 is to make the wheel cycle around | |
strip.setPixelColor(i, Wheel( ((i * 384 / strip.numPixels()) + j) % 384) ); | |
} | |
strip.show(); // write all the pixels out | |
switch (pirVal) { | |
case LOW: | |
delay(wait); | |
break; | |
case HIGH: | |
delay(0); | |
break; | |
} | |
} | |
} | |
void colorWipe(uint32_t c, uint8_t wait) { | |
int i; | |
for (i=0; i < strip.numPixels(); i++) { | |
strip.setPixelColor(i, c); | |
strip.show(); | |
delay(wait); | |
} | |
} | |
/* Helper functions */ | |
//Input a value 0 to 384 to get a color value. | |
//The colours are a transition r - g -b - back to r | |
uint32_t Wheel(uint16_t WheelPos) { | |
byte r, g, b; | |
switch(WheelPos / 128) { | |
case 0: | |
r = 127 - WheelPos % 128; //Red down | |
g = WheelPos % 128; // Green up | |
b = 0; //blue off | |
break; | |
case 1: | |
g = 127 - WheelPos % 128; //green down | |
b = WheelPos % 128; //blue up | |
r = 0; //red off | |
break; | |
case 2: | |
b = 127 - WheelPos % 128; //blue down | |
r = WheelPos % 128; //red up | |
g = 0; //green off | |
break; | |
} | |
return(strip.Color(r,g,b)); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment