Back to Parent

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


#define PIXEL_PIN D2
#define PIXEL_COUNT 8
#define PIXEL_TYPE WS2812
Adafruit_NeoPixel stick = Adafruit_NeoPixel( PIXEL_COUNT, PIXEL_PIN, PIXEL_TYPE );
uint32_t color = stick.Color( 255, 255, 255 );//white
uint32_t colorOff = stick.Color( 0, 0, 0 );//LED off
uint32_t red = stick.Color( 255, 0,0);
uint32_t green = stick.Color( 0, 255, 0 );
uint32_t yellow = stick.Color( 255, 255, 0 );
float quart=856.0f;
float milk_size=856.0f;
double milk_level=0.0f;
int toggle=0;
void setup() {
 
    stick.begin();
    stick.show();
    Particle.subscribe("DIOT/CP2/Milkbox/Weight",getmilk);
    Particle.function("Milkcartonsize:",milksize);
    Particle.function("Get_Milk_Level",getmilklevel);
    for(int j=0;j<3;j++)//for 3 times, lights on and off one by one for each LED in sequence
    {
        for(int i=0;i<stick.numPixels();i++)
        {
            stick.setPixelColor(i,color);
            stick.show();
            delay(50);
            stick.setPixelColor(i,colorOff);
            stick.show();
        }
    }
}

void loop() {
 
  if(milk_level>0.75*milk_size)
  {
        // double factor=scale.get_units()/milk_level*stick.numPixels();
        // int pixel=ceil(factor);
        for(int i=0; i< stick.numPixels();i++)
        {
            stick.setPixelColor(i,green);
            stick.show();
        }
        toggle=1;
        // for(int i=pixel;i<stick.numPixels();i++)
        // {
        //     stick.setPixelColor(i,colorOff);
        //     stick.show();
        // }
  }
  else if(milk_level>=0.25*milk_size && milk_level<=0.75*milk_size)
  {
        // double factor=scale.get_units()/milk_level*stick.numPixels();
        // int pixel=ceil(factor);
        for(int i=0; i< stick.numPixels();i++)
        {
            stick.setPixelColor(i,yellow);
            stick.show();
        }
        toggle=1;
        // for(int i=pixel;i<stick.numPixels();i++)
        // {
        //     stick.setPixelColor(i,colorOff);
        //     stick.show();
        // }
  }
  else if(milk_level>30 && milk_level<0.25*milk_size)
  {
        // double factor=scale.get_units()/milk_level*stick.numPixels();
        // int pixel=ceil(factor);
        for(int i=0; i<stick.numPixels();i++)
        {
            stick.setPixelColor(i,red);
            stick.show();
        }
        if(toggle==1)
        {
            Particle.publish("milk_is_low");
            toggle=0;
        }
        // for(int i=pixel;i<stick.numPixels();i++)
        // {
        //     stick.setPixelColor(i,colorOff);
        //     stick.show();
        // }
  }
  else
  {
    for(int i=0;i<stick.numPixels();i++)
        {
            stick.setPixelColor(i,colorOff);
            stick.show();
        }
        toggle=1;
  }
}

int milksize(String command)
{
    if(command.toInt()==100)
    milk_size=quart*4;
    else if(command.toInt()==50)
    milk_size=quart*2;
    else if(command.toInt()==25)
    milk_size=quart;
    
    return 1;
}
void getmilk(const char *event, const char *data)
{
    milk_level=atof(data);
}
int getmilklevel(String command)
{
    int level=(milk_level/milk_size)*100;
    Particle.publish("Milk_level",String(level));
    return 1;
}
Click to Expand

Content Rating

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

0