Water Tank Monitor Using Hall Effect Switches

Yes, that is something I wrote. I have a file called Extras where I put some utility methods. It has a couple of simple timers and a method to take care daylight savings time. Here’s the code for the .h file

#include "Particle.h"

void setZone();


enum Unit {
    MILLIS, SECONDS, MINUTES
};


class Wait {

    public:

		Wait(unsigned long period, enum Unit unitOfTime);
		void begin();
        bool isUp(void);
		void reset();

	private:

        unsigned long start;
        unsigned long waitTime = 0;
};




class OneShot {

public:

	OneShot(int hour, int minute);
	bool fired(void);
	void cancel();

//private:

	int timeToFire;
	int weekdayToFire;
};

Here is the .cpp file,

#include "Extras.h"
#include "Particle.h"

void setZone() {
	int month = Time.month();
	int day = Time.day();
	int weekday = Time.weekday();
	int previousSunday = day - weekday + 1;

	if (month < 3 || month > 11) {
		Time.zone(-8);
	}else if (month > 3 && month < 11) {
		Time.zone(-7);
	}else if (month == 3) {
		int offset = (previousSunday >= 8)? -7 : -8;
		Time.zone(offset);
	} else{
		int offset = (previousSunday <= 0)? -7 : -8;
		Time.zone(offset);
	}
}


 Wait::Wait(unsigned long period, enum Unit unitOfTime) {
	 unsigned long multiplier;
     switch (unitOfTime) {
         case MILLIS:
            multiplier = 1;
            break;
         case SECONDS:
            multiplier = 1000;
            break;
         case MINUTES:
            multiplier = 60000;
            break;
     }

	 waitTime = period * multiplier;
 }


 void Wait::begin() {
	 start = millis();
 }


 bool Wait::isUp() {
     if (millis() - start >= waitTime) {
         start = millis();
         return true;
     }else{
         return false;
     }
 }

 void Wait::reset() {
	 start = millis();
 }


 OneShot::OneShot(int hour, int minute) { // timer for once a day events
	 setZone();
	 timeToFire = hour * 60 + minute;
	 if (timeToFire < (Time.hour() * 60  + Time.minute())) {
		 weekdayToFire = (Time.weekday() % 7) + 1;
	 }else{
		 weekdayToFire = Time.weekday();
	 }
 }




 bool  OneShot::fired() {
	int theTime = Time.hour() * 60 + Time.minute();
 	if (theTime >= timeToFire && weekdayToFire == Time.weekday()) {
 		weekdayToFire = (weekdayToFire % 7) + 1; // setup for the next day to run again
 		return true;
	}else{
 		return false;
 	}
 }

1 Like