Back to Parent

int happyLedPin = D0;
int sadLedPin = D1;

int happyButtonPin = D2;
int sadButtonPin = D3;
int sadVal;
int happyVal;

void setup()
{
  //setup buttons
  pinMode( happyButtonPin , INPUT_PULLUP);
  pinMode( sadButtonPin , INPUT_PULLUP);

  //setup pins
  pinMode( happyLedPin, OUTPUT);
  pinMode( sadLedPin, OUTPUT);

  //start LEDs off
  digitalWrite( happyLedPin, LOW );
  digitalWrite( sadLedPin, LOW );
  //subscribe to sad emotion events
  Particle.subscribe("db2017/sadEmotion-pushedOff", sadOffhandler);
  Particle.subscribe("db2017/sadEmotion-pushedOn", sadOnhandler);
  //subscribe to happy emotion events
  Particle.subscribe("db2017/happyEmotion-pushedOff", happyOffhandler);
  Particle.subscribe("db2017/happyEmotion-pushedOn", happyOnhandler);
}

void loop() {

  // read the value from the button pin
  sadVal = digitalRead(sadButtonPin);
  happyVal = digitalRead(happyButtonPin);


  if( sadVal == LOW )
  {
      announceSadEmotionOn();
      //digitalWrite(sadLedPin, HIGH);
      delay( 1000 );

  }
  else if ( sadVal == HIGH)
  {
    announceSadEmotionOff();
    //digitalWrite(sadLedPin, LOW);
    delay( 1000 );
  }

  if( happyVal == LOW )
  {
      announceHappyEmotionOn();
      //digitalWrite(happyLedPin, HIGH);
      delay( 1000 );
  }
  else if ( happyVal == HIGH)
  {
    announceHappyEmotionOff();
    //digitalWrite(happyLedPin, LOW);
    delay( 1000 );
  }



}

void announceSadEmotionOn()
{
  Particle.publish( "lm2017/sadEmotion-pushedOn" );
}

void announceSadEmotionOff()
{
  Particle.publish( "lm2017/sadEmotion-pushedOff");
}

void announceHappyEmotionOn()
{
  Particle.publish( "lm2017/happyEmotion-pushedOn");
}

void announceHappyEmotionOff()
{
  Particle.publish( "lm2017/happyEmotion-pushedOff");
}


// sad event handlers and functions

void sadOffhandler(const char *eventName, const char *data) {
  sadOff();
}

void sadOff() {
  digitalWrite(sadLedPin, LOW);
}

void sadOnhandler(const char *eventName, const char *data) {
  sadOn();
}

void sadOn() {
  digitalWrite(sadLedPin, HIGH);
}

// happy event handlers and functions
void happyOffhandler(const char *eventName, const char *data) {
  happyOff();
}

void happyOff() {
  digitalWrite(happyLedPin, LOW);
}

void happyOnhandler(const char *eventName, const char *data) {
  happyOn();
}

void happyOn(){
  digitalWrite(happyLedPin, HIGH);
}
Click to Expand

Content Rating

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

0