Code for ambient devices

CODE | Accelerometer (Fork)

 
#include "neopixel.h"
#define PIXEL_PIN D0
#define PIXEL_COUNT 16
#define PIXEL_TYPE SK6812RGBW
Adafruit_NeoPixel ring = Adafruit_NeoPixel(PIXEL_COUNT, PIXEL_PIN, PIXEL_TYPE);
void setup() {
  Serial.begin(9600);
//  ring.setBrightness(55);
  ring.begin();
  ring.show();
  //rainforest
  Particle.subscribe("diot2017/musical_plates/activity", activity);
  Particle.subscribe("eatingend", finishedeating, "310039000f51353338363333");
  //space
  Particle.subscribe("space_eatingend", spacefinishedeating, "310039000f51353338363333");
}
void loop() {
}
void activity(const char *event,const char *data) {
  String activity = data;
  if (activity == "drink") {
    changeColor(ring.Color(0, 0, 0, random(200, 256)));
    delay(random(50,70));
    changeColor(ring.Color(0, 0, 0, random(100, 200)));
    delay(random(50,70));
    changeColor(ring.Color(0, 0, 0, random(200, 256)));
    delay(random(600,800));
    changeColor(ring.Color(0, 0, 0, 0));
    delay(random(200,300));
    changeColor(ring.Color(0, 0, 0, random(100, 200)));
    delay(random(50,70));
    changeColor(ring.Color(0, 0, 0, random(200, 256)));
    delay(random(50,70));
    changeColor(ring.Color(0, 0, 0, random(100, 200)));
    delay(random(50,70));
    changeColor(ring.Color(0, 0, 0, random(200, 256)));
    delay(random(50,70));
    changeColor(ring.Color(0, 0, 0, 256));
    delay(random(150,300));
    changeColor(ring.Color(0, 0, 0, 0));
    delay(random(250,350));
    changeColor(ring.Color(0, 0, 0, random(100, 200)));
    delay(random(20,50));
    changeColor(ring.Color(0, 0, 0, 230));
    delay(random(200,300));
    for(int i = 230; i >= 0; i -= 20) {
      changeColor(ring.Color(0,0,0,i));
      delay(1);
    }
  }
  else if (activity == "space_drink") {
    for(int j=0; j<256; j++) {
      for(uint16_t i=0; i<ring.numPixels(); i++) {
        ring.setPixelColor(i, ring.Color(0, 0, 0, 255) );
        ring.show();
        delay(85);
      }
      changeColor(ring.Color(0, 0, 0, 0));
      delay(300);
      changeColor(ring.Color(0, 0, 0, 255));
      delay(1000);
      changeColor(ring.Color(0, 0, 0, 0));
      off();
    }
  }
  else {
    off();
  }
}
void finishedeating(const char *event,const char *data) {
  hueChase(ring.Color(127, 100, 0), 50); // Lime Green
  hueChase(ring.Color(80, 0, 127), 50); // Cyan
  hueChase(ring.Color(127, 0, 0), 50); // Green
  hueChase(ring.Color(127, 100, 0), 50); // Lime Green
  hueChase(ring.Color(80, 0, 127), 50); // Cyan
  hueChase(ring.Color(127, 0, 0), 50); // Green
  off();
}
void spacefinishedeating(const char *event, const char *data) {
  greencolor();
  redcolor();
  bluecolor();
}
void greencolor(){
  for(int i = 0; i <= 240; i += 20) {
    changeColor(ring.Color(i,0,0,0));
    delay(1);
  }
  delay(500);
  for(int i = 240; i >= 0; i -= 20) {
    changeColor(ring.Color(i,0,0,0));
    delay(1);
  }
}
void redcolor(){
  for(int i = 0; i <= 240; i += 20) {
    changeColor(ring.Color(0,i,0,0));
    delay(1);
  }
  delay(500);
  for(int i = 240; i >= 0; i -= 20) {
    changeColor(ring.Color(0,i,0,0));
    delay(1);
  }
}
void bluecolor(){
  for(int i = 0; i <= 240; i += 20) {
    changeColor(ring.Color(0,0,i,0));
    delay(1);
  }
  delay(500);
  for(int i = 240; i >= 0; i -= 20) {
    changeColor(ring.Color(0,0,i,0));
    delay(1);
  }
}
void hueChase(uint32_t c, uint8_t wait) {
  for (int j=0; j<5; j++) {
    for (int q=0; q < 3; q++) {
      for (uint16_t i=0; i < ring.numPixels(); i=i+3) {
        ring.setPixelColor(i+q, c);
      }
      ring.show();
      delay(wait);
      for (uint16_t i=0; i < ring.numPixels(); i=i+3) {
        ring.setPixelColor(i+q, 0);
      }
    }
  }
}
void off() {
  changeColor(ring.Color(0, 0, 0, 0));
}
void changeColor(uint32_t color) {
  for(uint16_t i=0; i < ring.numPixels(); i++) {
    ring.setPixelColor(i, color);
    ring.show();
  }
}

