code modification trying to allow fade to any color - this wont fade back to white and just loops
// This #include statement was automatically added by the Particle IDE.
#include <neopixel.h>
#define LEDPIN D3
#define PROBE A1
#define LED01 D4
#define LED02 D2
#define PIXEL_COUNT 12
#define PIXEL_TYPE WS2812
Adafruit_NeoPixel ring = Adafruit_NeoPixel(PIXEL_COUNT, LEDPIN, PIXEL_TYPE);
typedef struct{
uint8_t r;
uint8_t g;
uint8_t b;
}c;
typedef struct {
uint8_t mode = 0;
c color[PIXEL_COUNT];
bool loop = false;
int dir = -1;
// timer t;
}LED;
LED myLEDs;
uint8_t count = HIGH; //LED blink for checking timer state
unsigned long timer = 0;
uint32_t tdelay = 0;
bool test = false;
int ledColor[3];
int setColor[3];
float factor[3]= {1,1,1};
float colorVals[3];
int colorFactor=255;
int mode;
void getFactor(){
int f[3];
for (int i=0; i<3; i++){
f[i] = ledColor[i]-setColor[i];
}
int maxnum = max(abs(f[0]),abs(f[1]));
maxnum=max(maxnum,abs(f[2]));
colorFactor = maxnum;
for (int i=0; i<3; i++){
factor[i]=f[i]/maxnum;
colorVals[i]=ledColor[i];
}
}
int runFade(String command){
int input = command.toInt();
tdelay = round(input*60*1000/255);
if (test){
tdelay = tdelay/10;
}
myLEDs.dir = -1;
myLEDs.loop = true;
digitalWrite(LED01,HIGH);
timer = millis();
return input;
}
int setLEDColor(String command){
char tempmsg[12];
char *msgNdx; // pointer index for parsing input message into tokens
strcpy(tempmsg,command); //store first part of message as char array
msgNdx = strtok(tempmsg, ":,"); //split on : ,
setColor[0] = atoi(msgNdx);
msgNdx = strtok(NULL, ":,");
setColor[1]= atoi(msgNdx);
msgNdx = strtok(NULL, ":,");
setColor[2] = atoi(msgNdx);
getFactor();
return 1;
}
void setLEDs(int S,int F,int* color){
uint32_t c = ring.Color(color[0],color[1],color[2]);
constrain(F, S, PIXEL_COUNT);
for (int i = S; i<F; i++){
ring.setPixelColor(i,c);
}
ring.show();
delay(1);
}
void setup() {
ring.begin();
ring.show();
Particle.function("fade over minutes", runFade);
Particle.function("LED Color", setLEDColor);
pinMode(LED02,OUTPUT);
pinMode(LED01,OUTPUT);
for (int i=0; i<3; i++){
ledColor[i]=255;
}
setLEDs(0,PIXEL_COUNT,ledColor);
}
void loop() {
if (myLEDs.loop == true){
if (millis()-timer>=tdelay){
count = !count;
digitalWrite(LED02,count );
digitalWrite(LED01,count );
fade(myLEDs.dir);
timer = millis();
}
}
}
bool check(int* A,int* B){
for (int i=0; i<3; i++){
if (A[i]!=B[i]){
return false;
}
}
return true;
}
void fade(int d){
int white[3]={255,255,255};
if (check(ledColor,white)){
//if ledColor is white
myLEDs.loop = false; //stop loop
mode=0;//waiting
}
else if (check(ledColor,setColor)){
//if ledColor and setColor are the same
myLEDs.dir = 1;
mode=2;//return to white
}
if (d == 1){
digitalWrite(LED02,HIGH);
//to white
for (int i=0; i<3; i++){
ledColor[i]++;
constrain(ledColor[i],0,255);
}
setLEDs(0,PIXEL_COUNT,ledColor);
}
else if (d == -1){
digitalWrite(LED01,HIGH);
//to other color
for (int i=0; i<3; i++){
colorVals[i] = colorVals[i]-factor[i];
ledColor[i] = round(colorVals[i]);
constrain(ledColor[i],setColor[i],255);
}
setLEDs(0,PIXEL_COUNT,ledColor);
}
}
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. .