Back to Parent

#include <string.h>

 int washerAnalogPin = A0;
 int dryerAnalogPin = A1;
 int led1 = D0;
 int led2 = D7;
 bool isWasherRunning;
 bool isDryerRunning;
 unsigned long WasherRunTime_sec;
 unsigned long DryerRunTime_sec;
 unsigned long msecLast;
 int washerValue;
 int dryerValue;

void setup() {
  pinMode(led1, OUTPUT);
  pinMode(led2, OUTPUT);
  digitalWrite(led2, LOW);
  // variables
  msecLast = millis();
  washerValue = 0;
  dryerValue = 0;

  Particle.variable( "Washer_WhiteBlack", washerValue) ;
  Particle.variable("Dryer_BlueGreen", dryerValue);
}

// loop() runs over and over again, as quickly as it can execute.
void loop() {

  unsigned long msec = millis();
  if (msec > msecLast) {
    unsigned long diff = msec - msecLast;
    if (diff >= 1000) {
      ReadAnalogs();//only read them once per second
      diff -= 1000;
      msecLast = msec - diff;
    }
  } else {
    msecLast = msec;// this throws away 1 millisecond every 49 days due to wrapping
  }
  digitalWrite(led1, HIGH);
  digitalWrite(led2, HIGH);

  // We'll leave it on for 1 second...
  delay(1000);

  // Then we'll turn it off...
  digitalWrite(led1, LOW);
  digitalWrite(led2, LOW);

  // Wait 1 second...
  delay(1000);
}


void ReadAnalogs(void) {
  washerValue = analogRead(washerAnalogPin);  // read the analogPin
  bool isON = (washerValue < 1024);
  if (isWasherRunning) {
    if (isON) {
      WasherRunTime_sec++;
    } else {
      isWasherRunning = false;
      if (WasherRunTime_sec >= 60) { //prevents notifications when cycling thru timer
        Particle.publish("LAUNDRY","Washer Done",60,PRIVATE);
      }
    }
  } else {
    if (isON) {
      isWasherRunning = true;
      WasherRunTime_sec = 0;
    }
  }
  dryerValue = analogRead(dryerAnalogPin);  // read the analogPin
  isON = (dryerValue < 1024);
  if (isDryerRunning) {
    if (isON) {
      DryerRunTime_sec++;
    } else {
      isDryerRunning = false;
      if (DryerRunTime_sec >= 60) { //prevents notifications when cycling thru timer
        Particle.publish("LAUNDRY","Dryer Done",60,PRIVATE);
      }
    }
  } else {
    if (isON) {
      isDryerRunning = true;
      DryerRunTime_sec = 0;
    }
  }
  if (isWasherRunning || isDryerRunning) { //used of initial testing
    digitalWrite(led2, HIGH);
  } else {
    digitalWrite(led2, LOW);
  }
}
Click to Expand

Content Rating

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

0