CODE | Neopixel (Plate)

 
// This publishes an event every few seconds to test with
// That's all
#define SERIESRESISTOR 560
// What pin to connect the sensor to
#define SENSORPIN A0
int last_publish = 0;
int next_publish_after = 1000;
int readingbefore = 0;
String activity;
int button = D0;
void setup()
{
  Serial.begin( 9600 );
  pinMode (button, INPUT_PULLUP);
}
void loop()
{
  float reading;
  Particle.variable("drinkAct", activity);
  reading = analogRead(SENSORPIN);
  Serial.print("Analog reading ");
  Serial.println(reading);
int buttonState = digitalRead(button);
if(buttonState == LOW){
  Serial.println(buttonState);
//read button
  // convert the value to resistance
  /*reading = (1023 / reading)  - 1;
  reading = SERIESRESISTOR / reading;
  Serial.print("Sensor resistance ");
  Serial.println(reading);*/
  if( last_publish + next_publish_after < millis() ){
    // publish a random event named fork, drink or plate
    //int r = random(4);
     activity = "other";
  //  if( r == 0 ){
    //  activity = "fork";
  //  }
    //else if( r == 1 ){
    if(readingbefore - reading > 50){
      activity = "drink";
    }
  //  }else if( r == 2 ){
    //  activity = "plate";
    //}
    Particle.publish( "diot2017/musical_plates/activity", activity );
    last_publish = millis();
    //next_publish_after = random( 1000, 10000 ); // publish randomly between 1-10s
    delay(1000);
    readingbefore = reading;
  }
}else if(buttonState == HIGH){
  // convert the value to resistance
  /*reading = (1023 / reading)  - 1;
  reading = SERIESRESISTOR / reading;
  Serial.print("Sensor resistance ");
  Serial.println(reading);*/
  Serial.println(buttonState);
  if( last_publish + next_publish_after < millis() ){
    // publish a random event named fork, drink or plate
    //int r = random(4);
     activity = "space_other";
  //  if( r == 0 ){
    //  activity = "fork";
  //  }
    //else if( r == 1 ){
    if(readingbefore - reading > 500){
      activity = "space_drink";
    }
  //  }else if( r == 2 ){
    //  activity = "plate";
    //}
    Particle.publish( "diot2017/musical_plates/activity", activity );
    last_publish = millis();
    //next_publish_after = random( 1000, 10000 ); // publish randomly between 1-10s
    delay(1000);
    readingbefore = reading;
}
}
  delay( 100 );
}

CODE | Liquid Sensor (Cup)

 
int x, y, z;
int counter; // sets a counter
int button = D0;

void setup() {
  Serial.begin(9600);      // sets the serial port to 9600
  pinMode(button, INPUT_PULLUP); // sets pinMode for push button
}

void loop() {
  int buttonState = digitalRead (button); // read digital input from push button

  x = analogRead(0);       // read analog input pin 0
  y = analogRead(1);       // read analog input pin 1
  z = analogRead(2);       // read analog input pin 1


  if(x<2000 || y<2000 || z<1600) { // if the accelerometer changed position
    if (buttonState == LOW) { // jungle version
      //Serial.println("moving");
      Particle.publish("diot2017/musical_plates/activity", "plate" ); // start playing jungle background music
      Particle.publish("diot2017/musical_plates/activity", "fork" ); // start playing criket sound clip
    }else{ // space version
      //Serial.println("moving");
      Particle.publish("diot2017/musical_plates/activity", "space_plate" ); // start playing space background music
      Particle.publish("diot2017/musical_plates/activity", "space_fork" ); // start playing blade sound clip
    }
    counter = 0; // reset the counter
    delay(100);
  }else{
    Serial.println("stopping");
    counter ++; // counts every 0.1s
    //Serial.println(counter);
    delay(100);}

  if(counter == 300) { // trigger end of eating event after 30s inactivity
    //Serial.println("=======eating stopped======");
    if (buttonState == LOW) { // jungle version
    Particle.publish("eatingend"); // send a signal to plate
    Particle.publish("diot2017/musical_plates/activity", "eatingstop"); // stop the jungle background music
    }else{ // space version
    Particle.publish("space_eatingend"); // send a signal to plate
    Particle.publish("diot2017/musical_plates/activity", "space_eatingstop"); // stop the space background music
    }
    delay(100);
  }else if(counter > 30000) {
    counter = 0; // after 3000s, reset the counter
  }
}