Back to Parent

// Stage 3 final 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
unsigned long oneHour = 60*60*1000; // declaring oneHour in milli-seconds
unsigned long lastPublishTime = 0; // assigning lastPublishTime to 0

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){ 

// detecting if the last published time is more than an hour
    if(millis()-lastPublishTime > oneHour){ 
// If the pill bottle was not removed, remind to take pill
      if (removeCounter == 0){
        Particle.publish( "pill-update", "Take pill");
        lastPublishTime = millis(); // record the latest run time to lastPublishTime
      }
// Else publish thanks for taking the pills
      else{
        Particle.publish( "pill-update", "Thanks for taking the pill");
        lastPublishTime = millis(); // record the latest run time to lastPublishTime
      }
// Detect the low level of pills and send notification
      if (tarePressure<=pressureReading && pressureReading<=notificationThreshold){
        Particle.publish( "pill-update", "Consider reordering");
        lastPublishTime = millis(); // record the latest run time to lastPublishTime
    }

  }

  }

  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