Back to Parent

// Stage 2 code

int ledPin =  D0; // Light pin digital output
int pressureSensorPin = A0; // connected to analog pin 0
int pressureReading; // define a variable to store our sensor reading
int ledBrightness = 0; // assigning ledBrightness to 0
int removeCounter = 0; //assign removeCounter to 0
int tarePressure = 100; // assigning tarePressure to 100
int notificationThreshold = 1000; // assigning notificationThreshold to 1000

void setup() {
  Serial.begin(9600);
  Time.zone(-5); // Setting time zone to EST
  pinMode(ledPin, OUTPUT);
  Particle.variable("pressure", &pressureReading, INT);
}

void loop(){
  pressureReading = analogRead(pressureSensorPin);
  Serial.println( pressureReading );
  ledBrightness = map(pressureReading, 0, 4095, 50, 255);

// detecting the number of times the bottle was removed
  if (pressureReading < 100){
    removeCounter++;
  }

// detecting if the time is 1 PM
  if (Time.hour() == 13){

// If the pill bottle was not removed, remind to take pill
      if (removeCounter == 0){
        Particle.publish( "pill-update", "Take pill");
      }
// Else publish thanks for taking the pills
      else{
        Particle.publish( "pill-update", "Thanks for taking the pill");
      }
// Detect the low level of pills and send notification
      if (tarePressure<=pressureReading && pressureReading<=notificationThreshold){
        Particle.publish( "pill-update", "Consider reordering");
    }
}
analogWrite(ledPin, ledBrightness); // control the ledPin output
delay(1000); // take reading every second 

}
Click to Expand

Content Rating

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

0