Back to Parent

Final Code
//----------------------------------------------------------------------
//Phoebe DeGroot
//AMBIENT ORB
//----------------------------------------------------------------------

#include <neopixel.h>
#define LEDPIN D3
#define PROBE A1
#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; 
    // timer t; 
}LED;

uint8_t count = HIGH; //LED blink for checking timer state
unsigned long timer = 0; 
uint32_t tdelay = 0; 
int dir = -1; 
LED myLEDs; 
bool test = false; 
c ledColor; 

int runFade(String command){
    int input = command.toInt();
    tdelay = round(input*60*1000/255);
    if (test){
        tdelay = tdelay/10;
    }
    dir = -1;
    myLEDs.loop = true; 
    timer = millis();
    
    return input; 
}

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(1);
}

void setup() {
    ring.begin();
    ring.show();
    Particle.function("fade over minutes", runFade);
    pinMode(LED02,OUTPUT);

    ledColor = {255,255,255};
    setLEDs(0,PIXEL_COUNT,ledColor);
}

void loop() {
    if (myLEDs.loop == true){

        if (millis()-timer>=tdelay){
            count = !count;
            digitalWrite(LED02,count );
            fadeREDWHITE(dir);
            timer = millis();
        }
    }
}


void fadeREDWHITE(int d){
    if (d == 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
        
    }
}
Click to Expand

Content Rating

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

0