Back to Parent

Liquid Sensor
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
  }
}
Click to Expand

Content Rating

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

0