Skill Dev III

Made by Felicia Luo

Neopixel fade from white to red before an event about to begin and fade back to white after it has begun.

Created: December 18th, 2022

0

Outcome

0

Process

  • I had to code step by step and test if that part works. I started with lighting up the neopixel once set up and turning it off. Then I lit it up once the Particle.function is call. And then I tested how to change the green and blue value and maintaining the red value so that the light color can fade to red. As a last step, I hook it up to IFTTT.

0

Reflection

  • At first, I coded up everything and it turned out not as expected. As I can't print out anything like in other IDE, I had to go back to test step by step and lit up the neopixel step by step to determine which part went wrong. I could've avoided a lot of laborious decoding by simple test step by step as I coded.

0
// This #include statement was automatically added by the Particle IDE.
#include <neopixel.h>

// IMPORTANT: Set pixel COUNT, PIN and TYPE
#define PIXEL_PIN D2
#define PIXEL_COUNT 8
#define PIXEL_TYPE WS2812

Adafruit_NeoPixel strip = Adafruit_NeoPixel(PIXEL_COUNT, PIXEL_PIN, PIXEL_TYPE);


uint16_t i;
uint32_t off = strip.Color(0, 0, 0);
uint32_t blue = strip.Color(0, 0, 255);
uint32_t red = strip.Color(255, 0, 0);
uint32_t green = strip.Color(0, 255, 0);
uint32_t white = strip.Color(255, 255, 255);

unsigned long lastFade = 0;

void setup() {
    //Register our Particle function here
    Particle.function("NPControl", NPControl);
    

    strip.begin();
    strip.show(); // Initialize all pixels to 'off'
    
    //Neopixel lights up when connected
	for(int j=0; j < strip.numPixels(); j++)
        {
        strip.setPixelColor(j, white);
        strip.show();
        delay(100);
        }
}

void loop() {
}

int NPControl(String command) {
    
    // find out the state of the LED
    int value = command.toInt();

    if( value > 0 ) {
        
        int gb = 255;
        
        // fade from white to red
        while (gb > 0) {
            
            gb -= 1;
            
            for(int j=0; j < strip.numPixels(); j++) {
                    strip.setPixelColor(j, 255, gb, gb);
                    strip.show();
                    }
            delay(30*1000/255); // current fade time is 30 seconds
        }
        
        // fade from red to white
        while (gb < 255) {
            
            gb += 1;
            
            for(int j=0; j < strip.numPixels(); j++) {
                    strip.setPixelColor(j, 255, gb, gb);
                    strip.show();
                    }
            delay(30*1000/255);// current fade time is 30 seconds
        }

	
    }
    
    else {
        return -1;
    }

    return 1;
}
Click to Expand
x
Share this Project

Courses

About

Neopixel fade from white to red before an event about to begin and fade back to white after it has begun.