Irrigation Controller (up to 16 valves with Shift Registers)
-
@BulldogLowell thanks for making this! I have bookmarked this post and I'll be back when I get a chance to assemble this.
@petewill, well, I hope you like it. I wired together with my relays and damn if it didn't work on one zone. It turns out I have a bad optocoupler on the bank of 8 relays I bought. :(
But no despair, I'll have a replacement shortly.
Please be critical. Any way to make it work better is an idea worth pursuing.
Thanks!
-
can anyone take a whack at updating my sketch for the latest IDE?
I have someone on the vera forum waiting for an update but I haven't updated my net yet.
this setback will cost me a week or so to update, ask I'm looking for help...
-
This might help:
http://forum.mysensors.org/topic/172/convert-sketch-from-1-3-to-1-4
-
I updated the Irrigation Controller to 1.4.1 and added some more functionality. It will control up to 16 valves now, controls an LCD display and some fun new things.

Video of the communications icons
Take a look and I'll update when I get it all in a box:
/* MySprinkler for MySensors Arduino Multi-Zone Sprinkler Control November, 2014 *** Version 2.0 *** Upgraded to http://MySensors.org version 1.4.1 *** Expanded for up to 16 Valves *** Setup for active low relay board or comment out #define ACTIVE_LOW to switch to active high *** Switch to bitshift method vs byte arrays *** Changed RUN_ALL_ZONES Vera device to 0 (was highest valve) *** Added optional LCD display featuring remaining time, date last ran & current time *** Features 'raindrop' and 'clock' icons which indicate sensor is updating valve times and clock respectively Utilizing your Vera home automation controller and the MySensors.org gateway you can control up to a sixteen zone irrigation system with only three digital pins. This sketch will create NUMBER_OF_VALVES + 1 devices on your Vera controller This sketch features the following: * Allows you to cycle through All zones (RUN_ALL_ZONES) or individual zone (RUN_SINGLE_ZONE) control. * Use the 0th controller to activate RUN_ALL_ZONES (each zone in numeric sequence 1 to n) using Variable1 as the "ON" time in minutes in each of the vera devices created. * Use the individual zone controller to activate a single zone. This feature uses Variable2 as the "ON" time for each individual device/zone. * Connect according to pinout below and uses Shift Registers as to allow the MySensors standard radio configuration and still leave available digital pins * Turning on any zone will stop the current process and begin that particular process. * Turning off any zone will stop the current process and turn off all zones. * To push your new time intervals for your zones, simply change the variable on your Vera and your arduino will call to Vera once a minute and update accordingly. * Pushbutton activation to RUN_ALL_ZONES * LED status indicator INSTRUCTIONS: * After assembling your arduino, radio, decoupling capacitors, shift register(s), status LED, pushbutton LCD (I2C connected to A4 and A5) and relays, and load the sketch. * Following the instructions at https://MySensors.org include the device to your MySensors Gateway. * Verify that each new device has a Variable1 and Variable2. Populate data accordingly with whole minutes for the RUN_ALL_ZONES routine and the RUN_SINGLE_ZONE routines. The values entered may be zero. * Once you have entered values for each zone and each variable, save the settings by pressing the red save button on your Vera. * Restart your arduino; verify the settings are loaded into your arduino with the serial monitor; the array will be printed on the serial monitor. * Your arduino should slow-flash, indicating that it is in ready mode. * There are multiple debug serial prints that can be monitored to assure that it is operating properly. * https://bitbucket.org/fmalpartida/new-liquidcrystal/downloads for the I2C library, or use yours Contributed by Jim (BulldogLowell@gmail.com) and is released to the public domain */ // #include <Wire.h> #include <Time.h> #include <MySensor.h> #include <SPI.h> #include <LiquidCrystal_I2C.h> // #define NUMBER_OF_VALVES 8 // Change this to set your valve count up to 16. #define VALVE_RESET_TIME 7500UL // Change this (in milliseconds) for the time you need your valves to hydraulically reset and change state #define RADIO_ID 9 // AUTO // Change this to fix your Radio ID or use Auto // #define ACTIVE_LOW // comment out this line if your relays are active high // #define DEBUG_ON // comment out to supress serial monitor output // #ifdef ACTIVE_LOW #define BITSHIFT_VALVE_NUMBER ~(1U << (valveNumber-1)) #define ALL_VALVES_OFF 0xFFFF #else #define BITSHIFT_VALVE_NUMBER (1U << (valveNumber-1)) #define ALL_VALVES_OFF 0U #endif // #ifdef DEBUG_ON #define DEBUG_PRINT(x) Serial.print(x) #define DEBUG_PRINTLN(x) Serial.println(x) #define SERIAL_START(x) Serial.begin(x) #else #define DEBUG_PRINT(x) #define DEBUG_PRINTLN(x) #define SERIAL_START(x) #endif // typedef enum { STAND_BY_ALL_OFF, RUN_SINGLE_ZONE, RUN_ALL_ZONES, CYCLE_COMPLETE} SprinklerStates; // SprinklerStates state = STAND_BY_ALL_OFF; SprinklerStates lastState; // int allZoneTime [NUMBER_OF_VALVES + 1]; int valveSoloTime [NUMBER_OF_VALVES + 1]; int valveNumber; int lastValve; unsigned long startMillis; const int ledPin = 5; boolean buttonPushed = false; boolean showTime = true; boolean clockUpdating = false; boolean recentUpdate = true; const char *dayOfWeek[] = { "Null","Sunday ","Monday ", "Tuesday ", "Wednesday ", "Thursday ", "Friday ", "Saturday "}; // time_t lastTimeRun = 0; //Setup Shift Register... const int latchPin = 8; const int clockPin = 4; const int dataPin = 7; // uint8_t clock[8] = {0x0,0xe,0x15,0x17,0x11,0xe,0x0}; // fetching time indicator uint8_t raindrop[8] = {0x4,0x4,0xA,0xA,0x11,0xE,0x0,}; // fetching Valve Data indicator // LiquidCrystal_I2C lcd(0x27, 16, 2); // set the LCD I2C address to 0x27 (16 characters and 2 line display) MySensor gw; // MyMessage msg1valve(0,V_LIGHT); MyMessage var1valve(0,V_VAR1); MyMessage var2valve(0,V_VAR2); // void setup() { Serial.begin(115200); delay(5000); lcd.init(); lcd.clear(); lcd.backlight(); lcd.createChar(0, clock); lcd.createChar(1, raindrop); DEBUG_PRINTLN(F("Initialising...")); pinMode(latchPin, OUTPUT); pinMode(clockPin, OUTPUT); pinMode(dataPin, OUTPUT); pinMode(ledPin, OUTPUT); attachInterrupt(2, PushButton, CHANGE); digitalWrite (ledPin, HIGH); // //check for saved date in EEPROM DEBUG_PRINTLN(F("Checking EEPROM for stored date:")); if (gw.loadState(0) == 0xFF); // EEPROM flag { DEBUG_PRINTLN(F("Retreiving last run time from EEPROM...")); for (int i = 0; i < 4 ; i++) { lastTimeRun = lastTimeRun << 8; lastTimeRun = lastTimeRun | gw.loadState(i+1); // assemble 4 bytes into an ussigned long epoch timestamp } } gw.begin(getVariables, RADIO_ID, false); // Change 'false' to 'true' to create a Radio repeating node gw.sendSketchInfo("MySprinkler", "2.0"); for (byte i = 0; i <= NUMBER_OF_VALVES; i++) { gw.present(i, S_LIGHT); } DEBUG_PRINTLN(F("Sensor Presentation Complete")); // DEBUG_PRINTLN(F("Turning All Valves Off...")); updateRelays(ALL_VALVES_OFF); digitalWrite (ledPin, LOW); DEBUG_PRINTLN(F("Ready...")); // lcd.setCursor(0, 0); lcd.print(F(" Syncing Time ")); lcd.setCursor(15, 0); lcd.write(0); //lcd.print(0, BYTE); lcd.setCursor(0, 1); int clockCounter = 0; while(timeStatus() == timeNotSet && clockCounter < 21) { gw.process(); gw.requestTime(receiveTime); DEBUG_PRINTLN(F("Requesting time from Gateway:")); delay(1000); lcd.print("."); DEBUG_PRINT(F(".")); clockCounter++; if (clockCounter > 16) { DEBUG_PRINTLN(F("Failed initial clock synchronization!")); lcd.clear(); lcd.print(F(" Failed Clock ")); lcd.setCursor(0,1); lcd.print(F(" Syncronization ")); delay(2000); break; } } // lcd.clear(); } // void loop() { gw.process(); updateClock(); updateDisplay(); goGetValveTimes(); // if (buttonPushed) { DEBUG_PRINTLN(F("Button Pressed")); if (state != RUN_ALL_ZONES); { state = RUN_ALL_ZONES; valveNumber = 1; gw.send(msg1valve.setSensor(0).set(true), false); startMillis = millis(); for (byte i = 0; i < 5; i++) // flash lcd backlight on button press { lcd.noBacklight(); delay(25); lcd.backlight(); } delay(50); fastClear(); lcd.setCursor(0,0); lcd.print(F("*AllZone Active*")); lcd.setCursor(0,0); lcd.print(F(" Cycling Zones ")); delay(1000); DEBUG_PRINT(F("State = ")); DEBUG_PRINTLN(state); } buttonPushed = false; } if (state == STAND_BY_ALL_OFF) { slowToggleLED (); if (state != lastState) { updateRelays(ALL_VALVES_OFF); DEBUG_PRINTLN(F("State Changed... all Zones off")); for (byte i = 0; i <= NUMBER_OF_VALVES; i++) { delay(50); gw.send(msg1valve.setSensor(i).set(false), false); } lastValve = -1; } } // else if (state == RUN_ALL_ZONES) { if (lastValve != valveNumber) { for (byte i = 0; i <= NUMBER_OF_VALVES; i++) { if (i == 0 || i == valveNumber) { gw.send(msg1valve.setSensor(i).set(true), false); } else { gw.send(msg1valve.setSensor(i).set(false), false); } } } lastValve = valveNumber; fastToggleLed(); if (state != lastState) { valveNumber = 1; updateRelays(ALL_VALVES_OFF); DEBUG_PRINTLN(F("State Changed, Running All Zones...")); } unsigned long nowMillis = millis(); if (nowMillis - startMillis < VALVE_RESET_TIME) { updateRelays(ALL_VALVES_OFF); } else if (nowMillis - startMillis < (allZoneTime[valveNumber] * 60000UL)) { updateRelays(BITSHIFT_VALVE_NUMBER); } else { DEBUG_PRINTLN(F("Changing Valves...")); updateRelays(ALL_VALVES_OFF); startMillis = millis(); valveNumber++; if (valveNumber > NUMBER_OF_VALVES) { state = CYCLE_COMPLETE; startMillis = millis(); lastValve = -1; lastTimeRun = now(); saveDateToEEPROM(lastTimeRun); for (byte i = 0; i <= NUMBER_OF_VALVES; i++) { gw.send(msg1valve.setSensor(i).set(false), false); } DEBUG_PRINT(F("State = ")); DEBUG_PRINTLN(state); } } } // else if (state == RUN_SINGLE_ZONE) { fastToggleLed(); if (state != lastState) { for (byte i = 0; i <= NUMBER_OF_VALVES; i++) { if (i == 0 || i == valveNumber) { gw.send(msg1valve.setSensor(i).set(true), false); } else { gw.send(msg1valve.setSensor(i).set(false), false); } } DEBUG_PRINTLN(F("State Changed, Single Zone Running...")); DEBUG_PRINT(F("Zone: ")); DEBUG_PRINTLN(valveNumber); } unsigned long nowMillis = millis(); if (nowMillis - startMillis < VALVE_RESET_TIME) { updateRelays(ALL_VALVES_OFF); } else if (nowMillis - startMillis < (valveSoloTime [valveNumber] * 60000UL)) { updateRelays(BITSHIFT_VALVE_NUMBER); } else { updateRelays(ALL_VALVES_OFF); for (byte i = 0; i <= NUMBER_OF_VALVES; i++) { gw.send(msg1valve.setSensor(i).set(false), false); } state = CYCLE_COMPLETE; startMillis = millis(); DEBUG_PRINT(F("State = ")); DEBUG_PRINTLN(state); } lastTimeRun = now(); } else if (state == CYCLE_COMPLETE) { if (millis() - startMillis < 30000UL) { fastToggleLed(); } else { state = STAND_BY_ALL_OFF; } } lastState = state; } // void updateRelays(int value) { digitalWrite(latchPin, LOW); shiftOut(dataPin, clockPin, MSBFIRST, highByte(value)); shiftOut(dataPin, clockPin, MSBFIRST, lowByte(value)); digitalWrite(latchPin, HIGH); } // void PushButton() //interrupt with debounce { static unsigned long last_interrupt_time = 0; unsigned long interrupt_time = millis(); if (interrupt_time - last_interrupt_time > 200) { buttonPushed = true; } last_interrupt_time = interrupt_time; } // void fastToggleLed() { static unsigned long fastLedTimer; if (millis() - fastLedTimer >= 100UL) { digitalWrite(ledPin, !digitalRead(ledPin)); fastLedTimer = millis (); } } // void slowToggleLED () { static unsigned long slowLedTimer; if (millis() - slowLedTimer >= 1250UL) { digitalWrite(ledPin, !digitalRead(ledPin)); slowLedTimer = millis (); } } // void getVariables(const MyMessage &message) { boolean zoneTimeUpdate = false; if (message.isAck()) { DEBUG_PRINTLN(F("This is an ack from gateway")); } for (byte i = 0; i<= NUMBER_OF_VALVES; i++) { if (message.sensor == i) { if (message.type == V_LIGHT) { int switchState = atoi(message.data); if (switchState == 0) { state = STAND_BY_ALL_OFF; DEBUG_PRINTLN(F("Recieved Instruction to Cancel...")); } else { if (i == 0) { state = RUN_ALL_ZONES; valveNumber = 1; DEBUG_PRINTLN(F("Recieved Instruction to Run All Zones...")); } else { state = RUN_SINGLE_ZONE; valveNumber = i; DEBUG_PRINT(F("Recieved Instruction to Activate Zone: ")); DEBUG_PRINTLN(i); } } startMillis = millis(); } else if (message.type == V_VAR1) { int variable1 = atoi(message.data);// RUN_ALL_ZONES time DEBUG_PRINT(F("Recieved variable1 valve:")); DEBUG_PRINT(i); DEBUG_PRINT(F(" = ")); DEBUG_PRINTLN(variable1); if (variable1 != allZoneTime[i]) { allZoneTime[i] = variable1; zoneTimeUpdate = true; } } else if (message.type == V_VAR2) { int variable2 = atoi(message.data);// RUN_SINGLE_ZONE time DEBUG_PRINT(F("Recieved variable2 valve:")); DEBUG_PRINT(i); DEBUG_PRINT(F(" = ")); DEBUG_PRINTLN(variable2); if (variable2 != valveSoloTime[i]) { valveSoloTime[i] = variable2; zoneTimeUpdate = true; } } } } if (zoneTimeUpdate) { // DEBUG_PRINTLN(F("New Zone Times Recieved...")); for (byte i = 0; i <= NUMBER_OF_VALVES; i++) { if (i != 0) { DEBUG_PRINT(F("Zone ")); DEBUG_PRINT(i); DEBUG_PRINT(F(" individual time: ")); DEBUG_PRINT(valveSoloTime[i]); DEBUG_PRINT(F(" group time: ")); DEBUG_PRINTLN(allZoneTime[i]); recentUpdate = true; } } } else { recentUpdate = false; } } // void updateDisplay() { static unsigned long lastUpdateTime; static boolean displayToggle = false; //static byte toggleCounter = 0; static SprinklerStates lastDisplayState; if (state != lastDisplayState || millis() - lastUpdateTime >= 3000UL) { displayToggle = !displayToggle; switch (state){ case STAND_BY_ALL_OFF: // fastClear(); lcd.setCursor(0,0); if (displayToggle) { lcd.print(F(" System Ready ")); if (clockUpdating) { lcd.setCursor(15,0); lcd.write(0); } lcd.setCursor(0,1); lcd.print(hourFormat12() < 10 ? F(" ") : F("")); lcd.print(hourFormat12()); lcd.print(minute() < 10 ? F(":0") : F(":")); lcd.print(minute()); lcd.print(isAM() ? F("am") : F("pm")); lcd.print(F(" ")); lcd.print(month() < 10? F("0") : F("")); lcd.print(month()); lcd.print(day() < 10? F("/0") : F("/")); lcd.print(day()); lcd.print(F("/")); lcd.print(year()%100); } else { lcd.print(F(" Last Watered ")); if (clockUpdating) { lcd.setCursor(15,0); lcd.write(0); } lcd.setCursor(0,1); lcd.print(dayOfWeek[weekday(lastTimeRun)]); lcd.setCursor(11,1); lcd.print(month(lastTimeRun) < 10 ? F(" ") : F("")); lcd.print(month(lastTimeRun)); lcd.print(day(lastTimeRun) < 10 ? F("/0") : F("/")); lcd.print(day(lastTimeRun)); } break; case RUN_SINGLE_ZONE: // fastClear(); lcd.setCursor(0,0); if (displayToggle) { lcd.print(F("Single Zone Mode")); lcd.setCursor(0,1); lcd.print(F(" Zone:")); if (valveNumber < 10) lcd.print(F("0")); lcd.print(valveNumber); lcd.print(F(" Active")); } else { lcd.print(F(" Time Remaining ")); lcd.setCursor(0,1); unsigned long timeRemaining = (valveSoloTime[valveNumber] * 60) - ((millis() - startMillis) / 1000); lcd.print(timeRemaining / 60 < 10 ? " 0" : " "); lcd.print(timeRemaining / 60); lcd.print("min"); lcd.print(timeRemaining % 60 < 10 ? " 0" : " "); lcd.print(timeRemaining % 60); lcd.print("sec "); } break; case RUN_ALL_ZONES: // fastClear(); lcd.setCursor(0,0); if (displayToggle) { lcd.print(F(" All-Zone Mode ")); lcd.setCursor(0,1); lcd.print(F(" Zone:")); if (valveNumber < 10) lcd.print(F("0")); lcd.print(valveNumber); lcd.print(F(" Active ")); } else { lcd.print(F(" Time Remaining ")); lcd.setCursor(0,1); int timeRemaining = (allZoneTime[valveNumber] * 60) - ((millis() - startMillis) / 1000); lcd.print((timeRemaining / 60) < 10 ? " 0" : " "); lcd.print(timeRemaining / 60); lcd.print("min"); lcd.print(timeRemaining % 60 < 10 ? " 0" : " "); lcd.print(timeRemaining % 60); lcd.print("sec "); } break; case CYCLE_COMPLETE: // if (displayToggle) { lcd.setCursor(0,0); lcd.print(F(" Watering Cycle ")); lcd.setCursor(0,1); lcd.print(F(" Complete ")); } else { int totalTimeRan = 0; for (int i = 1; i < NUMBER_OF_VALVES + 1; i++) { totalTimeRan += allZoneTime[i]; } lcd.setCursor(0,0); lcd.print(F(" Total Time Run ")); lcd.setCursor(0,1); lcd.print(totalTimeRan < 10 ? " 0" : " "); lcd.print(totalTimeRan); lcd.print(" Minutes "); } } lastUpdateTime = millis(); } lastDisplayState = state; } void receiveTime(time_t newTime) { DEBUG_PRINTLN(F("Time value received and updated...")); int lastSecond = second(); int lastMinute = minute(); int lastHour = hour(); setTime(newTime); if (((second() != lastSecond) || (minute() != lastMinute) || (hour() != lastHour)) || showTime) { DEBUG_PRINTLN(F("Clock updated....")); DEBUG_PRINT(F("Sensor's time currently set to:")); DEBUG_PRINT(hourFormat12() < 10 ? F(" 0") : F(" ")); DEBUG_PRINT(hourFormat12()); DEBUG_PRINT(minute() < 10 ? F(":0") : F(":")); DEBUG_PRINT(minute()); DEBUG_PRINTLN(isAM() ? F("am") : F("pm")); DEBUG_PRINT(month()); DEBUG_PRINT(F("/")); DEBUG_PRINT(day()); DEBUG_PRINT(F("/")); DEBUG_PRINTLN(year()); DEBUG_PRINTLN(dayOfWeek[weekday()]); showTime = false; } else { DEBUG_PRINTLN(F("Sensor's time did NOT need adjustment greater than 1 second.")); } clockUpdating = false; } void fastClear() { lcd.setCursor(0,0); lcd.print(F(" ")); lcd.setCursor(0,1); lcd.print(F(" ")); } // void updateClock() { static unsigned long lastVeraGetTime; if (millis() - lastVeraGetTime >= 60000UL) // updates clock time and gets zone times from vera once every 10 minutes { DEBUG_PRINTLN(F("Requesting time and valve data from Gateway...")); lcd.setCursor(15,0); lcd.write(0); clockUpdating = true; gw.requestTime(receiveTime); lastVeraGetTime = millis(); } } // void saveDateToEEPROM(unsigned long theDate) { DEBUG_PRINTLN(F("Saving Last Run date")); if (gw.loadState(0) != 0xFF) { gw.saveState(0,0xFF); // EEPROM flag for last date saved stored in EEPROM (location zero) } // for (int i = 1; i < 5; i++) { gw.saveState(5-i, byte(theDate >> 8 * (i-1)));// store epoch datestamp in 4 bytes of EEPROM starting in location one } } // void goGetValveTimes() { static unsigned long valveUpdateTime; static byte valveIndex = 1; if (millis() - valveUpdateTime >= 300000UL / NUMBER_OF_VALVES)// update each valve once every 5 mins (distributes the traffic) { DEBUG_PRINTLN(F("Calling for Valve Times...")); lcd.setCursor(15, 0); lcd.write(1); gw.request(valveIndex, V_VAR1); gw.request(valveIndex, V_VAR2); valveUpdateTime = millis(); valveIndex++; if (valveIndex > NUMBER_OF_VALVES+1) { valveIndex = 1; } } } -
I updated the Irrigation Controller to 1.4.1 and added some more functionality. It will control up to 16 valves now, controls an LCD display and some fun new things.

