Last active
August 29, 2015 14:17
-
-
Save phoenixperry/a17ac6ccb07a83890514 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 is a minimal example, see extra-examples.cpp for a version | |
* with more explantory documentation, example routines, how to | |
* hook up your pixels and all of the pixel types that are supported. | |
* | |
*/ | |
#include "application.h" | |
//#include "spark_disable_wlan.h" // For faster local debugging only | |
#include "neopixel.h" | |
// IMPORTANT: Set pixel COUNT, PIN and TYPE | |
#define PIXEL_PIN D2 | |
#define PIXEL_COUNT 2 | |
#define PIXEL_TYPE WS2812B | |
Adafruit_NeoPixel strip = Adafruit_NeoPixel(PIXEL_COUNT, PIXEL_PIN, PIXEL_TYPE); | |
//currntly displaying color | |
uint32_t r= 255; | |
uint32_t g= 255; | |
uint32_t b= 255; | |
//variables for fading colors | |
uint32_t current_r = 255; | |
uint32_t current_g=255; | |
uint32_t current_b =255; | |
uint32_t target_r = 255; | |
uint32_t target_b = 255; | |
uint32_t target_g =255; | |
uint32_t col =255; //current color - this will get refactored later. | |
//these ints act as state switches | |
int light_blue = 0; | |
int teal = 1; | |
int pink =0; | |
int grey = 0; | |
int orange =0; | |
int yellow = 0; | |
int cream = 0; | |
int violet = 0; | |
//if I want to faded to colors, I can change this between 0 and 1 | |
int change = 1; | |
//start fade at 0% | |
float pct = 0; | |
//this function changes from one color to another based on the stats in the if statement in the main loop | |
void triggerFade(){ | |
//for as long as we have pixels | |
for(int16_t i=0; i<strip.numPixels(); i++) | |
{ | |
Serial.println("changingpixels"); | |
//change each rgb value one at a time | |
for(int16_t i=0; i< 256; i++) | |
{ | |
//divide by 255 to get closer and closer to 1 | |
pct = i/255; | |
//fade from old color to new | |
r = (1-pct) * current_r + (pct) * target_r; | |
g = (1-pct) * current_b + (pct) * target_b; | |
b = (1-pct) * current_g + (pct) * target_g; | |
//transition the actual pixels | |
col = strip.Color(r,g,b); | |
strip.setPixelColor(i, col); | |
} | |
strip.show(); | |
delay(10); | |
} | |
//make sure the color we set the current color when we are done fading in | |
current_r = r; | |
current_g = b; | |
current_b = b; | |
change = 0; //flip off change | |
} | |
//fucntions for calling from OF | |
int set_light_blue(String command){ | |
return 0; | |
} | |
void setup() | |
{ | |
//color switches just in case I want to query them for later - currently only for debugging | |
Spark.variable("light_blue", &light_blue, INT); | |
Spark.variable("teal", &teal, INT); | |
Spark.variable("pink", &pink, INT); | |
Spark.variable("grey", &pink, INT); | |
Spark.variable("orange", &pink, INT); | |
Spark.variable("yellow", &pink, INT); | |
Spark.variable("cream", &pink, INT); | |
Spark.variable("violet", &pink, INT); | |
//ultimately I'll have an exposed function for each color to change the active color. right now only this one is stubbed in. | |
Spark.function("set_light_blue", set_light_blue); | |
//code required to start the strip up | |
strip.begin(); | |
strip.show(); | |
// Make sure your Serial Terminal app is closed before powering your Core | |
Serial.begin(9600); | |
// Now open your Serial Terminal, and hit any key to continue! | |
while(!Serial.available()) SPARK_WLAN_Loop(); | |
} | |
void loop() | |
{ | |
//if you want to change colors | |
if(change==1){ | |
//see which color to change to and call the fade function | |
if(light_blue==1){target_r=0; target_g=0; target_b=255; triggerFade();} | |
else if(teal==1){target_r=0; target_g=155; target_b=100;Serial.println("teal");triggerFade();} | |
else if(pink==1){target_r=0; target_g=0; target_b=0;triggerFade();} | |
else if(grey==1){target_r=0; target_g=0; target_b=0;triggerFade();} | |
else if(orange==1){target_r=0; target_g=0; target_b=0;triggerFade();} | |
else if(yellow==1){target_r=0; target_g=0; target_b=0;triggerFade();} | |
else if(cream==1){target_r=0; target_g=0; target_b=0;triggerFade();} | |
//violet is last! | |
else { target_r=0; target_g=0; target_b=0;triggerFade();} | |
} | |
// if there's no change | |
else{ | |
//show all colors at the current color | |
for(int i=0; i< strip.numPixels()+1; i++) | |
{ | |
Serial.println("got to else"); | |
col = strip.Color(r,g,b); | |
strip.setPixelColor(i, col); | |
} | |
} | |
strip.show(); | |
delay(100); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment