Back to Parent

Original wip code with multiple led modes - this may have been working but it was hard to debug
//----------------------------------------------------------------------
//Phoebe DeGroot
//AMBIENT ORB
//----------------------------------------------------------------------
#include <neopixel.h>

#define LEDPIN D3
#define PROBE A1

#define PIXEL_COUNT 12
#define PIXEL_TYPE WS2812

Adafruit_NeoPixel ring = Adafruit_NeoPixel(PIXEL_COUNT, LEDPIN, PIXEL_TYPE);

typedef struct {
    unsigned long c_t = 0; 
    uint32_t delay = 0; 
    uint32_t end;
    bool active = true; 
}timer;

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; 
    timer t; 
}LED;

uint32_t delay_1 = 1000; 
unsigned long timer_1 = 0; 

String cmdNames[] = {"OFF","ALL","NUM","CHASE","BLINK","FADE","STORE"};
String loopmodeNames[] = {"FADE","BLINK","CHASE","RED2WHITE"};

c setColor; //temp for setting from command only!
c ledColor; //stored single color
c stored[8]; //stored color pattern
char msgString[8];
char tempmsg[12];
int msgInt[2]= {0,0};
LED myLEDs; 
int dir = 1; 

void setLEDs(int S,int F,c color){
    uint32_t c = ring.Color(color.r,color.g,color.b);
    constrain(F, S, PIXEL_COUNT);
    for (int i = S; i<F; i++){
        ring.setPixelColor(i,c);
    }
    ring.show();
    delay(10);
}

void fadeLEDs(int n1, int n2){
    
}

int setLEDMode(String command){
    msgInt[0],msgInt[1] = 0,0;
    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 : , 
    strcpy(msgString, msgNdx); //store first part of message as char array 
    msgString[0] &= 0xDF; 
    msgString[1] &= 0xDF; //make chars uppercase
    msgNdx = strtok(NULL, ":,"); //continue tokenizing message
    msgInt[0] = atoi(msgNdx); //store next part of message as int
     msgNdx = strtok(NULL, ":,"); //continue tokenizing message
    msgInt[1] = atoi(msgNdx); //store next part of message as int
    getCommand(); //run command with message
    timer_1 = millis();
    return myLEDs.mode; 
}


void store(int n, c color){
    constrain(n, 1, 8);
    stored[n] ={color.r,color.g,color.b};
}

int setLEDColor(String command){
    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.r = atoi(msgNdx);
    msgNdx = strtok(NULL, ":,");
    setColor.g = atoi(msgNdx);
    msgNdx = strtok(NULL, ":,");
    setColor.b = atoi(msgNdx);
    return colortoint(setColor);
}


void getCommand(){
    char cmd = msgString[0];
    int minutes; 
    switch(cmd){
        case 'A':
        myLEDs.loop = false; 
        myLEDs.mode = 4; 
        setLEDs(0,PIXEL_COUNT,setColor);
        break;
        case 'N':
        myLEDs.loop = false; 
        myLEDs.mode = 4; 
        setLEDs(msgInt[0],msgInt[1],setColor);
        break;
        case 'F':
        fadeLEDs(msgInt[0],msgInt[1]);
        myLEDs.mode = 0; 
        break;
        case 'C':
        myLEDs.mode = 2; 
        //chase mode
        break;
        case 'B':
        myLEDs.mode = 1; 
        //blink mode
        break;
        case 'S':
        //store the color at index
        break;
        store(msgInt[0]+1,setColor);
        case 'R':
        //fade to red
        myLEDs.loop = true; 
        myLEDs.mode = 3; 
        minutes = msgInt[0];
        delay_1= 10000;
        myLEDs.t.delay = round(minutes*1000*60/255);
        dir = -1;
        break;
        case 'W':
        //fade to white
        myLEDs.loop = true; 
        myLEDs.mode = 3; 
        minutes = msgInt[0];
        myLEDs.t.delay = round(minutes*1000/255);
        delay_1 = 10000;
        dir = 1; 
        break;
    }
    
}


void setup() {
    ring.begin();
    ring.show();

    Particle.function("LED mode", setLEDMode);
    Particle.function("LED Color", setLEDColor);

    ledColor = {255,255,255};
    setLEDs(0,PIXEL_COUNT,ledColor);
}
int colortoint(c color){
    return color.r*1000000+color.g*1000+color.b; 
    
}

void fadeREDWHITE(){
    if (dir == 1){
        //to white
        ledColor.g= ledColor.g+1;
        ledColor.b= ledColor.b+1;
        
        constrain(ledColor.g,0,255);
        constrain(ledColor.b,0,255);
        setLEDs(0,PIXEL_COUNT,ledColor);
        
        if (ledColor.g>=255) myLEDs.loop = false; //stop loop
    }
    else{
        //to red
        ledColor.g= ledColor.g-1;
        ledColor.b= ledColor.b-1;
        constrain(ledColor.g,0,255);
        constrain(ledColor.b,0,255);
        setLEDs(0,PIXEL_COUNT,ledColor);
        if (ledColor.g<=0) dir = 1;//swap loop direction
        
    }
}
void loop() {
    if (myLEDs.loop == true){
        if (millis()-timer_1>=delay_1){
            switch(myLEDs.mode){
                case 0:
                //fade
                case 1:
                //blink
                case 2:
                //chase
                default:
                    fadeREDWHITE;
            }
            timer_1 = millis();
        }
    }
}
Click to Expand

Content Rating

Is this a good/useful/informative piece of content to include in the project? Have your say!

0