Video of the communications icons
Take a look and I'll update when I get it all in a box:
/* MySprinkler for MySensors Arduino Multi-Zone Sprinkler Control November, 2014 *** Version 2.0 *** Upgraded to http://MySensors.org version 1.4.1 *** Expanded for up to 16 Valves *** Setup for active low relay board or comment out #define ACTIVE_LOW to switch to active high *** Switch to bitshift method vs byte arrays *** Changed RUN_ALL_ZONES Vera device to 0 (was highest valve) *** Added optional LCD display featuring remaining time, date last ran & current time *** Features 'raindrop' and 'clock' icons which indicate sensor is updating valve times and clock respectively Utilizing your Vera home automation controller and the MySensors.org gateway you can control up to a sixteen zone irrigation system with only three digital pins. This sketch will create NUMBER_OF_VALVES + 1 devices on your Vera controller This sketch features the following: * Allows you to cycle through All zones (RUN_ALL_ZONES) or individual zone (RUN_SINGLE_ZONE) control. * Use the 0th controller to activate RUN_ALL_ZONES (each zone in numeric sequence 1 to n) using Variable1 as the "ON" time in minutes in each of the vera devices created. * Use the individual zone controller to activate a single zone. This feature uses Variable2 as the "ON" time for each individual device/zone. * Connect according to pinout below and uses Shift Registers as to allow the MySensors standard radio configuration and still leave available digital pins * Turning on any zone will stop the current process and begin that particular process. * Turning off any zone will stop the current process and turn off all zones. * To push your new time intervals for your zones, simply change the variable on your Vera and your arduino will call to Vera once a minute and update accordingly. * Pushbutton activation to RUN_ALL_ZONES * LED status indicator INSTRUCTIONS: * After assembling your arduino, radio, decoupling capacitors, shift register(s), status LED, pushbutton LCD (I2C connected to A4 and A5) and relays, and load the sketch. * Following the instructions at https://MySensors.org include the device to your MySensors Gateway. * Verify that each new device has a Variable1 and Variable2. Populate data accordingly with whole minutes for the RUN_ALL_ZONES routine and the RUN_SINGLE_ZONE routines. The values entered may be zero. * Once you have entered values for each zone and each variable, save the settings by pressing the red save button on your Vera. * Restart your arduino; verify the settings are loaded into your arduino with the serial monitor; the array will be printed on the serial monitor. * Your arduino should slow-flash, indicating that it is in ready mode. * There are multiple debug serial prints that can be monitored to assure that it is operating properly. * https://bitbucket.org/fmalpartida/new-liquidcrystal/downloads for the I2C library, or use yours Contributed by Jim (BulldogLowell@gmail.com) and is released to the public domain */ // #include <Wire.h> #include <Time.h> #include <MySensor.h> #include <SPI.h> #include <LiquidCrystal_I2C.h> // #define NUMBER_OF_VALVES 8 // Change this to set your valve count up to 16. #define VALVE_RESET_TIME 7500UL // Change this (in milliseconds) for the time you need your valves to hydraulically reset and change state #define RADIO_ID 9 // AUTO // Change this to fix your Radio ID or use Auto // #define ACTIVE_LOW // comment out this line if your relays are active high // #define DEBUG_ON // comment out to supress serial monitor output // #ifdef ACTIVE_LOW #define BITSHIFT_VALVE_NUMBER ~(1U << (valveNumber-1)) #define ALL_VALVES_OFF 0xFFFF #else #define BITSHIFT_VALVE_NUMBER (1U << (valveNumber-1)) #define ALL_VALVES_OFF 0U #endif // #ifdef DEBUG_ON #define DEBUG_PRINT(x) Serial.print(x) #define DEBUG_PRINTLN(x) Serial.println(x) #define SERIAL_START(x) Serial.begin(x) #else #define DEBUG_PRINT(x) #define DEBUG_PRINTLN(x) #define SERIAL_START(x) #endif // typedef enum { STAND_BY_ALL_OFF, RUN_SINGLE_ZONE, RUN_ALL_ZONES, CYCLE_COMPLETE} SprinklerStates; // SprinklerStates state = STAND_BY_ALL_OFF; SprinklerStates lastState; // int allZoneTime [NUMBER_OF_VALVES + 1]; int valveSoloTime [NUMBER_OF_VALVES + 1]; int valveNumber; int lastValve; unsigned long startMillis; const int ledPin = 5; boolean buttonPushed = false; boolean showTime = true; boolean clockUpdating = false; boolean recentUpdate = true; const char *dayOfWeek[] = { "Null","Sunday ","Monday ", "Tuesday ", "Wednesday ", "Thursday ", "Friday ", "Saturday "}; // time_t lastTimeRun = 0; //Setup Shift Register... const int latchPin = 8; const int clockPin = 4; const int dataPin = 7; // uint8_t clock[8] = {0x0,0xe,0x15,0x17,0x11,0xe,0x0}; // fetching time indicator uint8_t raindrop[8] = {0x4,0x4,0xA,0xA,0x11,0xE,0x0,}; // fetching Valve Data indicator // LiquidCrystal_I2C lcd(0x27, 16, 2); // set the LCD I2C address to 0x27 (16 characters and 2 line display) MySensor gw; // MyMessage msg1valve(0,V_LIGHT); MyMessage var1valve(0,V_VAR1); MyMessage var2valve(0,V_VAR2); // void setup() { Serial.begin(115200); delay(5000); lcd.init(); lcd.clear(); lcd.backlight(); lcd.createChar(0, clock); lcd.createChar(1, raindrop); DEBUG_PRINTLN(F("Initialising...")); pinMode(latchPin, OUTPUT); pinMode(clockPin, OUTPUT); pinMode(dataPin, OUTPUT); pinMode(ledPin, OUTPUT); attachInterrupt(2, PushButton, CHANGE); digitalWrite (ledPin, HIGH); // //check for saved date in EEPROM DEBUG_PRINTLN(F("Checking EEPROM for stored date:")); if (gw.loadState(0) == 0xFF); // EEPROM flag { DEBUG_PRINTLN(F("Retreiving last run time from EEPROM...")); for (int i = 0; i < 4 ; i++) { lastTimeRun = lastTimeRun << 8; lastTimeRun = lastTimeRun | gw.loadState(i+1); // assemble 4 bytes into an ussigned long epoch timestamp } } gw.begin(getVariables, RADIO_ID, false); // Change 'false' to 'true' to create a Radio repeating node gw.sendSketchInfo("MySprinkler", "2.0"); for (byte i = 0; i <= NUMBER_OF_VALVES; i++) { gw.present(i, S_LIGHT); } DEBUG_PRINTLN(F("Sensor Presentation Complete")); // DEBUG_PRINTLN(F("Turning All Valves Off...")); updateRelays(ALL_VALVES_OFF); digitalWrite (ledPin, LOW); DEBUG_PRINTLN(F("Ready...")); // lcd.setCursor(0, 0); lcd.print(F(" Syncing Time ")); lcd.setCursor(15, 0); lcd.write(0); //lcd.print(0, BYTE); lcd.setCursor(0, 1); int clockCounter = 0; while(timeStatus() == timeNotSet && clockCounter < 21) { gw.process(); gw.requestTime(receiveTime); DEBUG_PRINTLN(F("Requesting time from Gateway:")); delay(1000); lcd.print("."); DEBUG_PRINT(F(".")); clockCounter++; if (clockCounter > 16) { DEBUG_PRINTLN(F("Failed initial clock synchronization!")); lcd.clear(); lcd.print(F(" Failed Clock ")); lcd.setCursor(0,1); lcd.print(F(" Syncronization ")); delay(2000); break; } } // lcd.clear(); } // void loop() { gw.process(); updateClock(); updateDisplay(); goGetValveTimes(); // if (buttonPushed) { DEBUG_PRINTLN(F("Button Pressed")); if (state != RUN_ALL_ZONES); { state = RUN_ALL_ZONES; valveNumber = 1; gw.send(msg1valve.setSensor(0).set(true), false); startMillis = millis(); for (byte i = 0; i < 5; i++) // flash lcd backlight on button press { lcd.noBacklight(); delay(25); lcd.backlight(); } delay(50); fastClear(); lcd.setCursor(0,0); lcd.print(F("*AllZone Active*")); lcd.setCursor(0,0); lcd.print(F(" Cycling Zones ")); delay(1000); DEBUG_PRINT(F("State = ")); DEBUG_PRINTLN(state); } buttonPushed = false; } if (state == STAND_BY_ALL_OFF) { slowToggleLED (); if (state != lastState) { updateRelays(ALL_VALVES_OFF); DEBUG_PRINTLN(F("State Changed... all Zones off")); for (byte i = 0; i <= NUMBER_OF_VALVES; i++) { delay(50); gw.send(msg1valve.setSensor(i).set(false), false); } lastValve = -1; } } // else if (state == RUN_ALL_ZONES) { if (lastValve != valveNumber) { for (byte i = 0; i <= NUMBER_OF_VALVES; i++) { if (i == 0 || i == valveNumber) { gw.send(msg1valve.setSensor(i).set(true), false); } else { gw.send(msg1valve.setSensor(i).set(false), false); } } } lastValve = valveNumber; fastToggleLed(); if (state != lastState) { valveNumber = 1; updateRelays(ALL_VALVES_OFF); DEBUG_PRINTLN(F("State Changed, Running All Zones...")); } unsigned long nowMillis = millis(); if (nowMillis - startMillis < VALVE_RESET_TIME) { updateRelays(ALL_VALVES_OFF); } else if (nowMillis - startMillis < (allZoneTime[valveNumber] * 60000UL)) { updateRelays(BITSHIFT_VALVE_NUMBER); } else { DEBUG_PRINTLN(F("Changing Valves...")); updateRelays(ALL_VALVES_OFF); startMillis = millis(); valveNumber++; if (valveNumber > NUMBER_OF_VALVES) { state = CYCLE_COMPLETE; startMillis = millis(); lastValve = -1; lastTimeRun = now(); saveDateToEEPROM(lastTimeRun); for (byte i = 0; i <= NUMBER_OF_VALVES; i++) { gw.send(msg1valve.setSensor(i).set(false), false); } DEBUG_PRINT(F("State = ")); DEBUG_PRINTLN(state); } } } // else if (state == RUN_SINGLE_ZONE) { fastToggleLed(); if (state != lastState) { for (byte i = 0; i <= NUMBER_OF_VALVES; i++) { if (i == 0 || i == valveNumber) { gw.send(msg1valve.setSensor(i).set(true), false); } else { gw.send(msg1valve.setSensor(i).set(false), false); } } DEBUG_PRINTLN(F("State Changed, Single Zone Running...")); DEBUG_PRINT(F("Zone: ")); DEBUG_PRINTLN(valveNumber); } unsigned long nowMillis = millis(); if (nowMillis - startMillis < VALVE_RESET_TIME) { updateRelays(ALL_VALVES_OFF); } else if (nowMillis - startMillis < (valveSoloTime [valveNumber] * 60000UL)) { updateRelays(BITSHIFT_VALVE_NUMBER); } else { updateRelays(ALL_VALVES_OFF); for (byte i = 0; i <= NUMBER_OF_VALVES; i++) { gw.send(msg1valve.setSensor(i).set(false), false); } state = CYCLE_COMPLETE; startMillis = millis(); DEBUG_PRINT(F("State = ")); DEBUG_PRINTLN(state); } lastTimeRun = now(); } else if (state == CYCLE_COMPLETE) { if (millis() - startMillis < 30000UL) { fastToggleLed(); } else { state = STAND_BY_ALL_OFF; } } lastState = state; } // void updateRelays(int value) { digitalWrite(latchPin, LOW); shiftOut(dataPin, clockPin, MSBFIRST, highByte(value)); shiftOut(dataPin, clockPin, MSBFIRST, lowByte(value)); digitalWrite(latchPin, HIGH); } // void PushButton() //interrupt with debounce { static unsigned long last_interrupt_time = 0; unsigned long interrupt_time = millis(); if (interrupt_time - last_interrupt_time > 200) { buttonPushed = true; } last_interrupt_time = interrupt_time; } // void fastToggleLed() { static unsigned long fastLedTimer; if (millis() - fastLedTimer >= 100UL) { digitalWrite(ledPin, !digitalRead(ledPin)); fastLedTimer = millis (); } } // void slowToggleLED () { static unsigned long slowLedTimer; if (millis() - slowLedTimer >= 1250UL) { digitalWrite(ledPin, !digitalRead(ledPin)); slowLedTimer = millis (); } } // void getVariables(const MyMessage &message) { boolean zoneTimeUpdate = false; if (message.isAck()) { DEBUG_PRINTLN(F("This is an ack from gateway")); } for (byte i = 0; i<= NUMBER_OF_VALVES; i++) { if (message.sensor == i) { if (message.type == V_LIGHT) { int switchState = atoi(message.data); if (switchState == 0) { state = STAND_BY_ALL_OFF; DEBUG_PRINTLN(F("Recieved Instruction to Cancel...")); } else { if (i == 0) { state = RUN_ALL_ZONES; valveNumber = 1; DEBUG_PRINTLN(F("Recieved Instruction to Run All Zones...")); } else { state = RUN_SINGLE_ZONE; valveNumber = i; DEBUG_PRINT(F("Recieved Instruction to Activate Zone: ")); DEBUG_PRINTLN(i); } } startMillis = millis(); } else if (message.type == V_VAR1) { int variable1 = atoi(message.data);// RUN_ALL_ZONES time DEBUG_PRINT(F("Recieved variable1 valve:")); DEBUG_PRINT(i); DEBUG_PRINT(F(" = ")); DEBUG_PRINTLN(variable1); if (variable1 != allZoneTime[i]) { allZoneTime[i] = variable1; zoneTimeUpdate = true; } } else if (message.type == V_VAR2) { int variable2 = atoi(message.data);// RUN_SINGLE_ZONE time DEBUG_PRINT(F("Recieved variable2 valve:")); DEBUG_PRINT(i); DEBUG_PRINT(F(" = ")); DEBUG_PRINTLN(variable2); if (variable2 != valveSoloTime[i]) { valveSoloTime[i] = variable2; zoneTimeUpdate = true; } } } } if (zoneTimeUpdate) { // DEBUG_PRINTLN(F("New Zone Times Recieved...")); for (byte i = 0; i <= NUMBER_OF_VALVES; i++) { if (i != 0) { DEBUG_PRINT(F("Zone ")); DEBUG_PRINT(i); DEBUG_PRINT(F(" individual time: ")); DEBUG_PRINT(valveSoloTime[i]); DEBUG_PRINT(F(" group time: ")); DEBUG_PRINTLN(allZoneTime[i]); recentUpdate = true; } } } else { recentUpdate = false; } } // void updateDisplay() { static unsigned long lastUpdateTime; static boolean displayToggle = false; //static byte toggleCounter = 0; static SprinklerStates lastDisplayState; if (state != lastDisplayState || millis() - lastUpdateTime >= 3000UL) { displayToggle = !displayToggle; switch (state){ case STAND_BY_ALL_OFF: // fastClear(); lcd.setCursor(0,0); if (displayToggle) { lcd.print(F(" System Ready ")); if (clockUpdating) { lcd.setCursor(15,0); lcd.write(0); } lcd.setCursor(0,1); lcd.print(hourFormat12() < 10 ? F(" ") : F("")); lcd.print(hourFormat12()); lcd.print(minute() < 10 ? F(":0") : F(":")); lcd.print(minute()); lcd.print(isAM() ? F("am") : F("pm")); lcd.print(F(" ")); lcd.print(month() < 10? F("0") : F("")); lcd.print(month()); lcd.print(day() < 10? F("/0") : F("/")); lcd.print(day()); lcd.print(F("/")); lcd.print(year()%100); } else { lcd.print(F(" Last Watered ")); if (clockUpdating) { lcd.setCursor(15,0); lcd.write(0); } lcd.setCursor(0,1); lcd.print(dayOfWeek[weekday(lastTimeRun)]); lcd.setCursor(11,1); lcd.print(month(lastTimeRun) < 10 ? F(" ") : F("")); lcd.print(month(lastTimeRun)); lcd.print(day(lastTimeRun) < 10 ? F("/0") : F("/")); lcd.print(day(lastTimeRun)); } break; case RUN_SINGLE_ZONE: // fastClear(); lcd.setCursor(0,0); if (displayToggle) { lcd.print(F("Single Zone Mode")); lcd.setCursor(0,1); lcd.print(F(" Zone:")); if (valveNumber < 10) lcd.print(F("0")); lcd.print(valveNumber); lcd.print(F(" Active")); } else { lcd.print(F(" Time Remaining ")); lcd.setCursor(0,1); unsigned long timeRemaining = (valveSoloTime[valveNumber] * 60) - ((millis() - startMillis) / 1000); lcd.print(timeRemaining / 60 < 10 ? " 0" : " "); lcd.print(timeRemaining / 60); lcd.print("min"); lcd.print(timeRemaining % 60 < 10 ? " 0" : " "); lcd.print(timeRemaining % 60); lcd.print("sec "); } break; case RUN_ALL_ZONES: // fastClear(); lcd.setCursor(0,0); if (displayToggle) { lcd.print(F(" All-Zone Mode ")); lcd.setCursor(0,1); lcd.print(F(" Zone:")); if (valveNumber < 10) lcd.print(F("0")); lcd.print(valveNumber); lcd.print(F(" Active ")); } else { lcd.print(F(" Time Remaining ")); lcd.setCursor(0,1); int timeRemaining = (allZoneTime[valveNumber] * 60) - ((millis() - startMillis) / 1000); lcd.print((timeRemaining / 60) < 10 ? " 0" : " "); lcd.print(timeRemaining / 60); lcd.print("min"); lcd.print(timeRemaining % 60 < 10 ? " 0" : " "); lcd.print(timeRemaining % 60); lcd.print("sec "); } break; case CYCLE_COMPLETE: // if (displayToggle) { lcd.setCursor(0,0); lcd.print(F(" Watering Cycle ")); lcd.setCursor(0,1); lcd.print(F(" Complete ")); } else { int totalTimeRan = 0; for (int i = 1; i < NUMBER_OF_VALVES + 1; i++) { totalTimeRan += allZoneTime[i]; } lcd.setCursor(0,0); lcd.print(F(" Total Time Run ")); lcd.setCursor(0,1); lcd.print(totalTimeRan < 10 ? " 0" : " "); lcd.print(totalTimeRan); lcd.print(" Minutes "); } } lastUpdateTime = millis(); } lastDisplayState = state; } void receiveTime(time_t newTime) { DEBUG_PRINTLN(F("Time value received and updated...")); int lastSecond = second(); int lastMinute = minute(); int lastHour = hour(); setTime(newTime); if (((second() != lastSecond) || (minute() != lastMinute) || (hour() != lastHour)) || showTime) { DEBUG_PRINTLN(F("Clock updated....")); DEBUG_PRINT(F("Sensor's time currently set to:")); DEBUG_PRINT(hourFormat12() < 10 ? F(" 0") : F(" ")); DEBUG_PRINT(hourFormat12()); DEBUG_PRINT(minute() < 10 ? F(":0") : F(":")); DEBUG_PRINT(minute()); DEBUG_PRINTLN(isAM() ? F("am") : F("pm")); DEBUG_PRINT(month()); DEBUG_PRINT(F("/")); DEBUG_PRINT(day()); DEBUG_PRINT(F("/")); DEBUG_PRINTLN(year()); DEBUG_PRINTLN(dayOfWeek[weekday()]); showTime = false; } else { DEBUG_PRINTLN(F("Sensor's time did NOT need adjustment greater than 1 second.")); } clockUpdating = false; } void fastClear() { lcd.setCursor(0,0); lcd.print(F(" ")); lcd.setCursor(0,1); lcd.print(F(" ")); } // void updateClock() { static unsigned long lastVeraGetTime; if (millis() - lastVeraGetTime >= 60000UL) // updates clock time and gets zone times from vera once every 10 minutes { DEBUG_PRINTLN(F("Requesting time and valve data from Gateway...")); lcd.setCursor(15,0); lcd.write(0); clockUpdating = true; gw.requestTime(receiveTime); lastVeraGetTime = millis(); } } // void saveDateToEEPROM(unsigned long theDate) { DEBUG_PRINTLN(F("Saving Last Run date")); if (gw.loadState(0) != 0xFF) { gw.saveState(0,0xFF); // EEPROM flag for last date saved stored in EEPROM (location zero) } // for (int i = 1; i < 5; i++) { gw.saveState(5-i, byte(theDate >> 8 * (i-1)));// store epoch datestamp in 4 bytes of EEPROM starting in location one } } // void goGetValveTimes() { static unsigned long valveUpdateTime; static byte valveIndex = 1; if (millis() - valveUpdateTime >= 300000UL / NUMBER_OF_VALVES)// update each valve once every 5 mins (distributes the traffic) { DEBUG_PRINTLN(F("Calling for Valve Times...")); lcd.setCursor(15, 0); lcd.write(1); gw.request(valveIndex, V_VAR1); gw.request(valveIndex, V_VAR2); valveUpdateTime = millis(); valveIndex++; if (valveIndex > NUMBER_OF_VALVES+1) { valveIndex = 1; } } }@BulldogLowell What valves did you use? Have you had any issues with a valve not closing all the way, thus, boom goes the dynamite.
-
@BulldogLowell What valves did you use? Have you had any issues with a valve not closing all the way, thus, boom goes the dynamite.
My valves were there already, it was controlled by simple timer. The valves are 24V like these.
Really impossible to get that kind of failure...
-
@BulldogLowell I am finally getting ready to make this. Thanks and great job as usual! I was waiting for what seemed like forever for the LCD to come in and when it did it was missing the I2C interface. DANG IT!! I think I'm going to see if I can wire it without I2C and then change it back when it comes in.
I did have one thought as I was going through the code... What do you think of the idea of requesting the run time variables from Vera just before the relay is turned on instead of every 5 minutes? That might help minimize communication on the network as well as ensure the most up to date run times are received from Vera.
-
On a plane to china, working on the Rain Gauge!!!
Thanks for the kind words, this is the one I'm most happy with of all the Mysensors I've done so far!!!
@petewill said:
What do you think of the idea of requesting the run time variables from Vera just before the relay is turned on instead of every 5 minutes?
So, I've been using it for several months, along with the rain gauge. I use PLEG to send updates to the Vera Device Files each season (remember I'm in Florida). It updates the times (longer in the winter) and number of days (twice a week during Winter, once a week Spring and Autumn, and never on during Summer, lots of rain) .
So, because I am never 'manually' updating the times, I never thought about the 5 minute update. since I started it up!!! I have from time to time updated the Single Zone times. If I need to push them, I just restart the arduino.
So, you can certainly look to update whenever you prefer, it is in a nice little function for you.
I personally don't mind at all the calls every five minutes, each call is a few milliseconds and it has the side benefit of updating the device last updated time on vera's device, so I know it's in contact with the gateway. I haven't had a traffic problem on my system yet.

