Created
October 30, 2015 15:12
-
-
Save dmiddlecamp/6a89683295c29807df23 to your computer and use it in GitHub Desktop.
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
// This #include statement was automatically added by the Particle IDE. | |
#include "neopixel/neopixel.h" | |
#include "math.h" | |
SYSTEM_MODE(MANUAL); | |
#define RANDOM_SPIRITS true | |
#define RANDOM_SPIRIT_CHANCE 0.10 | |
// // This #include statement was automatically added by the Spark IDE. | |
// #include "SparkButton/SparkButton.h" | |
// IMPORTANT: Set pixel COUNT, PIN and TYPE | |
#define STRIP_PIXEL_PIN D0 | |
#define STRIP_PIXEL_COUNT 40 | |
#define STRIP_PIXEL_TYPE WS2812B | |
Adafruit_NeoPixel strip = Adafruit_NeoPixel(STRIP_PIXEL_COUNT, STRIP_PIXEL_PIN, STRIP_PIXEL_TYPE); | |
bool safe_mode = false; | |
float bright_step = 0.01; | |
float brightness = 0.20; | |
unsigned long lastStep = 0; | |
void setup() | |
{ | |
pinMode(A0, INPUT); | |
pinMode(A2, INPUT); | |
pinMode(A4, INPUT); | |
pinMode(A1, OUTPUT); | |
pinMode(A3, OUTPUT); | |
pinMode(A5, OUTPUT); | |
digitalWrite(A1, LOW); | |
digitalWrite(A3, LOW); | |
digitalWrite(A5, LOW); | |
pinMode(D7, INPUT); | |
if (digitalRead(D7) == HIGH) { | |
Spark.connect(); | |
safe_mode = true; | |
} | |
else { | |
WiFi.off(); | |
} | |
strip.begin(); | |
strip.show(); // Initialize all pixels to 'off' | |
} | |
int redReads[100]; | |
int greenReads[100]; | |
int blueReads[100]; | |
int readIdx = 0; | |
unsigned long lastDraw = 0; | |
int nextPixelIdx = 0; | |
int counter = 0; | |
float PIMultiple = 314159.0; | |
void loop() | |
{ | |
// float red = (analogRead(A0) / 4095.0) * 255; | |
// float green = (analogRead(A2) / 4095.0) * 255; | |
// float blue = (analogRead(A4) / 4095.0) * 255; | |
float red = getScaledPin(A0, redReads, readIdx); | |
float green = getScaledPin(A2, greenReads, readIdx); | |
float blue = getScaledPin(A4, blueReads, readIdx); | |
readIdx++; | |
if (readIdx >= 100) { | |
readIdx = 0; | |
} | |
float brightness = sin(counter / PIMultiple); | |
//float brightness = (random(75) / 100.0); | |
unsigned long now = millis(); | |
if ((now - lastDraw) > 10) { | |
strip.setPixelColor( | |
nextPixelIdx, | |
red * brightness, | |
green * brightness, | |
blue * brightness | |
); | |
strip.show(); | |
nextPixelIdx++; | |
if (nextPixelIdx >= strip.numPixels()) { | |
nextPixelIdx = 0; | |
} | |
lastDraw = now; | |
} | |
counter++; | |
if (counter > (PIMultiple * 2)) { | |
// if we're dark... | |
// if all the PINS GO LOW | |
int total = analogRead(A0) + analogRead(A2) + analogRead(A4); | |
if (total < 1000) { | |
//BOOM TURN ON! | |
counter = 0; | |
} | |
} | |
// unsigned long now = millis(); | |
// if ((now - lastDraw) > 1000) { | |
// for(int i=0; i<strip.numPixels(); i++) { | |
// strip.setPixelColor(i, red, green, blue); | |
// } | |
// strip.show(); | |
// lastDraw = now; | |
// } | |
//delay(1000); | |
} | |
float getScaledPin(int pin, int *pinReads, int pinIdx) { | |
//pinIdx = pinIdx + 1; | |
if (pinIdx >= 100) { | |
pinIdx = 0; | |
} | |
int readValue = analogRead(pin); | |
pinReads[pinIdx] = readValue; | |
float average = 0; | |
int minValue = 10000, maxValue = 0; | |
for(int i=0;i<100;i++) { | |
int val = pinReads[i]; | |
if (val > maxValue) { | |
maxValue = val; | |
} | |
if (val < minValue) { | |
minValue = val; | |
} | |
//average += pinReads[i]; | |
} | |
//average = average / 100.0; | |
if ((maxValue - minValue) < 50) { | |
maxValue += 25; | |
minValue -= 25; | |
} | |
//map(value, fromLow, fromHigh, toLow, toHigh); | |
float mappedValue = map(readValue, minValue, maxValue, 0, 255); | |
return mappedValue; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment