Back to Parent

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

//************************************************//
//Button code
//************************************************//

//Declare buttons
int blueButton = D3; //Flavor 2
int redButton = D4; //Flavor 3
int yellowButton = D2; //Flavor 1

int blueButtonState = 0;
int redButtonState = 0;
int yellowButtonState = 0;

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

//************************************************//
//Neopixel code
//************************************************//

#define PIXEL_PIN D5
#define PIXEL_COUNT 7
#define PIXEL_TYPE WS2812
#include <neopixel.h>
Adafruit_NeoPixel strip = Adafruit_NeoPixel(PIXEL_COUNT, PIXEL_PIN, PIXEL_TYPE); //define neopixel

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


void setup() {
    
    Particle.subscribe("langley/2019/iot/eventFinished/", finishedEvent);
    //Set up particle variables for console
    Particle.variable("blueButtonState", blueButtonState);
    Particle.variable("redButtonState", redButtonState);
    Particle.variable("yellowButtonState", yellowButtonState);
    //Particle.variable("completed", completed);

    //Declare button pin modes
    pinMode(blueButton, INPUT_PULLUP);    
    pinMode(redButton, INPUT_PULLUP);    
    pinMode(yellowButton, INPUT_PULLUP);
    
    strip.begin();
    strip.show(); 

    //For steup, test neopixel
    iceCreamFlavor1();
    turnColorOff();
}

void loop() {
    
    //get button states
    blueButtonState = digitalRead( blueButton );
    redButtonState = digitalRead( redButton );
    yellowButtonState = digitalRead( yellowButton );

    //Determine which button is pressed
    //Publish event accordingly and color for Neopixel
    if ( blueButtonState == LOW )
    {
        delay(1000);
        iceCreamFlavor2();
        blueButtonPublish();
        delay(2000);
    }
    else if (redButtonState == LOW) {
        iceCreamFlavor3();
        redButtonPublish();
        delay(2000);
    }
    else if (yellowButtonState == LOW) {
        iceCreamFlavor1();
        yellowButtonPublish();
        delay(2000);
    }
    
}

void blueButtonPublish()
{
  // Remember that a device can publish at rate of about 1 event/sec,
  // with bursts of up to 4 allowed in 1 second.
  // Back to back burst of 4 messages will take 4 seconds to recover.
  // So we want to limit the amount of publish events that happen.

  // check that it's been 10 second  since our last publish
  if( lastPublishedAt + publishAfter < millis() )
  {

      String eventName = "langley/2019/iot/blueButtonPressed/" + System.deviceID();

      // then we share it out
      Particle.publish( eventName, "Blue" );

      // And this will get shared out to all devices using this code

      // we just pubished so capture this.
      lastPublishedAt = millis();
  }

}

void redButtonPublish()
{
    // Remember that a device can publish at rate of about 1 event/sec,
  // with bursts of up to 4 allowed in 1 second.
  // Back to back burst of 4 messages will take 4 seconds to recover.
  // So we want to limit the amount of publish events that happen.

  // check that it's been 10 secondds since our last publish
  if( lastPublishedAt + publishAfter < millis() )
  {

      String eventName = "langley/2019/iot/redButtonPressed/" + System.deviceID();

      // now we have something like "diot/2019/paired/0123456789abcdef"
      // and that corresponds to this devices info

      // then we share it out
      Particle.publish( eventName, "Red" );

      // And this will get shared out to all devices using this code

      // we just pubished so capture this.
      lastPublishedAt = millis();
  }

}

void yellowButtonPublish()
{
  // Remember that a device can publish at rate of about 1 event/sec,
  // with bursts of up to 4 allowed in 1 second.
  // Back to back burst of 4 messages will take 4 seconds to recover.
  // So we want to limit the amount of publish events that happen.

  // check that it's been 10 secondds since our last publish
  if( lastPublishedAt + publishAfter < millis() )
  {
   
      String eventName = "langley/2019/iot/yellowButtonPressed/" + System.deviceID();

      // now we have something like "diot/2019/paired/0123456789abcdef"
      // and that corresponds to this devices info

      // then we share it out
      Particle.publish( eventName, "Yellow" );

      // And this will get shared out to all devices using this code

      // we just pubished so capture this.
      lastPublishedAt = millis();
  }

}

//Yellow Button
void iceCreamFlavor1() {
    
    //setting colour based on the handler event (blue/green/yellow)
    uint32_t colorYellow = strip.Color(2550, 255, 50);
    
    for(int i=0; i< strip.numPixels(); i++) { //light up one bye one 
        strip.setPixelColor(i, colorYellow );
        delay( 1000 );
        strip.show();
    }
    
}

//Blue Button
void iceCreamFlavor2() {
    
    //setting colour based on the handler event (blue/green/yellow)
    uint32_t colorBlue = strip.Color(0, 0, 255);
    
    for(int i=0; i< strip.numPixels(); i++) { //light up one bye one 
        strip.setPixelColor(i, colorBlue );
        delay( 1000 );
        strip.show();
    }
    
}

//Red Button
void iceCreamFlavor3() {
    
    //setting colour based on the handler event (blue/green/yellow)
    uint32_t colorRed = strip.Color(255, 0, 0);
    
    for(int i=0; i< strip.numPixels(); i++) { //light up one bye one 
        strip.setPixelColor(i, colorRed );
        delay( 1000 );
        strip.show();
    }
    
}

void turnColorOff() {
    
    for(int i=0; i< strip.numPixels(); i++) { //light up one bye one 
        strip.setPixelColor(i, colorOff);
        delay( 1000 );
        strip.show();
    }
    
}

void finishedEvent(const char *event, const char *data)
{

    String eventName = String( event ); 
    
    String deviceID = System.deviceID();
    
    if( eventName.indexOf( deviceID ) != -1 ){
      return;
    }
    
    turnColorOff();

}
Click to Expand

Content Rating

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

0