I can mail you an I2C LCD if you need it, you just have to wait until I'm back from China. Email or PM me an address to send it! I'm back on the 29th. You only need to send one back when you get the chance!
-
@BulldogLowell said:
On a plane to china, working on the Rain Gauge!!!
Awesome! I should have some time this weekend to test if you have something by then.
I personally don't mind at all the calls every five minutes, each call is a few milliseconds and it has the side benefit of updating the device last updated time on vera's device, so I know it's in contact with the gateway. I haven't had a traffic problem on my system yet.
Good to know. I have around 80 MySensors devices so I'm always thinking about keeping communication to a minimum. I may just run it as is and if I notice any issues I can look into adjusting the code.
I can mail you an I2C LCD if you need it, you just have to wait until I'm back from China.
Wow, thanks for the offer! I have already ordered a replacement so hopefully it will arrive right around the time you get back. This is my first time working with LCDs so I didn't even think to look for the I2C connectivity. I just read the description and took their word for it. That will teach me...
Oh yeah... that's cool you're going to China! Are you stocking up on a whole bunch of parts while you're there? :)
-
I think waiting for the I2C piggyback will be worth the time, but you can debug with the Serial connection, I have all that in there.
I would think that retrieving the latest times for the sprinklers could be brought back to once a day even, if you are that worried about traffic.
Just curious, but how many valves will you end up controlling?
PS my factory is in the north, and I won't be getting to the south this trip. Shenzhen is really the epicenter of hobby electronics plus, it is never a good thing to bring a lot of small electronic components, wires and sensors in your bags through airport security!
-
I think waiting for the I2C piggyback will be worth the time, but you can debug with the Serial connection, I have all that in there.
I would think that retrieving the latest times for the sprinklers could be brought back to once a day even, if you are that worried about traffic.
Just curious, but how many valves will you end up controlling?
PS my factory is in the north, and I won't be getting to the south this trip. Shenzhen is really the epicenter of hobby electronics plus, it is never a good thing to bring a lot of small electronic components, wires and sensors in your bags through airport security!
@BulldogLowell said:
I think waiting for the I2C piggyback will be worth the time, but you can debug with the Serial connection, I have all that in there.
Yeah, I'm thinking I can still get it partially working this weekend (hopefully) but I wish I could see the LCD. That is such a cool feature!
I would think that retrieving the latest times for the sprinklers could be brought back to once a day even, if you are that worried about traffic.
I still need to think through the PLEG but I was hoping to do something like this...
-
If there has been rain in the past 5 days adjust the irrigation timing based on the amount.
-
Also, factor in the past high temperatures as well as the upcoming forecast.
-
If it is currently raining, stop the irrigation.
I still need to think through it a little more but calculations like that would require the variables to be updated to the irrigation controller more than once a day (at least I think). That's why I was thinking it would be cool if it pulled the values before it ran each zone. It could still update every 5 hours or so though for the heartbeat. I really like that benefit!
Just curious, but how many valves will you end up controlling?
Currently only 5 but I will probably add 2 more in the next year or so.
it is never a good thing to bring a lot of small electronic components, wires and sensors in your bags through airport security!
Haha, very true!
-
-
-
@BulldogLowell I'm getting an error when I try to compile the code (I am finally getting to the point of uploading it). There are a long string of errors but they seem to be related to this:
IrrigationController.ino:122:34: error: invalid conversion from 'int' to 't_backlighPol' [-fpermissive] In file included from IrrigationController.ino:59:0: C:\Users\petewill\Documents\Arduino\libraries\LiquidCrystal/LiquidCrystal_I2C.h:53:4: error: initializing argument 3 of 'LiquidCrystal_I2C::LiquidCrystal_I2C(uint8_t, uint8_t, t_backlighPol)' [-fpermissive] LiquidCrystal_I2C (uint8_t lcd_Addr, uint8_t backlighPin, t_backlighPol pol);Here is the Arduino code:
LiquidCrystal_I2C lcd(0x27, 16, 2); // set the LCD I2C address to 0x27 (16 characters and 2 line display)I downloaded the 1.2.1 library from here: https://bitbucket.org/fmalpartida/new-liquidcrystal/downloads
Is there something else I should be doing?
Thanks,
Pete
-
@BulldogLowell I'm getting an error when I try to compile the code (I am finally getting to the point of uploading it). There are a long string of errors but they seem to be related to this:
IrrigationController.ino:122:34: error: invalid conversion from 'int' to 't_backlighPol' [-fpermissive] In file included from IrrigationController.ino:59:0: C:\Users\petewill\Documents\Arduino\libraries\LiquidCrystal/LiquidCrystal_I2C.h:53:4: error: initializing argument 3 of 'LiquidCrystal_I2C::LiquidCrystal_I2C(uint8_t, uint8_t, t_backlighPol)' [-fpermissive] LiquidCrystal_I2C (uint8_t lcd_Addr, uint8_t backlighPin, t_backlighPol pol);Here is the Arduino code:
LiquidCrystal_I2C lcd(0x27, 16, 2); // set the LCD I2C address to 0x27 (16 characters and 2 line display)I downloaded the 1.2.1 library from here: https://bitbucket.org/fmalpartida/new-liquidcrystal/downloads
Is there something else I should be doing?
Thanks,
Pete
9:15am here in china!
can you post what you are trying to compile? it looks like a library issue (as you figured out) but I want to make suer I can compile exactly what you have.
-
@BulldogLowell 9am nice! I'm ready for bed :)
Here is the code I'm trying to compile. I think it's pretty much what you have except a couple small additions like Child ID, Sketch Name etc.
/* MySprinkler for MySensors Arduino Multi-Zone Sprinkler Control November, 2014 *** Version 2.0 *** Upgraded to http://MySensors.org version 1.4.1 *** Expanded for up to 16 Valves *** Setup for active low relay board or comment out #define ACTIVE_LOW to switch to active high *** Switch to bitshift method vs byte arrays *** Changed RUN_ALL_ZONES Vera device to 0 (was highest valve) *** Added optional LCD display featuring remaining time, date last ran & current time *** Features 'raindrop' and 'clock' icons which indicate sensor is updating valve times and clock respectively Utilizing your Vera home automation controller and the MySensors.org gateway you can control up to a sixteen zone irrigation system with only three digital pins. This sketch will create NUMBER_OF_VALVES + 1 devices on your Vera controller This sketch features the following: * Allows you to cycle through All zones (RUN_ALL_ZONES) or individual zone (RUN_SINGLE_ZONE) control. * Use the 0th controller to activate RUN_ALL_ZONES (each zone in numeric sequence 1 to n) using Variable1 as the "ON" time in minutes in each of the vera devices created. * Use the individual zone controller to activate a single zone. This feature uses Variable2 as the "ON" time for each individual device/zone. * Connect according to pinout below and uses Shift Registers as to allow the MySensors standard radio configuration and still leave available digital pins * Turning on any zone will stop the current process and begin that particular process. * Turning off any zone will stop the current process and turn off all zones. * To push your new time intervals for your zones, simply change the variable on your Vera and your arduino will call to Vera once a minute and update accordingly. * Pushbutton activation to RUN_ALL_ZONES * LED status indicator INSTRUCTIONS: * After assembling your arduino, radio, decoupling capacitors, shift register(s), status LED, pushbutton LCD (I2C connected to A4 and A5) and relays, and load the sketch. * Following the instructions at https://MySensors.org include the device to your MySensors Gateway. * Verify that each new device has a Variable1 and Variable2. Populate data accordingly with whole minutes for the RUN_ALL_ZONES routine and the RUN_SINGLE_ZONE routines. The values entered may be zero. * Once you have entered values for each zone and each variable, save the settings by pressing the red save button on your Vera. * Restart your arduino; verify the settings are loaded into your arduino with the serial monitor; the array will be printed on the serial monitor. * Your arduino should slow-flash, indicating that it is in ready mode. * There are multiple debug serial prints that can be monitored to assure that it is operating properly. * https://bitbucket.org/fmalpartida/new-liquidcrystal/downloads for the I2C library, or use yours Contributed by Jim (BulldogLowell@gmail.com) and is released to the public domain */ // #include <Wire.h> #include <Time.h> #include <MySensor.h> #include <SPI.h> #include <LiquidCrystal_I2C.h> // #define NUMBER_OF_VALVES 5 // Change this to set your valve count up to 16. #define VALVE_RESET_TIME 7500UL // Change this (in milliseconds) for the time you need your valves to hydraulically reset and change state #define RADIO_ID AUTO // AUTO // Change this to fix your Radio ID or use Auto //*Added sketch name and version #define SKETCH_NAME "MySprinkler" #define SKETCH_VERSION "2.0" // //*Added Child ID definition #define CHILD_ID_SPRINKLER 0 // #define ACTIVE_LOW // comment out this line if your relays are active high // #define DEBUG_ON // comment out to supress serial monitor output // #ifdef ACTIVE_LOW #define BITSHIFT_VALVE_NUMBER ~(1U << (valveNumber-1)) #define ALL_VALVES_OFF 0xFFFF #else #define BITSHIFT_VALVE_NUMBER (1U << (valveNumber-1)) #define ALL_VALVES_OFF 0U #endif // #ifdef DEBUG_ON #define DEBUG_PRINT(x) Serial.print(x) #define DEBUG_PRINTLN(x) Serial.println(x) #define SERIAL_START(x) Serial.begin(x) #else #define DEBUG_PRINT(x) #define DEBUG_PRINTLN(x) #define SERIAL_START(x) #endif // typedef enum { STAND_BY_ALL_OFF, RUN_SINGLE_ZONE, RUN_ALL_ZONES, CYCLE_COMPLETE} SprinklerStates; // SprinklerStates state = STAND_BY_ALL_OFF; SprinklerStates lastState; // int allZoneTime [NUMBER_OF_VALVES + 1]; int valveSoloTime [NUMBER_OF_VALVES + 1]; int valveNumber; int lastValve; unsigned long startMillis; const int ledPin = 5; boolean buttonPushed = false; boolean showTime = true; boolean clockUpdating = false; boolean recentUpdate = true; const char *dayOfWeek[] = { "Null","Sunday ","Monday ", "Tuesday ", "Wednesday ", "Thursday ", "Friday ", "Saturday "}; // time_t lastTimeRun = 0; //Setup Shift Register... const int latchPin = 8; const int clockPin = 4; const int dataPin = 7; // uint8_t clock[8] = {0x0,0xe,0x15,0x17,0x11,0xe,0x0}; // fetching time indicator uint8_t raindrop[8] = {0x4,0x4,0xA,0xA,0x11,0xE,0x0,}; // fetching Valve Data indicator // LiquidCrystal_I2C lcd(0x27, 16, 2); // set the LCD I2C address to 0x27 (16 characters and 2 line display) MySensor gw; // MyMessage msg1valve(CHILD_ID_SPRINKLER,V_LIGHT); MyMessage var1valve(CHILD_ID_SPRINKLER,V_VAR1); MyMessage var2valve(CHILD_ID_SPRINKLER,V_VAR2); // void setup() { //*Not needed //Serial.begin(115200); delay(5000); lcd.init(); lcd.clear(); lcd.backlight(); lcd.createChar(0, clock); lcd.createChar(1, raindrop); DEBUG_PRINTLN(F("Initialising...")); pinMode(latchPin, OUTPUT); pinMode(clockPin, OUTPUT); pinMode(dataPin, OUTPUT); pinMode(ledPin, OUTPUT); //*Changed for Pin 3 on the pro mini attachInterrupt(1, PushButton, CHANGE); digitalWrite (ledPin, HIGH); // //check for saved date in EEPROM DEBUG_PRINTLN(F("Checking EEPROM for stored date:")); if (gw.loadState(0) == 0xFF); // EEPROM flag { DEBUG_PRINTLN(F("Retreiving last run time from EEPROM...")); for (int i = 0; i < 4 ; i++) { lastTimeRun = lastTimeRun << 8; lastTimeRun = lastTimeRun | gw.loadState(i+1); // assemble 4 bytes into an ussigned long epoch timestamp } } gw.begin(getVariables, RADIO_ID, false); // Change 'false' to 'true' to create a Radio repeating node gw.sendSketchInfo(SKETCH_NAME, SKETCH_VERSION); for (byte i = 0; i <= NUMBER_OF_VALVES; i++) { gw.present(i, S_LIGHT); } DEBUG_PRINTLN(F("Sensor Presentation Complete")); // DEBUG_PRINTLN(F("Turning All Valves Off...")); updateRelays(ALL_VALVES_OFF); digitalWrite (ledPin, LOW); DEBUG_PRINTLN(F("Ready...")); // lcd.setCursor(0, 0); lcd.print(F(" Syncing Time ")); lcd.setCursor(15, 0); lcd.write(0); //lcd.print(0, BYTE); lcd.setCursor(0, 1); int clockCounter = 0; while(timeStatus() == timeNotSet && clockCounter < 21) { gw.process(); gw.requestTime(receiveTime); DEBUG_PRINTLN(F("Requesting time from Gateway:")); delay(1000); lcd.print("."); DEBUG_PRINT(F(".")); clockCounter++; if (clockCounter > 16) { DEBUG_PRINTLN(F("Failed initial clock synchronization!")); lcd.clear(); lcd.print(F(" Failed Clock ")); lcd.setCursor(0,1); lcd.print(F(" Syncronization ")); delay(2000); break; } } // lcd.clear(); } // void loop() { gw.process(); updateClock(); updateDisplay(); goGetValveTimes(); // if (buttonPushed) { DEBUG_PRINTLN(F("Button Pressed")); if (state != RUN_ALL_ZONES); { state = RUN_ALL_ZONES; valveNumber = 1; gw.send(msg1valve.setSensor(0).set(true), false); startMillis = millis(); for (byte i = 0; i < 5; i++) // flash lcd backlight on button press { lcd.noBacklight(); delay(25); lcd.backlight(); } delay(50); fastClear(); lcd.setCursor(0,0); lcd.print(F("*AllZone Active*")); lcd.setCursor(0,0); lcd.print(F(" Cycling Zones ")); delay(1000); DEBUG_PRINT(F("State = ")); DEBUG_PRINTLN(state); } buttonPushed = false; } if (state == STAND_BY_ALL_OFF) { slowToggleLED (); if (state != lastState) { updateRelays(ALL_VALVES_OFF); DEBUG_PRINTLN(F("State Changed... all Zones off")); for (byte i = 0; i <= NUMBER_OF_VALVES; i++) { delay(50); gw.send(msg1valve.setSensor(i).set(false), false); } lastValve = -1; } } // else if (state == RUN_ALL_ZONES) { if (lastValve != valveNumber) { for (byte i = 0; i <= NUMBER_OF_VALVES; i++) { if (i == 0 || i == valveNumber) { gw.send(msg1valve.setSensor(i).set(true), false); } else { gw.send(msg1valve.setSensor(i).set(false), false); } } } lastValve = valveNumber; fastToggleLed(); if (state != lastState) { valveNumber = 1; updateRelays(ALL_VALVES_OFF); DEBUG_PRINTLN(F("State Changed, Running All Zones...")); } unsigned long nowMillis = millis(); if (nowMillis - startMillis < VALVE_RESET_TIME) { updateRelays(ALL_VALVES_OFF); } else if (nowMillis - startMillis < (allZoneTime[valveNumber] * 60000UL)) { updateRelays(BITSHIFT_VALVE_NUMBER); } else { DEBUG_PRINTLN(F("Changing Valves...")); updateRelays(ALL_VALVES_OFF); startMillis = millis(); valveNumber++; if (valveNumber > NUMBER_OF_VALVES) { state = CYCLE_COMPLETE; startMillis = millis(); lastValve = -1; lastTimeRun = now(); saveDateToEEPROM(lastTimeRun); for (byte i = 0; i <= NUMBER_OF_VALVES; i++) { gw.send(msg1valve.setSensor(i).set(false), false); } DEBUG_PRINT(F("State = ")); DEBUG_PRINTLN(state); } } } // else if (state == RUN_SINGLE_ZONE) { fastToggleLed(); if (state != lastState) { for (byte i = 0; i <= NUMBER_OF_VALVES; i++) { if (i == 0 || i == valveNumber) { gw.send(msg1valve.setSensor(i).set(true), false); } else { gw.send(msg1valve.setSensor(i).set(false), false); } } DEBUG_PRINTLN(F("State Changed, Single Zone Running...")); DEBUG_PRINT(F("Zone: ")); DEBUG_PRINTLN(valveNumber); } unsigned long nowMillis = millis(); if (nowMillis - startMillis < VALVE_RESET_TIME) { updateRelays(ALL_VALVES_OFF); } else if (nowMillis - startMillis < (valveSoloTime [valveNumber] * 60000UL)) { updateRelays(BITSHIFT_VALVE_NUMBER); } else { updateRelays(ALL_VALVES_OFF); for (byte i = 0; i <= NUMBER_OF_VALVES; i++) { gw.send(msg1valve.setSensor(i).set(false), false); } state = CYCLE_COMPLETE; startMillis = millis(); DEBUG_PRINT(F("State = ")); DEBUG_PRINTLN(state); } lastTimeRun = now(); } else if (state == CYCLE_COMPLETE) { if (millis() - startMillis < 30000UL) { fastToggleLed(); } else { state = STAND_BY_ALL_OFF; } } lastState = state; } // void updateRelays(int value) { digitalWrite(latchPin, LOW); shiftOut(dataPin, clockPin, MSBFIRST, highByte(value)); shiftOut(dataPin, clockPin, MSBFIRST, lowByte(value)); digitalWrite(latchPin, HIGH); } // void PushButton() //interrupt with debounce { static unsigned long last_interrupt_time = 0; unsigned long interrupt_time = millis(); if (interrupt_time - last_interrupt_time > 200) { buttonPushed = true; } last_interrupt_time = interrupt_time; } // void fastToggleLed() { static unsigned long fastLedTimer; if (millis() - fastLedTimer >= 100UL) { digitalWrite(ledPin, !digitalRead(ledPin)); fastLedTimer = millis (); } } // void slowToggleLED () { static unsigned long slowLedTimer; if (millis() - slowLedTimer >= 1250UL) { digitalWrite(ledPin, !digitalRead(ledPin)); slowLedTimer = millis (); } } // void getVariables(const MyMessage &message) { boolean zoneTimeUpdate = false; if (message.isAck()) { DEBUG_PRINTLN(F("This is an ack from gateway")); } for (byte i = 0; i<= NUMBER_OF_VALVES; i++) { if (message.sensor == i) { if (message.type == V_LIGHT) { int switchState = atoi(message.data); if (switchState == 0) { state = STAND_BY_ALL_OFF; DEBUG_PRINTLN(F("Recieved Instruction to Cancel...")); } else { if (i == 0) { state = RUN_ALL_ZONES; valveNumber = 1; DEBUG_PRINTLN(F("Recieved Instruction to Run All Zones...")); } else { state = RUN_SINGLE_ZONE; valveNumber = i; DEBUG_PRINT(F("Recieved Instruction to Activate Zone: ")); DEBUG_PRINTLN(i); } } startMillis = millis(); } else if (message.type == V_VAR1) { int variable1 = atoi(message.data);// RUN_ALL_ZONES time DEBUG_PRINT(F("Recieved variable1 valve:")); DEBUG_PRINT(i); DEBUG_PRINT(F(" = ")); DEBUG_PRINTLN(variable1); if (variable1 != allZoneTime[i]) { allZoneTime[i] = variable1; zoneTimeUpdate = true; } } else if (message.type == V_VAR2) { int variable2 = atoi(message.data);// RUN_SINGLE_ZONE time DEBUG_PRINT(F("Recieved variable2 valve:")); DEBUG_PRINT(i); DEBUG_PRINT(F(" = ")); DEBUG_PRINTLN(variable2); if (variable2 != valveSoloTime[i]) { valveSoloTime[i] = variable2; zoneTimeUpdate = true; } } } } if (zoneTimeUpdate) { // DEBUG_PRINTLN(F("New Zone Times Recieved...")); for (byte i = 0; i <= NUMBER_OF_VALVES; i++) { if (i != 0) { DEBUG_PRINT(F("Zone ")); DEBUG_PRINT(i); DEBUG_PRINT(F(" individual time: ")); DEBUG_PRINT(valveSoloTime[i]); DEBUG_PRINT(F(" group time: ")); DEBUG_PRINTLN(allZoneTime[i]); recentUpdate = true; } } } else { recentUpdate = false; } } // void updateDisplay() { static unsigned long lastUpdateTime; static boolean displayToggle = false; //static byte toggleCounter = 0; static SprinklerStates lastDisplayState; if (state != lastDisplayState || millis() - lastUpdateTime >= 3000UL) { displayToggle = !displayToggle; switch (state){ case STAND_BY_ALL_OFF: // fastClear(); lcd.setCursor(0,0); if (displayToggle) { lcd.print(F(" System Ready ")); if (clockUpdating) { lcd.setCursor(15,0); lcd.write(0); } lcd.setCursor(0,1); lcd.print(hourFormat12() < 10 ? F(" ") : F("")); lcd.print(hourFormat12()); lcd.print(minute() < 10 ? F(":0") : F(":")); lcd.print(minute()); lcd.print(isAM() ? F("am") : F("pm")); lcd.print(F(" ")); lcd.print(month() < 10? F("0") : F("")); lcd.print(month()); lcd.print(day() < 10? F("/0") : F("/")); lcd.print(day()); lcd.print(F("/")); lcd.print(year()%100); } else { lcd.print(F(" Last Watered ")); if (clockUpdating) { lcd.setCursor(15,0); lcd.write(0); } lcd.setCursor(0,1); lcd.print(dayOfWeek[weekday(lastTimeRun)]); lcd.setCursor(11,1); lcd.print(month(lastTimeRun) < 10 ? F(" ") : F("")); lcd.print(month(lastTimeRun)); lcd.print(day(lastTimeRun) < 10 ? F("/0") : F("/")); lcd.print(day(lastTimeRun)); } break; case RUN_SINGLE_ZONE: // fastClear(); lcd.setCursor(0,0); if (displayToggle) { lcd.print(F("Single Zone Mode")); lcd.setCursor(0,1); lcd.print(F(" Zone:")); if (valveNumber < 10) lcd.print(F("0")); lcd.print(valveNumber); lcd.print(F(" Active")); } else { lcd.print(F(" Time Remaining ")); lcd.setCursor(0,1); unsigned long timeRemaining = (valveSoloTime[valveNumber] * 60) - ((millis() - startMillis) / 1000); lcd.print(timeRemaining / 60 < 10 ? " 0" : " "); lcd.print(timeRemaining / 60); lcd.print("min"); lcd.print(timeRemaining % 60 < 10 ? " 0" : " "); lcd.print(timeRemaining % 60); lcd.print("sec "); } break; case RUN_ALL_ZONES: // fastClear(); lcd.setCursor(0,0); if (displayToggle) { lcd.print(F(" All-Zone Mode ")); lcd.setCursor(0,1); lcd.print(F(" Zone:")); if (valveNumber < 10) lcd.print(F("0")); lcd.print(valveNumber); lcd.print(F(" Active ")); } else { lcd.print(F(" Time Remaining ")); lcd.setCursor(0,1); int timeRemaining = (allZoneTime[valveNumber] * 60) - ((millis() - startMillis) / 1000); lcd.print((timeRemaining / 60) < 10 ? " 0" : " "); lcd.print(timeRemaining / 60); lcd.print("min"); lcd.print(timeRemaining % 60 < 10 ? " 0" : " "); lcd.print(timeRemaining % 60); lcd.print("sec "); } break; case CYCLE_COMPLETE: // if (displayToggle) { lcd.setCursor(0,0); lcd.print(F(" Watering Cycle ")); lcd.setCursor(0,1); lcd.print(F(" Complete ")); } else { int totalTimeRan = 0; for (int i = 1; i < NUMBER_OF_VALVES + 1; i++) { totalTimeRan += allZoneTime[i]; } lcd.setCursor(0,0); lcd.print(F(" Total Time Run ")); lcd.setCursor(0,1); lcd.print(totalTimeRan < 10 ? " 0" : " "); lcd.print(totalTimeRan); lcd.print(" Minutes "); } } lastUpdateTime = millis(); } lastDisplayState = state; } void receiveTime(time_t newTime) { DEBUG_PRINTLN(F("Time value received and updated...")); int lastSecond = second(); int lastMinute = minute(); int lastHour = hour(); setTime(newTime); if (((second() != lastSecond) || (minute() != lastMinute) || (hour() != lastHour)) || showTime) { DEBUG_PRINTLN(F("Clock updated....")); DEBUG_PRINT(F("Sensor's time currently set to:")); DEBUG_PRINT(hourFormat12() < 10 ? F(" 0") : F(" ")); DEBUG_PRINT(hourFormat12()); DEBUG_PRINT(minute() < 10 ? F(":0") : F(":")); DEBUG_PRINT(minute()); DEBUG_PRINTLN(isAM() ? F("am") : F("pm")); DEBUG_PRINT(month()); DEBUG_PRINT(F("/")); DEBUG_PRINT(day()); DEBUG_PRINT(F("/")); DEBUG_PRINTLN(year()); DEBUG_PRINTLN(dayOfWeek[weekday()]); showTime = false; } else { DEBUG_PRINTLN(F("Sensor's time did NOT need adjustment greater than 1 second.")); } clockUpdating = false; } void fastClear() { lcd.setCursor(0,0); lcd.print(F(" ")); lcd.setCursor(0,1); lcd.print(F(" ")); } // void updateClock() { static unsigned long lastVeraGetTime; if (millis() - lastVeraGetTime >= 60000UL) // updates clock time and gets zone times from vera once every 10 minutes { DEBUG_PRINTLN(F("Requesting time and valve data from Gateway...")); lcd.setCursor(15,0); lcd.write(0); clockUpdating = true; gw.requestTime(receiveTime); lastVeraGetTime = millis(); } } // void saveDateToEEPROM(unsigned long theDate) { DEBUG_PRINTLN(F("Saving Last Run date")); if (gw.loadState(0) != 0xFF) { gw.saveState(0,0xFF); // EEPROM flag for last date saved stored in EEPROM (location zero) } // for (int i = 1; i < 5; i++) { gw.saveState(5-i, byte(theDate >> 8 * (i-1)));// store epoch datestamp in 4 bytes of EEPROM starting in location one } } // void goGetValveTimes() { static unsigned long valveUpdateTime; static byte valveIndex = 1; if (millis() - valveUpdateTime >= 300000UL / NUMBER_OF_VALVES)// update each valve once every 5 mins (distributes the traffic) { DEBUG_PRINTLN(F("Calling for Valve Times...")); lcd.setCursor(15, 0); lcd.write(1); gw.request(valveIndex, V_VAR1); gw.request(valveIndex, V_VAR2); valveUpdateTime = millis(); valveIndex++; if (valveIndex > NUMBER_OF_VALVES+1) { valveIndex = 1; } } } -
meanwhile I tried to compile my old code with the latest Arduino IDE and get the same errors...
It may take me some time, but meanwhile you can try Arduino board for I2C library for LCDs.
sorry!
-
@BulldogLowell said:
On a plane to china, working on the Rain Gauge!!!
Awesome! I should have some time this weekend to test if you have something by then.
I personally don't mind at all the calls every five minutes, each call is a few milliseconds and it has the side benefit of updating the device last updated time on vera's device, so I know it's in contact with the gateway. I haven't had a traffic problem on my system yet.
Good to know. I have around 80 MySensors devices so I'm always thinking about keeping communication to a minimum. I may just run it as is and if I notice any issues I can look into adjusting the code.
I can mail you an I2C LCD if you need it, you just have to wait until I'm back from China.
Wow, thanks for the offer! I have already ordered a replacement so hopefully it will arrive right around the time you get back. This is my first time working with LCDs so I didn't even think to look for the I2C connectivity. I just read the description and took their word for it. That will teach me...
Oh yeah... that's cool you're going to China! Are you stocking up on a whole bunch of parts while you're there? :)
@petewill said:
Good to know. I have around 80 MySensors devices so I'm always thinking about keeping communication to a minimum. I may just run it as is and if I notice any issues I can look into adjusting the code.
Holy cow, ~80 MySensor devices.. I'm surprised Vera handles all that traffic reliably... My Vera3 can't seem to handle my ~35 Z-Wave and ~ 15 MySensors devices reliably...
I'm in the starting process of moving everything over to HomeSeer. I'm really hoping this solution works better.. The little I've done so far, I'm VERY impressed. I will not miss PLEG (It's a great plug-in but difficult to build and maintain for me), as the built in event system in HS3 Pro is amazing and I've only just scratch the surface...
-
@ServiceXp said:
I'm surprised Vera handles all that traffic reliably
Depends on your definition of reliably :) I'd say 98% of the time there are no issues but the other 2% can be annoying... I am continually amazed by how well MySensors works. I just keep adding devices and it keeps performing. Henrik and team are awesome!
Vera can definitely be frustrating. The key for me is to limit the plugins. I am very careful what I install. I'm sure I'll have to switch from Vera one day (unless they start making more powerful hardware) but for now it works and it's cheap. What are you going to do with MySensors when you switch? I love MySensors too much!!