Back to Parent

// Campus Food Alert Map - Tejas Kashyap, Harshika Jain, Shawn Koid


// Library statements
#include <neopixel.h>
#include <neopixel.h>

// Time, pixel value and misc. initializations
int now = 0;
unsigned long tStartedAt =0;
int hour =0;
int currentTime =0;
int weekday =0;
int local =0;
int minute =0;
bool tRunning = FALSE;
int duration = 15*1000;
int x=12;
int y=224;
int z=187;
int loc=0;

uint32_t f;
uint32_t k;
int buttonPin = D3;
long lastPublishedAt = 0; // This value will store the last time we published an event
int publishAfter = 1000; // this is the time delay before we should publish a new event from this device

//Potentiometer initialization for floor determination
int potPin = A4;
int potReading = 0;
int flr = D2;
int flrnumber = 0;
  


// NEOPIXEL DEFINITIONS
#define PIXEL_COUNT 3
#define PIXEL_PIN D2
#define PIXEL_TYPE WS2812
Adafruit_NeoPixel strip(PIXEL_COUNT, PIXEL_PIN, PIXEL_TYPE);



void setup() 

   {
     Particle.subscribe(  "diot/2019/paired/sthfoodout/" , handleSharedEvent );
     pinMode( buttonPin , INPUT_PULLUP); // sets pin as input
     strip.begin();
     strip.show(); // Initialize all pixels to 'off'
  //  Particle.variable("time", &currentTime, INT);
     Particle.variable("HOUR", &hour, INT);
     Particle.variable("MINUTE", &minute, INT);
     Particle.variable("WEEKDAY", &weekday, INT);
     Time.zone(-5);
     pinMode(flr, OUTPUT);
   }
 


void loop() { 
    
        uint32_t c = strip.Color(x,y,z);    // Neopixel color function initialized

//Initializing and calling inbuilt time functions to check for pantry hours in the next step

        hour = Time.hour(Time.now());
        minute = Time.minute();
        weekday = Time.weekday();



//PANTRY OPERATION - Hours specified, and pantry light function called 

//THURSDAY 4-6PM
if ( weekday==5 && hour>=16 && hour<18)
        { 
            pantry(f);
           }
           
//PANTRY HOURS _ SATURDAY 2-4PM           
else if ( weekday==7 && hour>=14 && hour<16 )
          {
              pantry(f);
          }
//FOR CLASS DEMO - TUESDAY 2-5PM          
else if ( weekday==3 && hour>=14 && hour<17 )
          {
              pantry(f);
          }


    int buttonState = digitalRead( buttonPin );
    

// Setting neopixel colors, diming action through if loops 

    if( buttonState == LOW ){  // turn the LED ON
    tStartedAt = millis();
    tRunning = TRUE;
    publishMyEvent();
    rainbow(c);
    delay(100); // delay for a bit
    }
    
    int now = millis();
    int j=now-tStartedAt;
    
    if (loc==0) {
        
        if(tRunning && j%1000<100) {
            x=x-12/15;
            y=y-224/15;
            z=z-187/15;
            
            k = strip.Color(x, y, z);
            rainbow(k);
        }
    
        if (j>duration) {
            k = strip.Color(0,0,0);
            rainbow(k);
            tRunning=FALSE;
            x=12;
            y=224;
            z=187;
        }
    } else {
        if(tRunning && j%1000<100) {
            x=x-12/15;
            y=y-224/15;
            z=z-187/15;
            
            k = strip.Color(x, y, z);
            rainbow2(k);
        }
    
        if (j>duration) {
            k = strip.Color(0,0,0);
            rainbow2(k);
            tRunning=FALSE;
            
            x=12;
            y=224;
            z=187;
        }
    }
        
        
// Mapping potentiometer readings to five possible floors on buildings through if-else statements 

      potReading = analogRead(potPin);
      Particle.variable("POT", &potReading, INT);
    
    if (potReading<=819 && potReading >=0)
       {
          flr=1;
         }
    else if(potReading>819 && potReading <=1638)
       { 
          flr=2;
       }
    else if(potReading>1638 && potReading <=2457)
       { 
          flr=3;
       }
    else if(potReading>2457 && potReading <=3276)
       { 
          flr=4;
       }
    else if(potReading>3276 && potReading <=4095)
       { 
          flr=5;
       }

    Particle.variable("FLOOR", &flr, INT);
    
    
}



//Publishing events specific to floor, value of which obtained from potentiometer 

void publishMyEvent(){
     if( lastPublishedAt + publishAfter < millis() ) {
         String eventName = "diot/2019/paired/sthfoodout/" + System.deviceID();
         if (flr==1)
         {
         Particle.publish( eventName, "Food is available at Tepper School of Business, first floor" );
         }
         else if (flr==2)
         {
         Particle.publish( eventName, "Food is available at Tepper School of Business, second floor" );
         }
         else if (flr==3)
         {
         Particle.publish( eventName, "Food is available at Tepper School of Business, third floor" );
         }
         else if (flr==4)
         {
         Particle.publish( eventName, "Food is available at Tepper School of Business, fourth floor" );
         }
         else if (flr==5)
         {
         Particle.publish( eventName, "Food is available at Tepper School of Business, fifth floor" );
         }
         lastPublishedAt = millis();
         }
    }


// Checking if event is raised by own device or other, and performing functions accordingly

void handleSharedEvent(const char *event, const char *data) 
  {
         String eventName = String( event ); // convert to a string object
         String deviceID = System.deviceID();
         uint32_t c = strip.Color(x,y,z);
    
         if( eventName.indexOf( deviceID ) != -1 )   // matching event and device ID, light up when event published from other device
          {    
            rainbow(c);
            loc=0;
            return;
          } 
           else 
                {
                  tStartedAt = millis();
                  tRunning = TRUE;
                  rainbow2(c);
                  loc=1;
    }
    

}


//Function to light up own device neopixel
void rainbow(uint32_t c)
  {
     {   strip.setPixelColor(1, c); // i= which led to light up, c= set a color 
         strip.show();
       }
         delay( 100 );
   }


//Function to light up other device's neopixel
void rainbow2(uint32_t c)
  {
     {   strip.setPixelColor(0, c); // i= which led to light up, c= set a color 
         strip.show();
       }
         delay( 100 );
   }

//Function to light up the pantry neopixel    
void pantry(uint32_t f)
   {
         f = strip.Color(180, 200, 0);
         strip.setPixelColor(2,f);
         strip.show();
    }
    
// This code can be reproduced for other locations by changing the name of the building in published event, 
//and changing the corresponding pixel position value in rainbow functions
Click to Expand

Content Rating

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

0