Created
February 8, 2017 14:40
-
-
Save hhayley/567aa18cffafc90c10aef87cadddbb14 to your computer and use it in GitHub Desktop.
A Brush Candle
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 "Interval.h" | |
/* | |
A Brush Candle - Hayley Hwang | |
for Lighting & Interactivity Class | |
- NeoPixel RGBW x 1 | |
- Piezo Sensor x 1 | |
- 1mOhm x 1 | |
*/ | |
#include <Adafruit_NeoPixel.h> | |
Interval slowTimer; | |
Interval fastTimer; | |
#define PIN 6 | |
const int numPixels = 7; // Adafruit Jewel | |
int val = 0; | |
int pval = 0; | |
Adafruit_NeoPixel strip = Adafruit_NeoPixel(numPixels, PIN, NEO_RGBW + NEO_KHZ800); | |
int alpha; // Current value of the pixels | |
int dir = 1; // Direction of the pixels... 1 = getting brighter, 0 = getting dimmer | |
int flip; // Randomly flip the direction every once in a while | |
int minAlpha = 25; // Min value of brightness | |
int maxAlpha = 100; // Max value of brightness | |
int alphaDelta = 5; // Delta of brightness between times through the loop | |
void setup() { | |
Serial.begin(9600); | |
strip.begin(); | |
strip.show(); | |
slowTimer.setInterval(slowCandle, 100); | |
fastTimer.setInterval(fastCandle, 50); | |
} | |
void loop() { | |
val = analogRead(A0); | |
// Serial.println(val); | |
flip = random(32); | |
if (flip > val) { | |
dir = 1 - dir; | |
} | |
// Some example procedures showing how to display to the pixels: | |
if (dir == 1) { | |
alpha += alphaDelta; | |
} | |
if (dir == 0) { | |
alpha -= alphaDelta; | |
} | |
if (alpha < minAlpha) { | |
alpha = minAlpha; | |
dir = 1; | |
} | |
if (alpha > maxAlpha) { | |
alpha = maxAlpha; | |
dir = 0; | |
} | |
// if (val < 1) { | |
// candleSet(1); | |
// } else { | |
// candleSet(2); | |
// } | |
int temp = pval + val; //To subtact the noise 0 | |
Serial.println(temp); | |
if (temp > 3) { | |
candleSet(2); | |
// fastTimer.check(); | |
} else { | |
candleSet(1); | |
// slowTimer.check(); | |
} | |
pval = val; | |
strip.show(); | |
} | |
void candleSet(int state) { | |
if (state == 1) { | |
//slow | |
strip.setPixelColor(0, alpha / 5, alpha * 1.5, 0, 10); // orange - middle | |
strip.setPixelColor(1, alpha / 3, alpha, 0, alpha ); // white - top | |
strip.setPixelColor(4, 10, 0, 30); // cyan - bottom | |
strip.setPixelColor(2, 0, 0, 0, 0); | |
strip.setPixelColor(3, 0, 0, 0, 0); | |
strip.setPixelColor(5, 0, 0, 0, 0); | |
strip.setPixelColor(6, 0, 0, 0, 0); | |
} else if (state == 2) { | |
//Fast | |
strip.setPixelColor(0, alpha / 3, alpha, 0, alpha / 2); //middle | |
strip.setPixelColor(4, alpha / 10, alpha / 2, 0); //bottom | |
strip.setPixelColor(1, 0, 0, 0, 0); | |
strip.setPixelColor(5, 0, 0, 0, 0); | |
strip.setPixelColor(2, 0, 0, 0, 0); | |
strip.setPixelColor(6, 0, 0, 0, 0); | |
strip.setPixelColor(3, 0, 0, 0, 0); | |
} | |
strip.show(); | |
// Serial.println(alpha); | |
} | |
void slowCandle() { | |
//slow | |
strip.setPixelColor(0, alpha / 5, alpha * 1.5, 0, 10); // orange - middle | |
strip.setPixelColor(1, alpha / 3, alpha, 0, alpha ); // white - top | |
strip.setPixelColor(4, 10, 0, 30); // cyan - bottom | |
strip.setPixelColor(2, 0, 0, 0, 0); | |
strip.setPixelColor(3, 0, 0, 0, 0); | |
strip.setPixelColor(5, 0, 0, 0, 0); | |
strip.setPixelColor(6, 0, 0, 0, 0); | |
} | |
void fastCandle() { | |
//Fast | |
strip.setPixelColor(0, alpha / 3, alpha, 0, alpha / 2); //middle | |
strip.setPixelColor(4, alpha / 10, alpha / 2, 0); //bottom | |
strip.setPixelColor(1, 0, 0, 0, 0); | |
strip.setPixelColor(5, 0, 0, 0, 0); | |
strip.setPixelColor(2, 0, 0, 0, 0); | |
strip.setPixelColor(6, 0, 0, 0, 0); | |
strip.setPixelColor(3, 0, 0, 0, 0); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment