[code] // EDIT THIS LINE ******************************** String programName = "sprinkler-03a"; // from BareMinimum14 int onTimeMinutesR1 = 8; // minutes on-time int offTimeMinutesR1 = 150; // minutes off-time; 150 min = 2 hr 30 min long baudRateSer = 9600; // ------------------------------------- // Purpose: // Control for front yard sprinkler system // // // Edit History: // rev. 01: first prototype, simple program for just one zone // with timeOn and timeOff durations specified. // Not sure if this will work. counting hours with milliseconds seems sketchy. // rev. 02: Writing a function to count-down time until next action... // seems functional, but testing with the relay board // revealed that the relay board is reverse logic: // a low input activates the relay. // Adjusted code accordingly. // rev. 03a: works // cleaned up // // // // Program operation/flow: // setup: // blinkLED() for setupLED at start & end of setup function // printPreamble() starts serial to print programName to serial monitor // readMCUinfo (if MCU has eeprom) prints out: board model (eg. nano), ID#, and comments field // // set relay output HIGH to accommodate reverse logic relay board // // loop: // turn on output relay (LOW = on) // wait for onTimeDuration // turn off output relay // wait for offTimeDuration // repeat // // // Eeprom memory configuration: // address 0 & 1 are two bytes identifying the arduino model: // NN = Nano // UN = Uno // MG = Mega // address 2 - 4 are 3 bytes of ID# // address 5 is 1 byte of length of comments // address 6+ is comment // // // Hardware configuration: // setupLED normally onboard LED D-13 // // nano mcu in funduino board for easy removal to reprogram // 4-channel relay board // D7 - relay 1 // // ------------------------ // ************** INCLUDES ******************* #include // ---------- END includes ------------ // ************** GLOBALS ******************* unsigned long currentTime = millis(); // for HB and any other LED to be turned on for some amount of time // for heartbeat: int HBLED = 13; // usually onboard LED unsigned long LASTHBLED = millis(); // ------------------------------- // for setup blinks: int setupLED = 13; // most often onboard LED // ------------------------------- // for nano dig-out pins to control relay board: int R1 = 7; // relay-1 to control zone-1 // for sprinkler on/off timing: // these are set at top of code for easy access //int onTimeMinutesR1 = 5; // minutes on-time //int offTimeMinutesR1 = 180; // minutes off-time // ---------------- END globals ----------------- // ************** SETUP ******************* void setup() { pinMode(setupLED, OUTPUT); // for setup blinks pinMode(HBLED, OUTPUT); // for heartbeat // ----------------- // at start of setup: blink setupLED // blinkLED (LEDtoBlink, prePause, numBlinks, blinkOnTime, betweenBlinks, postPause); blinkLED (setupLED, 500, 3, 200, 200, 500); // ------------------- printPreamble(); readMCUinfo(); // ---------------------------------- // for sprinkler system: pinMode(R1, OUTPUT); digitalWrite(R1, HIGH); // to accomodate reverse logic relay board inputs // ---------------------------------- // at end of setup: blink boardLED // blinkLED (LEDtoBlink, prePause, numBlinks, blinkOnTime, betweenBlinks, postPause); blinkLED (setupLED, 500, 4, 50, 160, 1500); } // -------------- END setup -------------- // ************** LOOP ******************* void loop() { // Serial.println("Starting loop..."); // start from off // turn it on: digitalWrite (R1, LOW); // reverse logic - LOW is on countDownToAction(onTimeMinutesR1); // wait for onTime // turn it off: digitalWrite (R1, HIGH); // reverse logic - HIGH is off countDownToAction(offTimeMinutesR1); // wait for offTime } // --------------- END loop --------------- // **************** FUNCTION DEFS ***************** // list of function defs: // countAminute // countDownToAction // readMCUinfo // blinkLED // printPreamble // -------------------------- void countAminute() { currentTime = millis(); unsigned long startTime = millis(); while ( (currentTime - startTime) < 60000) { // must be 60000 for a minute of ms's ************************************************ currentTime = millis(); // keep looping // menwhile, blink HB every second if (currentTime > (1000 + LASTHBLED)) { // 1 second period blinkLED (HBLED, 1, 1, 50, 1, 1); LASTHBLED = currentTime; } } // if here, then a minute has elapsed } // ----------------- END countAminute ---------------------- void countDownToAction(int minutesDuration) { // takes input of minutes // counts down to zero // returns bool doneFlag = false; while (doneFlag == false) { countAminute(); minutesDuration -= 1; if (minutesDuration <= 0) { doneFlag = true; } // endif minutes = 0 } // endWhile } // ----------------- END countDownToAction ---------------------- void readMCUinfo() { // reads model code, ID#, and comments: int modelAddr = 0; int IDAddr = 2; int commentAddr = 5; // read model: char modelChar1 = EEPROM.read(modelAddr); char modelChar2 = EEPROM.read(modelAddr+1); Serial.print("model is: "); Serial.print(modelChar1); Serial.println(modelChar2); // ---------------------- // read ID: char IDChar1 = EEPROM.read(IDAddr); char IDChar2 = EEPROM.read(IDAddr+1); char IDChar3 = EEPROM.read(IDAddr+2); Serial.print("ID is: "); Serial.print(IDChar1); Serial.print(IDChar2); Serial.println(IDChar3); // ---------------------- // read comments: byte commentLength = EEPROM.read(commentAddr); Serial.print("Comment is: "); Serial.print(commentLength); Serial.println(" chars long"); for (int n = 1; n < commentLength+1; n++) { char inChar = EEPROM.read(commentAddr + n); Serial.print(inChar); } // endfor Serial.println(""); Serial.println("----------"); } // ----------------- END readMCUinfo ---------------------- void blinkLED (int LEDtoBlink, int prePause, int numBlinks, int blinkOn, int betweenBlinks, int postPause) { // blinks some LED // typically used to blink board or setup LED to indicate start & end of setup routine // takes parameters: LEDtoBlink; pre-pause; number of blinks; blinkOn duration; betweenBlink pause duration; post-pause // downside to this function is that it ties up the processor for duration of the blink delay(prePause); for (int n = 0; n < numBlinks; n++) { digitalWrite(LEDtoBlink, HIGH); delay(blinkOn); digitalWrite(LEDtoBlink, LOW); delay(betweenBlinks); } // endfor delay(postPause); } // ----------------- END blinkLED ---------------------- void printPreamble() { Serial.begin(baudRateSer); bool timedOut = false; int startTime = millis(); while (!Serial) { currentTime = millis(); if ((currentTime - startTime) > 5000) { timedOut = true; } if (timedOut) { break; } } // endif !Serial if (!timedOut) { Serial.println(" "); Serial.println(" ---------- "); Serial.print("Program name: "); Serial.println(programName); Serial.println(" ---------- "); } // Here consider using: Serial.end(); } // ---------------- END printPreamble ---------------- // --------------- END function defs ------------ // --------------- END program ------------------ [/code]