Back to Parent

/*
 * Project: EmoClock
 * Revision: B
 * Description: IoT Spring 2018 Final Project
 * Author: Corey Williams
 * Team: Home-wreckers
 * Date: 2/28/2018
 */

//Add libraries.
#include <neopixel.h>
#include "application.h"
#include "neopixel/neopixel.h"
#include "DFRobotDFPlayerMini.h"

//Define variables.
#define PIXEL_COUNT 7
#define PIXEL_PIN D6
#define PIXEL_PIN_B D5
#define PIXEL_TYPE WS2811

//Assign global variables.
long lastPublishedAt = 0; // This value will store the last time we published an event.
int publishAfter = 10000; // This is the time delay before we should publish a new event.

int mobileState = 0;
int mindCount = 0;
bool pressed = FALSE;

Adafruit_NeoPixel pixelsA = Adafruit_NeoPixel(PIXEL_COUNT, PIXEL_PIN, PIXEL_TYPE); //Initiate first neopixel strand.
Adafruit_NeoPixel pixelsB = Adafruit_NeoPixel(PIXEL_COUNT, PIXEL_PIN_B, PIXEL_TYPE); //Initiate first neopixel strand.

DFRobotDFPlayerMini myDFPlayer; //Initiate .mp3 player.

 //Assign pins.
int buttonPin = D4;

//For testing purposes.
int testPinA = D3;
int testPinB = D2;
int testPinC = D1;

void setup()
{
  Serial.begin(9600);
  Serial1.begin(9600);
  //Assign inputs.
  pinMode(buttonPin, INPUT_PULLUP);

  //Assign outputs.
  //Initiates the NeoPixel connection.
  pixelsA.begin();
  pixelsB.begin();
  for(int i=0; i < PIXEL_COUNT; i++)
  {
    pixelsA.setPixelColor(i, 0,0,0);
    pixelsA.show();
    pixelsB.setPixelColor(i, 0,0,0);
    pixelsB.show();
  }
  //Initiates the DFPlayer.
  if (!myDFPlayer.begin(Serial1))
  {  //Use Serial1 to communicate with player.
    Serial.println(F("Unable to begin:"));
    Serial.println(F("1.Please recheck the connection!"));
    Serial.println(F("2.Please insert the SD card!"));
    while(true){
      delay(0);
    }
  }
  Serial.println("DFPlayer Initialized");
  myDFPlayer.volume(30);

  //Subscribe to particle events.
  Particle.subscribe("DIOT-2018-DARA-TEAM-HOME-WRECKERS-MOBILE-MOBILEA", handleMobileA); //Subscribe to event that is published by mood mobile A.
  Particle.subscribe("DIOT-2018-DARA-TEAM-HOME-WRECKERS-MOBILE-MOBILEB", handleMobileB); //Subscribe to event that is published by mood mobile B.
  Particle.subscribe("DIOT-2018-DARA-TEAM-HOME-WRECKERS-MOBILE-MOBILEC", handleMobileC); //Subscribe to event that is published by mood mobile C.
  Particle.subscribe("DIOT-2018-DARA-TEAM-HOME-WRECKERS-PLAY", handlePlay); //Subscribe to event that is published by mood mobile for music playing.
  Particle.subscribe("DIOT-2018-DARA-TEAM-HOME-WRECKERS-MIND", handleMind); //Subscribe to event that is published by mind jar.

  //For testing.
  pinMode(testPinA, INPUT_PULLUP);
  pinMode(testPinB, INPUT_PULLUP);
  pinMode(testPinC, INPUT_PULLUP);

  Particle.function("Sun", publishA);
  Particle.function("Bolt", publishB);
  Particle.function("Cloud", publishC);
  Particle.function("Jar", publishJar);
}

void loop()
{
  int buttonState = digitalRead (buttonPin); //See if button is pushed or not.
  int testStateA = digitalRead (testPinA); //For testing purposes.
  int testStateB = digitalRead (testPinB); //For testing purposes.
  int testStateC = digitalRead (testPinC); //For testing purposes.

  if (buttonState == LOW && pressed == FALSE) //If button is pressed...
  {
    pressed = TRUE;
    publishMyEvent(); //Publish reset event.
  }

  if (buttonState == HIGH) //If button is not pressed...
  {
    pressed = FALSE;
  }

  //For testing purposes.
  if (testStateA == LOW)
  {
    publishTestA();
  }

  if (testStateB == LOW)
  {
    publishTestB();
  }

  if (testStateC == LOW)
  {
    publishTestC();
  }
}

