Back to Parent

Neopixel
// 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 );
}
Click to Expand

Content Rating

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

0