Back to Parent

#include <neopixel.h>


#include <math.h>





#define PIXEL_BLUE D2
#define PIXEL_GREEN D3
#define PIXEL_COUNT 16
#define PIXEL_TYPE WS2812

Adafruit_NeoPixel stripBlue = Adafruit_NeoPixel(PIXEL_COUNT, PIXEL_BLUE, PIXEL_TYPE);
Adafruit_NeoPixel stripGreen = Adafruit_NeoPixel(PIXEL_COUNT, PIXEL_GREEN, PIXEL_TYPE);
int r = 0;
int b = 0;
int g = 0;
long lastPublishedAt = 0;
int publishAfter = 500; //the initial delay

int baseInt = 40;
int maxInt = 1000;


int currBlueInt = 40;
int currGreenInt = 40;
bool dimBlue = false;
bool dimGreen = false;
bool greenBr = true;
bool blueBr = true;

int photo = A4;

bool blueOn = false; //color for Lily = blue
bool greenOn = false;

// -1 = distracted
// 1 = working
int currentState= 0;

void setup()
{
  
    stripBlue.begin();
    stripGreen.begin();


  Particle.subscribe( "working" , eventOn );
  Particle.subscribe( "distracted" , eventOff );
  Serial.begin( 9600 );

}

void loop()
{

    publishMyEvent();
    
    if(blueBr){
               
        if(currBlueInt >= maxInt){
            dimBlue = true;
        }
        else if(currBlueInt <= baseInt) {
            dimBlue = false;
        }
        
        if(dimBlue){
            currBlueInt-=10;
        }
        
        else{
            currBlueInt+=10;
        }
        
        stripBlue.setBrightness(currBlueInt);
        for(uint32_t i=0; i< stripBlue.numPixels(); i++) {
            stripBlue.setPixelColor(i, 50,10,40);
        }
        stripBlue.show();
    }
    
    if(greenBr){
               
        if(currGreenInt >= maxInt){
            dimGreen = true;
        }
        else if(currGreenInt <= baseInt) {
            dimGreen = false;
        }
        
        if(dimGreen){
            currGreenInt-=10;
        }
        
        else{
            currGreenInt+=10;
        }
        
        stripGreen.setBrightness(currGreenInt);
        for(uint32_t i=0; i< stripGreen.numPixels(); i++) {
            stripGreen.setPixelColor(i, 45, 25, 0);
        }
        stripGreen.show();
    }
    
    if (blueOn == true) {
        blueBr = false;
        currBlueInt = 40;
        stripBlue.setBrightness(currBlueInt);
        for(uint32_t i=0; i< stripBlue.numPixels(); i++) {
                stripBlue.setPixelColor(i, 50,10,40);
        }
        stripBlue.show();
    }
    
    else {
        blueBr = true;
    }
    
    if (greenOn == true){
        greenBr = false;
        currGreenInt = 40;
        stripGreen.setBrightness(currGreenInt);
        for(uint32_t i=0; i< stripGreen.numPixels(); i++) {
                stripGreen.setPixelColor(i, 45,25,0);
        }
        stripGreen.show();
    }
    
    else {
       greenBr = true;
    }
    

    // stripBlue.show();
    // stripGreen.show(); 
    delay (100);
    
}


void publishMyEvent()
{

  // check that it's been 10 secondds since our last publish
  if( lastPublishedAt + publishAfter < millis() )
  {
    
      if (System.deviceID() == "e00fce68d1958060a1d74bac"){
          
          if (analogRead(photo) > 3000) {
            if( currentState != -1){
                Particle.publish( "distracted", "blue");
            }
            currentState = -1; 
          }
          else{
            if( currentState != 1){
              Particle.publish( "working", "blue");
              
            }
            currentState = 1; 
          }
      }
      if (System.deviceID() == "e00fce687a57041bdac6f101"){
          if (analogRead(photo) > 3000) {
            if( currentState != -1){
                Particle.publish( "distracted", "green");
            }
            currentState = -1; 

          }
          else{
            if( currentState != 1){
              Particle.publish( "working", "green");
            }
            currentState = 1; 
          }
      }
      
      
      lastPublishedAt = millis();
  }

} 



void eventOn(const char *event, const char *data)
{
    String dataString = String(data);
    
    if (dataString.equals("blue")) {
        blueOn = true;
    }
    
    else if (dataString.equals("green")) {
        greenOn = true;
    }
    
    else {
        return;
    }
    
    
}


void eventOff(const char *event, const char *data)
{
    
    String dataString = String(data);

    if (dataString.equals("blue")) {
        blueOn = false;
    }
    
    
    else if (dataString.equals("green")) {
        greenOn = false;
    }
    else{
        return;
    }
    
}
Click to Expand

Content Rating

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

0