void publishMyEvent() //When button is pushed, publish an event to reset the inputs.
{
  if(lastPublishedAt + publishAfter < millis())
  {
    Particle.publish("DIOT-2018-DARA-TEAM-HOME-WRECKERS-RESET");
    lastPublishedAt = millis();
    Serial.println(mindCount);
    //Determine music output based on jar inputs.
    if (mindCount <= 2)
    {
      myDFPlayer.play(1);
    }
    if (mindCount > 2)
    {
      myDFPlayer.play(2);
    }
    //Determine light output based on mobile inputs.
    if (mobileState == 3) //If cloud (sad) mobile was hit last.
    {
      for(int i=0; i < PIXEL_COUNT; i++) //Light up neopixel based on Mood Mobile inputs.
      {
        pixelsA.setPixelColor(i, 30,29,236); //Blue.
        pixelsA.show();
        pixelsB.setPixelColor(i, 30,29,236); //Blue.
        pixelsB.show();
      }
      for(int z=0; z < 5; z++) //Add breathing effect.
      {
        for (int j=1; j<200; j++)
        {
          pixelsA.setBrightness(j);
          pixelsA.show();
          pixelsB.setBrightness(j);
          pixelsB.show();
          delay(15);
        }
        for (int j=200; j>1; j--)
        {
          pixelsA.setBrightness(j);
          pixelsA.show();
          pixelsB.setBrightness(j);
          pixelsB.show();
          delay(15);
        }
      }

      pixelsA.setBrightness(200);
      pixelsA.show();
      pixelsB.setBrightness(200);
      pixelsB.show();
    }

    if (mobileState == 2) //If bolt mobile (angry) was hit last.
    {
      for (int b=0; b < 10; b++) //Blinking effect.
      {
        for(int i=0; i < PIXEL_COUNT; i++) //Light up neopixel based on Mood Mobile inputs.
        {
          pixelsA.setPixelColor(i, 35,236,29); //Red on
          pixelsA.show();
          pixelsB.setPixelColor(i, 35,236,29); //Red on
          pixelsB.show();
        }
        delay(500);
        for(int i=0; i < PIXEL_COUNT; i++) //Light up neopixel based on Mood Mobile inputs.
        {
          pixelsA.setPixelColor(i, 0,0,0); //Off
          pixelsA.show();
          pixelsB.setPixelColor(i, 0,0,0); //Off
          pixelsB.show();
        }

        delay(500);
      }

      for(int i=0; i < PIXEL_COUNT; i++) //Light up neopixel based on Mood Mobile inputs.
      {
        pixelsA.setPixelColor(i, 35,236,29); //Red
        pixelsA.show();
        pixelsB.setPixelColor(i, 35,236,29); //Red
        pixelsB.show();
      }
    }

    if (mobileState == 1) //If mood moblie (happy) was hit last.
    {
      for(int i=0; i < PIXEL_COUNT; i++) //Light up neopixel based on Mood Mobile inputs
      {
        pixelsA.setPixelColor(i, 245,237,48); //Yellow
        pixelsA.show();
        pixelsB.setPixelColor(i, 245,237,48); //Yellow
        pixelsB.show();
      }

      delay(10000);
    }

    delay(10000);

    for(int i=0; i < PIXEL_COUNT; i++) //Turn off neopixels.
    {
      pixelsA.setPixelColor(i, 0,0,0);
      pixelsA.show();
      pixelsB.setPixelColor(i, 0,0,0);
      pixelsB.show();
    }

    mindCount = 0;//Reset jar count.
    myDFPlayer.pause();//Turn off music.
  }
}

//Create functions update input data from Mood Mobiles and Mind Jar.
void handleMobileA(const char *event, const char *data)
{
    mobileState = 1;
}

void handleMobileB(const char *event, const char *data)
{
    mobileState = 2;
}

void handleMobileC(const char *event, const char *data)
{
    mobileState = 3;
}

void handlePlay(const char *event, const char *data)
{
  if (String(data) == "MA")
  {
    myDFPlayer.play(5);
    delay(10000);
    myDFPlayer.pause();
  }

  if (String(data) == "MB")
  {
    myDFPlayer.play(4);
    delay(10000);
    myDFPlayer.pause();
  }

  if (String(data) == "MC")
  {
    myDFPlayer.play(6);
    delay(10000);
    myDFPlayer.pause();
  }
}

void handleMind(const char *event, const char *data)
{
    mindCount++;
    myDFPlayer.play(6);
    delay(10000);
    myDFPlayer.pause();
}

void publishTestA()
{
  if(lastPublishedAt + publishAfter < millis())
  {
      Particle.publish("DIOT-2018-DARA-TEAM-HOME-WRECKERS-MOBILE-MOBILEA", "6");
      Particle.publish("DIOT-2018-DARA-TEAM-HOME-WRECKERS-MIND", "1");
      lastPublishedAt = millis();
  }
}

void publishTestB()
{
  if(lastPublishedAt + publishAfter < millis())
  {
      Particle.publish("DIOT-2018-DARA-TEAM-HOME-WRECKERS-MOBILE-MOBILEB", "6");
      Particle.publish("DIOT-2018-DARA-TEAM-HOME-WRECKERS-MIND", "6");
      lastPublishedAt = millis();
  }
}

void publishTestC()
{
  if(lastPublishedAt + publishAfter < millis())
  {
      Particle.publish("DIOT-2018-DARA-TEAM-HOME-WRECKERS-MOBILE-MOBILEC", "6");
      lastPublishedAt = millis();
  }
}

int publishA(String command)
{
  Particle.publish("DIOT-2018-DARA-TEAM-HOME-WRECKERS-MOBILE-MOBILEA");
  Particle.publish("DIOT-2018-DARA-TEAM-HOME-WRECKERS-PLAY", "MA");
  lastPublishedAt = millis();
  return 1;
}

int publishB(String command)
{
  Particle.publish("DIOT-2018-DARA-TEAM-HOME-WRECKERS-MOBILE-MOBILEB");
  Particle.publish("DIOT-2018-DARA-TEAM-HOME-WRECKERS-PLAY", "MB");
  lastPublishedAt = millis();
  return 1;
}

int publishC(String command)
{
  Particle.publish("DIOT-2018-DARA-TEAM-HOME-WRECKERS-MOBILE-MOBILEC");
  Particle.publish("DIOT-2018-DARA-TEAM-HOME-WRECKERS-PLAY", "MC");
  lastPublishedAt = millis();
  return 1;
}

int publishJar(String command)
{
  mindCount = atoi(command);
  myDFPlayer.play(3);
  delay(8000);
  myDFPlayer.pause();
  return mindCount;
}
Click to Expand

Content Rating

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

0