Main Menu

search

You are here

Houseplant watering program

//
// Program name: waterSys06
//
// Runs hardware to periodically water houseplants
// one output controls a submersible fountain-style pump in a bucket of water
// the output of the pump is routed in poly hose to the plants
// needle valves are used to balance "T's"
//
// Blinks onboard LED (double-blink) at D-13 once per loop (w/ delay at end of loop to approx 1/sec)
// Reads status of pushbutton to ground
// and starts watering cycle when pressed
//
// Rev History:
// ...
// waterSys05: ... not fully tested or edited ...
// waterSys06: cleaned up & tested

// ----------- GLOBALS ----------------

// *** *** *** *** USER SET PREFERENCES HERE *** *** ***

long wateringDuration = 25; // # sec water is ON each watering cycle

long waterPeriod = 12; // # hrs between watering cycles

// --------- END user pref ------------------

// initialize variables

int HBpin = 13;
int pumpPin = 2;
int PBpin = 3;

boolean waterNowFlag = false; // flag set to signify time to water now
int blinkDuration = 100; // HB duration
long WDms = wateringDuration * 1000; // wateringDuration in sec converted to ms
long WPms = waterPeriod * 60 * 60 * 1000; // waterPeriod in hrs converted to ms
unsigned long timeLastms=0; // register to save time of last event

// end Globals

// -------------- SETUP ---------------------

void setup() {
// initialize heartbeat as output
pinMode(HBpin, OUTPUT);
// initialize pump pin as output
pinMode(pumpPin, OUTPUT);
// start with pump off
digitalWrite (pumpPin, LOW);
// initialize PBpin as input
pinMode(PBpin, INPUT_PULLUP);
}
// end setup

//---------- LOOP ------------------------

void loop() {

// HB blink twice
digitalWrite (HBpin, HIGH);
delay(blinkDuration);
digitalWrite (HBpin, LOW);
/* delay(blinkDuration);
digitalWrite (HBpin, HIGH);
delay(blinkDuration);
digitalWrite (HBpin, LOW);
*/
// is PB pressed?
if (digitalRead(PBpin) == LOW) {
// if so, set flag
waterNowFlag = true;
} // endif
//-----

// has enough time elapsed to water?
// what time is it now?
unsigned long timeNow = millis();
// Has time since timeLast exceeded WP limit? (accounting for rollover)
if ((unsigned long)(timeNow - timeLastms) >= WPms) {
// if it's time, set flag
waterNowFlag = true;
} // endif
// -----

// test flag
if (waterNowFlag == true) {
// start watering cycle
pumpON();
// reset flags:
timeLastms = timeNow;
waterNowFlag = false;
} // endif

delay(750); // set to nominally give about 1/sec heartbeat

} // end loop
// ****************** END Loop ****************

// --------------- Function Defs ----------------

void pumpON () {
digitalWrite(pumpPin, HIGH);
// pump sometimes 'sticks' and does not come on first time ...
// so here need to insert test of water flow sensor
// with ... repeated until water flow is sensed...
// ... delay 2 sec fwaiting for flow
// pump off; delay 1 sec; pump on; loop back
//
delay(WDms);
digitalWrite(pumpPin, LOW);
} // --- end pumpON ---

// ************** END PROGRAM **********************