Back to Parent

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

#define RING_PIN D9
#define RING_COUNT 7
#define RING_TYPE WS2812

Adafruit_NeoPixel ring = Adafruit_NeoPixel(RING_COUNT, RING_PIN, RING_TYPE);

#define STRIP_PIN D3
#define STRIP_COUNT 4
#define STRIP_TYPE WS2812

Adafruit_NeoPixel strip = Adafruit_NeoPixel(STRIP_COUNT, STRIP_PIN, STRIP_TYPE);

int mor_reminder = 0;
int nit_reminder = 0;
int toggle = 0;
int togglerfid=0;
unsigned long startAt = -1;
long elapsed = 0;

int solid_duration = 1000*3;
int fadeOut_duration = 1000*20;

unsigned long running = 0;
unsigned long ring_timer = 0;


uint32_t colorOn = strip.Color(255, 255, 255);
uint32_t colorOff = strip.Color(0 , 0 , 0);




void setup() {

    ring.begin();
    ring.show();
    
    for(int i=0; i < STRIP_COUNT; i++) {
        strip.setPixelColor(i, colorOff );
        strip.show();
    }
    
    strip.begin();
    strip.show();
    Particle.subscribe( "jiangtaoyan/2019/enchantedBar/" , handleSharedEvent );
    Particle.subscribe( "maverick/2019/rfidok/" , handleRfid );
    Particle.function("Morningrem", morrem);
    Particle.function("Nightrem", nitrem);
}

void loop() {

    // strip
    if ( mor_reminder == 1 )
    {
        elapsed = millis() - startAt; //calculated elapsed time
        if ( elapsed <= solid_duration )
        {
            strip_solid(1);    
        }
        else if ( elapsed > solid_duration && elapsed <= fadeOut_duration )
        {
            strip_fadeOut(1);
        }
        else
        {
            mor_reminder = 0;
        }
    }
    if ( nit_reminder == 1 )
    {
        elapsed = millis() - startAt; //calculated elapsed time
        if ( elapsed <= solid_duration )
        {
            strip_solid(2);    
        }
        else if ( elapsed > solid_duration && elapsed <= fadeOut_duration )
        {
            strip_fadeOut(2);
        }
        else
        {
            nit_reminder = 0;
        }
    }
    
    
    // ring
    if ( toggle == 1)
    {
        ring_on(1);
    }
    else
    {
        ring_off();
    }
    
    if(togglerfid==1)
    {
        ring_on(2);
    }
    else
    {
        ring_off();
    }
    delay(100);
}

// strip on and fade out in 20s
void strip_solid(int a)
{
    if(a==1)
    {
    for(int i=0; i < STRIP_COUNT; i++) {
        strip.setPixelColor(i, 0 , 255, 255 );
        strip.show();
    }
    }
    else if(a==2)
    {
    for(int i=0; i < STRIP_COUNT; i++) {
        strip.setPixelColor(i, 255 , 255, 0 );
        strip.show();
    }
    }
}


void strip_fadeOut (int a)
{
    int r=0;
    int g=0;
    int b=0;
    int colorValue = map(elapsed, solid_duration, fadeOut_duration, 0, 255);
    if(a==1)
    {
     g = 255-colorValue;
    r = 0;
    b = 255-colorValue;
    for (int i=0; i< STRIP_COUNT; i++)
    {
        strip.setPixelColor(i,r,g,b);
    }
    strip.show();   
    }
    else if(a==2)
    {
     g = 255-colorValue;
    b = 0;
    r = 255-colorValue;
    for (int i=0; i< STRIP_COUNT; i++)
    {
        strip.setPixelColor(i,r,g,b);
    }
    strip.show();   
    }
}



void handleSharedEvent(const char *event, const char *data)
{
    // String eventName = String( event ); 
    // String deviceID = System.deviceID();
    
    // if( eventName.indexOf( deviceID ) != -1 ){
    //         return;
    // }
    toggle = 1;
    ring_timer = millis();
    Particle.publish("Discount");
}
void handleRfid(const char *event, const char *data)
{
    togglerfid = 1;
    ring_timer = millis();
}

// ring on for 5min then fade out
void ring_on(int a)
{
    int r=0;
    int g=0;
    int b=0;
    running  = millis() - ring_timer;
    if(a==1)
    {
    if (running <= solid_duration)
    {
        for(int i=0; i < RING_COUNT; i++) {
        ring.setPixelColor(i, 0 , 0, 255 );
    }
    ring.show();
    delay (100);    
    }
    
    else if ( running > solid_duration && running <= fadeOut_duration )
    {
        int colorValue = map(running, 0, 1000*20, 0, 255);
        g = 0;
        r = 0;
        b = colorValue;
        for (int i=0; i< RING_COUNT; i++)
        {
            ring.setPixelColor(i,r,g,255-b);
        }
        strip.show();    
    }
    else {
        toggle = 0;
    }
    }
    
    if(a==2)
     {
    if (running <= solid_duration)
    {
        for(int i=0; i < RING_COUNT; i++) {
        ring.setPixelColor(i, 255 , 164, 0 );
    }
    ring.show();
    delay (100);    
    }
    
    else if ( running > solid_duration && running <= fadeOut_duration )
    {
        int colorValue1 = map(running, 0, 1000*20, 0, 255);
        int colorValue2 = map(running, 0, 1000*20, 0, 164);
        g = 164-colorValue2;
        r = 255-colorValue1;
        b = 0;
        for (int i=0; i< RING_COUNT; i++)
        {
            ring.setPixelColor(i,r,g,b);
        }
        strip.show();    
    }
    else {
        togglerfid = 0;
    }
    }
}

void ring_off (){
    for(int i=0; i < RING_COUNT; i++) {
        ring.setPixelColor(i, colorOff );
    }
    ring.show();
    delay (100);
}

int morrem (String e){
    startAt = millis();
    mor_reminder = 1;
    sendnot(1);
    return 1;
}
int nitrem (String e){
    startAt = millis();
    nit_reminder = 1;
    sendnot(2);
    return 1;
}

void sendnot( int a ){
    // check if 1 minute has elapsed
    if( a == 1 ){
    Particle.publish( "Morning");
    long last_published = millis();
    delay(1000);
    }
    
    if( a == 2 ){
    Particle.publish( "Night");
    long last_published = millis();
    delay(1000);
    }
}
Click to Expand

Content Rating

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

0