Back to Parent

Code for Connected Coasters
//Records the photon's unique device ID
String myID = System.deviceID();
//Controls green and blue colors in first cluster of three LEDs
int greenPins1 = D0;
int bluePins1 = D1;
//Controls green and blue colors in second cluster of three LEDs
int greenPins2 = D2;
int bluePins2 = D3;
//Controls
int buttonPin = D4;
//Stores binary variable indicating status (dring / no drink) of sibling coaster
int friendStatus = 0;
//Stores binary variable indicating status (drink / no drink) of local coaster
int myStatus = 0;
void setup ()
{
  //Set the button as input
  pinMode (buttonPin, INPUT_PULLUP);
  //Sets the LED clusters as output
  pinMode (greenPins1, OUTPUT);
  pinMode (bluePins1, OUTPUT);
  pinMode (greenPins2, OUTPUT);
  pinMode (bluePins2, OUTPUT);
  //These events ultimately control the friendStatus variable (on both ends)
  Particle.subscribe("public-event/gstodtmeister11", eventHandler);
  Particle.subscribe("public-event/gstodtmeister22", eventHandler2);
  //Ensures the LEDs are initially deactivated
  nobodyDrinking();
}
void loop ()
{
  monitorButton();
}
void monitorButton()
{
  //Reads the state of the button on the coaster
  int buttonState = digitalRead(buttonPin);
  //If the button is depressed...
  if (buttonState == LOW)
  {
    //Publish event indicating the button is depressed
    Particle.publish("public-event/gstodtmeister11", myID);
    //Set status of local coaster as activated
    myStatus = 1;
    //If friend is also drinking...
    if (friendStatus == 1)
    {
      drinkingTogether();
    }
    else
    {
      drinkingAlone();
    }
  }
  //If the button is not depressed...
  else
  {
    //...and you just lifted a glass from the coaster
    if (myStatus == 1)
    {
    //Set status of coaster
    myStatus = 0;
    delay(1000);
    //Publish event indicating the button is not depressed
    Particle.publish("public-event/gstodtmeister22", myID);
    }
    //If friend is also drinking...
    if (friendStatus == 1)
    {
      friendDrinking();
    }
    else
    {
      nobodyDrinking();
    }
  }
}
/*If someone sets a glass on the sibling coaster, the code publishes an event
  that calls this method. If the corresponding device published the event (not
  your device), this method sets the friendStatus variable to 1. */
void eventHandler(const char *event, const char *data)
{
  if (myID != data)
  {
    friendStatus = 1;
  }
}
/*If someone lifts a glass off the sibling coaster, the code publishes an event
  that calls this method. If the corresponding device published the event (not
  your device), this method sets the friendStatus variable to 0.*/
void eventHandler2(const char *event, const char *data)
{
  if (myID != data)
  {
    friendStatus = 0;
  }
}
//Activates only the green LEDs in both clusters
//Indicates you're drinking alone
void drinkingAlone()
{
  digitalWrite(greenPins1, 0);
  digitalWrite(bluePins1, 255);
  digitalWrite(greenPins2, 0);
  digitalWrite(bluePins2, 255);
}
//Activates one LED cluster green and the other LED cluster blue
//Indicates both individuals have a cup on their coaster
void drinkingTogether()
{
  digitalWrite(greenPins1, 0);
  digitalWrite(bluePins1, 255);
  digitalWrite(greenPins2, 255);
  digitalWrite(bluePins2, 0);
}
//Activates only the blue LEDs in both clusters
//Indicates your friend is drinking alone
void friendDrinking ()
{
  digitalWrite(greenPins1, 255);
  digitalWrite(bluePins1, 0);
  digitalWrite(greenPins2, 255);
  digitalWrite(bluePins2, 0);
}
//Deactivates all LEDs in both clusters
//Indicates nobody is drinking
void nobodyDrinking ()
{
  digitalWrite(greenPins1, 255);
  digitalWrite(bluePins1, 255);
  digitalWrite(greenPins2, 255);
  digitalWrite(bluePins2, 255);
}
Click to Expand

Content Rating

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

0