Back to Parent

// RECEIVING DEVICE CODE

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

// We will be using D2 to control our LED
int ledPin = D2;

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

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

//Set RGB colors
int r = 0;
int g = 0;
int b = 0;

//Define variable to trigger startup fade
bool startupTrigger = false;

//Set loop speed
int speed = 50; 

//Store blink state
bool doBlink = false;

//Initiate blink success state
bool doBlinksuccess = false;

//Initiate millis fade calculator & fadeOut
unsigned long lastFade = 0;
int fadeOut = 255;
int fadeIn = 0;

void setup()
{
    //Initiate neopixels to off
    strip.begin();
    strip.show();
    
    //Initiate addedToPlaylist fallback function with call "addedToPlaylist" 
    Particle.subscribe( "addedToPlaylist.Success", addedToPlaylist );

    startupGlow();


}

//Turns all LED's to specified color (r, g, b) with adjustable ms delay
void startupLED(int r, int g, int b, int speed) {
    for (int i = 0; i < strip.numPixels(); i++) {
                strip.setPixelColor(i, r, g, b);
                strip.show(); 
                delay(speed);
        }
}

void startupGlow() {
    startupLED(0, 150, 150, 50);
    startupLED(0, 50, 255, 50);
    lastFade = 0;
    fadeOut = 255;    
    for (int fadeOut = 255; fadeOut > 0; fadeOut--) {

        for (int i = 0; i < strip.numPixels(); i++) {
            //Pulse to green
            strip.setPixelColor(i, 255 - fadeOut, max(50, 255 - fadeOut), 255); 
            strip.show();
        }
        
        delay(1);
    }
}

//using rgb, input light pulse when change of genre or startup song
void successPulse(int r, int g, int b) {
    
    lastFade = 0;
    fadeOut = 255; 
    for (int fadeOut = 255; fadeOut > 0; fadeOut--) {

        for (int i = 0; i < strip.numPixels(); i++) {
            //Pulse to green
            strip.setPixelColor(i, max(0, r - (255 - fadeOut)), min(255, g + (255 - fadeOut)), max(0, b - (255 - fadeOut))); 
            strip.show();
        }
    }
    r = 255;
    g = 50;
    b = 140;
    for (int fadeIn = 0; fadeIn < 255; fadeIn++) {

        for (int i = 0; i < strip.numPixels(); i++) {
            //Pulse to original color
            strip.setPixelColor(i, max(r, fadeIn), min(g, 255 - fadeIn), max(b,fadeIn)); 
            strip.show();
        }
        delay(10);
    }
}

//Turns all LED's to specified color (r, g, b) 
void setLED(int r, int g, int b) {
    for (int i = 0; i < strip.numPixels(); i++) {
            strip.setPixelColor(i, r, g, b);
            strip.show(); 
        }
}

//Turns all LED's to white
void setwhite( ){
    for (int i = 0; i < strip.numPixels(); i++) {
                strip.setPixelColor(i, 255, 255, 255);
                strip.show(); 
        }
}

//Turns NeoPixelOff
void neoPixelOff( ){
    for (int i = 0; i < strip.numPixels(); i++) {
                strip.setPixelColor(i, 0, 0, 0);
                strip.show(); 
        }
}

//Blink LEDs to input color
void blinkLED( int times, int r, int g, int b){
    
    for( int i = 0; i < times; i++) {
        for( int i = 0; i < strip.numPixels(); i++ ){
        strip.setPixelColor(i, r, g, b); // set blue 
        strip.show();
        }
        delay(500);
        for( int i = 0; i < strip.numPixels(); i++ ){
        strip.setPixelColor(i, 0, 0, 0); // turn off 
        strip.show();
        }
        delay(500);
    }
}

//define listen code to run Blink event
void addedToPlaylist( const char *event, const char *data) {
   doBlink = true;
}

void loop()
{
    setLED(255, 50, 140);
   
	//Blink LED's green 6 times
    if( doBlink == true ) {
        //Run Success pulse
        successPulse(r, g, b);
        //LEDs return to previous values 
        // setLED(255, 50, 140);
        // delay(200);
        successPulse(r, g, b);
        // setLED(255, 50, 140);
        // delay(200);
        doBlink = false;
    }


}
Click to Expand

Content Rating

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

0