#include "neopixel.h"
#define PIXEL_PIN D2
#define PIXEL_COUNT 16
#define PIXEL_TYPE WS2812
Adafruit_NeoPixel strip = Adafruit_NeoPixel(PIXEL_COUNT, PIXEL_PIN, PIXEL_TYPE);
// create a global variable to store the time when last performed an action
unsigned long lastFade = 0;
int brightness = 100;
// store rgb values in global variables
int r = 0;
int g = 0;
int b = 0;
// store the name of the color in a global variable
String npColor;
void setup(){
strip.begin();
// initialize all pixels to "off"
strip.show();
Particle.function("Set color", setColor);
Particle.function("Fade to white", fadeToWhite);
Particle.variable("brightness", brightness);
Particle.variable("Neopixel Color", npColor);
// set default color
setColor("red");
}
void loop(){
//call the function to fade to red (input: given time in minute)
fadeToWhite("1");
}
// function to fade from red to white
// function input: fading time in minute
int fadeToWhite(String command){
// convert input command to milliseconds
int totalFadingTime = command.toInt();
int t = ((totalFadingTime * 60) * 1000) / 100;
unsigned long timeNow = millis();
if(brightness <= 100){
if (timeNow - lastFade > t){
for(int i = 0; i < strip.numPixels(); i++){
strip.setPixelColor(i, r, 100-brightness, 100-brightness);
}
strip.show();
brightness = brightness-1;
lastFade = timeNow;
} else{
return -1;
}
}
return 1;
}
// function to set color to white or red
int setColor(String command){
// update the stored neopixel color value
npColor = command;
uint16_t i;
uint32_t white = strip.Color(100, 100, 100);
uint32_t red = strip.Color(100, 0, 0);
if (npColor == "white"){
// set rgb values all to 100 (white)
r = 100;
g = 100;
b = 100;
// turn on neopixel using the rgb values of white
for(i = 0; i < strip.numPixels(); i++){
strip.setPixelColor(i, r, g, b);
strip.show();
}
delay(100);
return 1;
} else if (npColor == "red"){
// set r=100, g & b = 0 (red)
r = 100;
g = 0;
b = 0;
// turn on neopixel using the rgb values of red
for(i = 0; i < strip.numPixels(); i++){
strip.setPixelColor(i, r, g, b);
strip.show();
}
delay(100);
return 1;
} else {
// if the command is neither white or red, return -1
return -1;
}
}
Click to Expand
Content Rating
Is this a good/useful/informative piece of content to include in the project? Have your say!
You must login before you can post a comment. .