Skip to content
  • MySensors
  • OpenHardware.io
  • Categories
  • Recent
  • Tags
  • Popular
Skins
  • Light
  • Brite
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Brand Logo
  1. Home
  2. My Project
  3. Irrigation Controller (up to 16 valves with Shift Registers)

Irrigation Controller (up to 16 valves with Shift Registers)

Scheduled Pinned Locked Moved My Project
371 Posts 56 Posters 247.8k Views 52 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • petewillP Offline
    petewillP Offline
    petewill
    Admin
    wrote on last edited by
    #33

    @BulldogLowell said:

    Prints the '0' special character.

    Ok, good to know. I'll see if I can do a little searching on that. Maybe there is a different way to implement them now?? It will be hard to test without an LCD but hopefully it will come soon and I can post back :)

    Speaking of testing I was able to control my relays last night from Vera! SO COOL!! This is one of the devices I have been wanting to add for a long time. I can't wait to be able to turn on my valves from my phone when I'm turning on my sprinkler system. It sure will beat sprinting to/from the basement...

    Thanks again for the awesome work!

    My "How To" home automation video channel: https://www.youtube.com/channel/UCq_Evyh5PQALx4m4CQuxqkA

    BulldogLowellB 1 Reply Last reply
    0
    • petewillP petewill

      @BulldogLowell said:

      Prints the '0' special character.

      Ok, good to know. I'll see if I can do a little searching on that. Maybe there is a different way to implement them now?? It will be hard to test without an LCD but hopefully it will come soon and I can post back :)

      Speaking of testing I was able to control my relays last night from Vera! SO COOL!! This is one of the devices I have been wanting to add for a long time. I can't wait to be able to turn on my valves from my phone when I'm turning on my sprinkler system. It sure will beat sprinting to/from the basement...

      Thanks again for the awesome work!

      BulldogLowellB Offline
      BulldogLowellB Offline
      BulldogLowell
      Contest Winner
      wrote on last edited by
      #34

      @petewill

      I edited above to give more detail...

      1 Reply Last reply
      0
      • petewillP Offline
        petewillP Offline
        petewill
        Admin
        wrote on last edited by
        #35

        @BulldogLowell said:

        I edited above to give more detail...

        Ok, thanks. I did a little research and found this from the Arduino website:

        When referencing custom character "0", if it is not in a variable, you need to cast it as a byte, otherwise the compiler throws an error. See the example below.

        http://www.arduino.cc/en/Reference/LiquidCrystalCreateChar

        I changed the code to this:

        byte clock[8] = {0x0,0xe,0x15,0x17,0x11,0xe,0x0};
        

        and this

        lcd.write(byte(0));
        

        and now it's compiling. But, I don't know if it's working.

        I'm still working on a couple of other small things I found. I'll post the code when I get it to a point where it's ready for your review.

        My "How To" home automation video channel: https://www.youtube.com/channel/UCq_Evyh5PQALx4m4CQuxqkA

        1 Reply Last reply
        0
        • petewillP Offline
          petewillP Offline
          petewill
          Admin
          wrote on last edited by
          #36

          @BulldogLowell

          Here is the code with some changes. I have also attached a document with the changes highlighted. The most notable thing is I added some code to update the valve times when the device is first powered on. I hope that's ok.

          One thing I can't figure out, but hopefully you can, is the button is always acting like it's "pressed" when the Arduino is first powered on. This could be a problem as the power seems to go off in my house a couple of times a year (I don't want my irrigation to run when it's not supposed to). Any idea how to fix it? I've tried everything I could think of (which isn't much) and nothing worked.

          Anyway, here is the PDF with the changes Irrigation Controller Changes.pdf

          And the code:

          /*
          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
          #define SKETCH_NAME "MySprinkler"
          #define SKETCH_VERSION "2.0"
          //
          #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;
          const int waterButtonPin = 3;
          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;
          // 
          byte clock[8] = {0x0,0xe,0x15,0x17,0x11,0xe,0x0}; // fetching time indicator
          byte raindrop[8] = {0x4,0x4,0xA,0xA,0x11,0xE,0x0,}; // fetching Valve Data indicator
          LiquidCrystal_I2C lcd(0x27);  // set the LCD I2C address to 0x27
          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() 
          { 
            delay(5000);
           //*Added
           lcd.begin(16, 2); //(16 characters and 2 line display)
          //*Removed 
          // 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);
           pinMode(waterButtonPin, INPUT_PULLUP);
           attachInterrupt(1, PushButton, RISING); //May need to change for your Arduino model
           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(byte(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();
           //Update valve times when first powered on
           for(byte i = 0; i <= NUMBER_OF_VALVES; i++)
           {
             allZoneTime[i]=-1;
             valveSoloTime[i]=-1;
             int clockCounter = 0;
             while((valveSoloTime[i]==-1 || allZoneTime[i]==-1) && clockCounter < 10)
             {
               DEBUG_PRINTLN(F("Calling for Valve Times..."));
               lcd.setCursor(15, 0);
               lcd.write(byte(1)); //lcd.write(1);
               gw.request(i, V_VAR1);
               gw.request(i, V_VAR2);
               delay(1000);
               gw.process();
               clockCounter++;
               if (clockCounter > 10)
               {
                  DEBUG_PRINTLN(F("Failed initial valve synchronization!"));
                  lcd.clear();
                  lcd.print(F("  Failed Valve  "));
                  lcd.setCursor(0,1);
                  lcd.print(F(" Syncronization "));
               }
             }
           }
           
          }
          //
          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);
              }
              else
              {
                state = STAND_BY_ALL_OFF;
                //valveNumber = 1;
                gw.send(msg1valve.setSensor(0).set(false), 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 Off*"));
                lcd.setCursor(0,0);
                lcd.print(F(" Stopping  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(byte(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(byte(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 >= 3600000UL) // updates clock time and gets zone times from vera once every hour
           {
              DEBUG_PRINTLN(F("Requesting time and valve data from Gateway..."));
              lcd.setCursor(15,0);
              lcd.write(byte(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(byte(1)); //lcd.write(1);
              gw.request(valveIndex, V_VAR1);
              gw.request(valveIndex, V_VAR2);
              valveUpdateTime = millis();
              valveIndex++;
              if (valveIndex > NUMBER_OF_VALVES+1)
              {
               valveIndex = 1;
              }
           }
          }```

          My "How To" home automation video channel: https://www.youtube.com/channel/UCq_Evyh5PQALx4m4CQuxqkA

          BulldogLowellB 1 Reply Last reply
          0
          • petewillP petewill

            @BulldogLowell

            Here is the code with some changes. I have also attached a document with the changes highlighted. The most notable thing is I added some code to update the valve times when the device is first powered on. I hope that's ok.

            One thing I can't figure out, but hopefully you can, is the button is always acting like it's "pressed" when the Arduino is first powered on. This could be a problem as the power seems to go off in my house a couple of times a year (I don't want my irrigation to run when it's not supposed to). Any idea how to fix it? I've tried everything I could think of (which isn't much) and nothing worked.

            Anyway, here is the PDF with the changes Irrigation Controller Changes.pdf

            And the code:

            /*
            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
            #define SKETCH_NAME "MySprinkler"
            #define SKETCH_VERSION "2.0"
            //
            #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;
            const int waterButtonPin = 3;
            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;
            // 
            byte clock[8] = {0x0,0xe,0x15,0x17,0x11,0xe,0x0}; // fetching time indicator
            byte raindrop[8] = {0x4,0x4,0xA,0xA,0x11,0xE,0x0,}; // fetching Valve Data indicator
            LiquidCrystal_I2C lcd(0x27);  // set the LCD I2C address to 0x27
            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() 
            { 
              delay(5000);
             //*Added
             lcd.begin(16, 2); //(16 characters and 2 line display)
            //*Removed 
            // 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);
             pinMode(waterButtonPin, INPUT_PULLUP);
             attachInterrupt(1, PushButton, RISING); //May need to change for your Arduino model
             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(byte(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();
             //Update valve times when first powered on
             for(byte i = 0; i <= NUMBER_OF_VALVES; i++)
             {
               allZoneTime[i]=-1;
               valveSoloTime[i]=-1;
               int clockCounter = 0;
               while((valveSoloTime[i]==-1 || allZoneTime[i]==-1) && clockCounter < 10)
               {
                 DEBUG_PRINTLN(F("Calling for Valve Times..."));
                 lcd.setCursor(15, 0);
                 lcd.write(byte(1)); //lcd.write(1);
                 gw.request(i, V_VAR1);
                 gw.request(i, V_VAR2);
                 delay(1000);
                 gw.process();
                 clockCounter++;
                 if (clockCounter > 10)
                 {
                    DEBUG_PRINTLN(F("Failed initial valve synchronization!"));
                    lcd.clear();
                    lcd.print(F("  Failed Valve  "));
                    lcd.setCursor(0,1);
                    lcd.print(F(" Syncronization "));
                 }
               }
             }
             
            }
            //
            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);
                }
                else
                {
                  state = STAND_BY_ALL_OFF;
                  //valveNumber = 1;
                  gw.send(msg1valve.setSensor(0).set(false), 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 Off*"));
                  lcd.setCursor(0,0);
                  lcd.print(F(" Stopping  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(byte(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(byte(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 >= 3600000UL) // updates clock time and gets zone times from vera once every hour
             {
                DEBUG_PRINTLN(F("Requesting time and valve data from Gateway..."));
                lcd.setCursor(15,0);
                lcd.write(byte(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(byte(1)); //lcd.write(1);
                gw.request(valveIndex, V_VAR1);
                gw.request(valveIndex, V_VAR2);
                valveUpdateTime = millis();
                valveIndex++;
                if (valveIndex > NUMBER_OF_VALVES+1)
                {
                 valveIndex = 1;
                }
             }
            }```
            BulldogLowellB Offline
            BulldogLowellB Offline
            BulldogLowell
            Contest Winner
            wrote on last edited by
            #37

            @petewill said:

            One thing I can't figure out, but hopefully you can, is the button is always acting like it's "pressed" when the Arduino is first powered on. This could be a problem as the power seems to go off in my house a couple of times a year (I don't want my irrigation to run when it's not supposed to).

            You could try a bigger pullup resistor, maybe the pin floats for a brief moment or it is somehow energized... I've read about others having a problem like that. Alternatively, reverse the logic and pull pull the pin down to ground. I'd expect that one or both can correct that issue.

            Glad to have the improvements in the code, that's great! Always better to have another set of eyes on a project.

            1 Reply Last reply
            0
            • petewillP Offline
              petewillP Offline
              petewill
              Admin
              wrote on last edited by
              #38

              @BulldogLowell said:

              You could try a bigger pullup resistor, maybe the pin floats for a brief moment or it is somehow energized...

              Ok, I'll do some testing with that. I don't think I'll get the chance today but I should be able to do some tomorrow. It works perfectly after the first start up so hopefully a bigger resistor will help.

              My "How To" home automation video channel: https://www.youtube.com/channel/UCq_Evyh5PQALx4m4CQuxqkA

              BulldogLowellB 1 Reply Last reply
              0
              • petewillP petewill

                @BulldogLowell said:

                You could try a bigger pullup resistor, maybe the pin floats for a brief moment or it is somehow energized...

                Ok, I'll do some testing with that. I don't think I'll get the chance today but I should be able to do some tomorrow. It works perfectly after the first start up so hopefully a bigger resistor will help.

                BulldogLowellB Offline
                BulldogLowellB Offline
                BulldogLowell
                Contest Winner
                wrote on last edited by
                #39

                @petewill

                yeah, I see in the code that the pushbutton is using interrupt pin, but still needs to be set to input

                pinMode(PushButton, INPUT_PULLUP);
                

                start there, sorry!

                1 Reply Last reply
                0
                • petewillP Offline
                  petewillP Offline
                  petewill
                  Admin
                  wrote on last edited by
                  #40

                  @BulldogLowell said:

                  yeah, I see in the code that the pushbutton is using interrupt pin, but still needs to be set to input

                  I thought I added it already with this:

                  pinMode(waterButtonPin, INPUT_PULLUP);
                  
                  

                  Is that correct? Maybe that's my problem...

                  My "How To" home automation video channel: https://www.youtube.com/channel/UCq_Evyh5PQALx4m4CQuxqkA

                  1 Reply Last reply
                  0
                  • petewillP Offline
                    petewillP Offline
                    petewill
                    Admin
                    wrote on last edited by
                    #41

                    @BulldogLowell said:

                    were you not getting a compile error? on the PushButton variable being undeclared?

                    Ok, I still think I have some things to learn about interrupts. I thought PushButton was the method being called when the interrupt was triggered. I didn't think it was the pin. I thought the pin was the first item 1 in this case. But, I thought 1 isn't even the pin. It's the interrupt number which is pin 3 in the case of a Pro Mini. I also thought I needed to declare the pinMode even if it's already in the interrupt. So confusing... Am I totally off?

                    My "How To" home automation video channel: https://www.youtube.com/channel/UCq_Evyh5PQALx4m4CQuxqkA

                    BulldogLowellB 1 Reply Last reply
                    0
                    • petewillP petewill

                      @BulldogLowell said:

                      were you not getting a compile error? on the PushButton variable being undeclared?

                      Ok, I still think I have some things to learn about interrupts. I thought PushButton was the method being called when the interrupt was triggered. I didn't think it was the pin. I thought the pin was the first item 1 in this case. But, I thought 1 isn't even the pin. It's the interrupt number which is pin 3 in the case of a Pro Mini. I also thought I needed to declare the pinMode even if it's already in the interrupt. So confusing... Am I totally off?

                      BulldogLowellB Offline
                      BulldogLowellB Offline
                      BulldogLowell
                      Contest Winner
                      wrote on last edited by
                      #42

                      @petewill

                      post deleted, my bad! your understanding is 100% correct!

                      your PushButton is the ISR, so you are right

                      Try the extra pullup or pull-down

                      really sorry for confusing you! I'm mega jet lagged

                      1 Reply Last reply
                      0
                      • petewillP Offline
                        petewillP Offline
                        petewill
                        Admin
                        wrote on last edited by
                        #43

                        @BulldogLowell said:

                        I'm mega jet lagged

                        Totally understandable :)

                        Ok, I'll try the other options discussed above and post back. Thanks.

                        My "How To" home automation video channel: https://www.youtube.com/channel/UCq_Evyh5PQALx4m4CQuxqkA

                        1 Reply Last reply
                        0
                        • petewillP Offline
                          petewillP Offline
                          petewill
                          Admin
                          wrote on last edited by
                          #44

                          @BulldogLowell said:

                          Try the extra pullup

                          Well, I had almost no time to work on MySensors stuff this weekend. :( I did do a quick test last night and it seems the external pullup worked! I power cycled the node 3 times and each time it worked flawlessly. I'll continue to test but so far so good. Thank you!

                          My "How To" home automation video channel: https://www.youtube.com/channel/UCq_Evyh5PQALx4m4CQuxqkA

                          BulldogLowellB 1 Reply Last reply
                          0
                          • petewillP petewill

                            @BulldogLowell said:

                            Try the extra pullup

                            Well, I had almost no time to work on MySensors stuff this weekend. :( I did do a quick test last night and it seems the external pullup worked! I power cycled the node 3 times and each time it worked flawlessly. I'll continue to test but so far so good. Thank you!

                            BulldogLowellB Offline
                            BulldogLowellB Offline
                            BulldogLowell
                            Contest Winner
                            wrote on last edited by
                            #45

                            @petewill

                            glad to hear that worked!

                            :thumbsup:

                            1 Reply Last reply
                            0
                            • BulldogLowellB BulldogLowell

                              I put together an extension of the multi-Relay controller for use as a controller for your irrigation project if you have more zones than available pins on your Arduino.

                              This sketch features the following:

                              • Allows you to cycle through All zones or individual zone control.
                              • Use the (n+1)th device to activate each zone in numeric sequence (zero 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 in the sketch and uses an 74HC595 (or equiv) Shift Register as to
                                allow the MySensors standard radio configuration and still leave available digital pins
                              • Compiles to ~12,000 Bytes, so will run on any Arduino
                              • 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.
                              • Sketch must collect your desired intervals so it takes several minutes to startup.
                              • If you change your desired time intervals for your zones, simply restart your arduino
                                and it will self update to reflect those changes.

                              Example, I am using with 8 relays:

                              This will create 9 devices. Zero through 7 are the individual relays. Eight is the Sequencer, so to speak (refer to attachment).

                              Once you create this and add it using the gateway, go to each of zero through 7 and edit Variable1 and Variable2 for what time you want to use for the Sequencer or Zone respectively. Then save the settings. Then, restart your arduino; your arduino will extract these settings and save them to an array.

                              When you turn on device 8 (aka the Sequencer) the relays will actuate in order from zero to seven, each one staying on for the period entered in the Variable1 field. There is a 5 second delay at the start of a new zone to allow for the valves to hydraulically reset.

                              When you turn on any of devices zero through 7, it will run that zone only for the period of time entered in Variable2.

                              Selecting any new zone (0-8) will stop the current process and start as per above.

                              Hope you have a use for it. If you see any opportunity to improve, or find a bug, let me know.

                              Jim
                              modified. Attached wrong file, whoops!

                              Sprinkler.ino

                              SparkmanS Offline
                              SparkmanS Offline
                              Sparkman
                              Hero Member
                              wrote on last edited by Sparkman
                              #46

                              @BulldogLowell Thanks for posting this. A sprinkler controller to replace my Rainbird controller is next on my list of projects.

                              For those looking for a good and relatively cheap soil moisture sensor, these ones are pretty good and use a capacitive sensor: http://www.ebay.com/itm/261675851824. They transmit on 433 MHz. I currently use them with RFXCom and HomeSeer, but for those that don't have RFXCom, the protocol should be relatively easy to reverse engineer and then use directly with MySensors and a 433MHz receiver. I've also bought a few from here: https://www.plantcaretools.com/en/webshop/wireless-moisture-sensor-en-detail. Both sellers were good to deal with. The only drawback to them is that the antenna (and therefore range) is not very good, but there is an easy mod to improve that: http://www.domoticz.com/forum/viewtopic.php?f=13&t=2712

                              Cheers
                              Al

                              DrJeffD 1 Reply Last reply
                              1
                              • hekH Offline
                                hekH Offline
                                hek
                                Admin
                                wrote on last edited by hek
                                #47

                                Added your project to the main site togeter with @petewill excellent new video.

                                http://www.mysensors.org/build/irrigation

                                https://youtu.be/l4GPRTsuHkI

                                BulldogLowellB C 2 Replies Last reply
                                2
                                • hekH hek

                                  Added your project to the main site togeter with @petewill excellent new video.

                                  http://www.mysensors.org/build/irrigation

                                  https://youtu.be/l4GPRTsuHkI

                                  BulldogLowellB Offline
                                  BulldogLowellB Offline
                                  BulldogLowell
                                  Contest Winner
                                  wrote on last edited by BulldogLowell
                                  #48

                                  Cool @hek!

                                  Well done @petewill So easy for folks to follow along your outstanding video!

                                  MySensors Community Rocks!

                                  DrJeffD 1 Reply Last reply
                                  0
                                  • petewillP Offline
                                    petewillP Offline
                                    petewill
                                    Admin
                                    wrote on last edited by
                                    #49

                                    @BulldogLowell said:

                                    MySensors Community Rocks!

                                    Agreed!!! Thanks to both @BulldogLowell and @hek for making this possible. I love seeing it hanging on my wall every time I go into the basement :)

                                    My "How To" home automation video channel: https://www.youtube.com/channel/UCq_Evyh5PQALx4m4CQuxqkA

                                    1 Reply Last reply
                                    0
                                    • RJ_MakeR Offline
                                      RJ_MakeR Offline
                                      RJ_Make
                                      Hero Member
                                      wrote on last edited by
                                      #50

                                      Great Work Guys!!

                                      RJ_Make

                                      1 Reply Last reply
                                      0
                                      • SparkmanS Sparkman

                                        @BulldogLowell Thanks for posting this. A sprinkler controller to replace my Rainbird controller is next on my list of projects.

                                        For those looking for a good and relatively cheap soil moisture sensor, these ones are pretty good and use a capacitive sensor: http://www.ebay.com/itm/261675851824. They transmit on 433 MHz. I currently use them with RFXCom and HomeSeer, but for those that don't have RFXCom, the protocol should be relatively easy to reverse engineer and then use directly with MySensors and a 433MHz receiver. I've also bought a few from here: https://www.plantcaretools.com/en/webshop/wireless-moisture-sensor-en-detail. Both sellers were good to deal with. The only drawback to them is that the antenna (and therefore range) is not very good, but there is an easy mod to improve that: http://www.domoticz.com/forum/viewtopic.php?f=13&t=2712

                                        Cheers
                                        Al

                                        DrJeffD Offline
                                        DrJeffD Offline
                                        DrJeff
                                        wrote on last edited by
                                        #51

                                        @Sparkman I like the sensors, I just built some and was dbating the way to make smaller, what kind of battery life do those moisture sensors have?

                                        @petewill Time to change my current 16 valve arduino sprinklers to a little smarter version like yours! I love this community.
                                        You have been coming on with some awesome videos and contributions!

                                        petewillP SparkmanS 2 Replies Last reply
                                        0
                                        • DrJeffD DrJeff

                                          @Sparkman I like the sensors, I just built some and was dbating the way to make smaller, what kind of battery life do those moisture sensors have?

                                          @petewill Time to change my current 16 valve arduino sprinklers to a little smarter version like yours! I love this community.
                                          You have been coming on with some awesome videos and contributions!

                                          petewillP Offline
                                          petewillP Offline
                                          petewill
                                          Admin
                                          wrote on last edited by
                                          #52

                                          @DrJeff I agree this is a great community! So much fun!

                                          Thanks for the kind words! All credit for this project goes to @BulldogLowell though. I just made the video :)

                                          My "How To" home automation video channel: https://www.youtube.com/channel/UCq_Evyh5PQALx4m4CQuxqkA

                                          1 Reply Last reply
                                          0
                                          Reply
                                          • Reply as topic
                                          Log in to reply
                                          • Oldest to Newest
                                          • Newest to Oldest
                                          • Most Votes


                                          15

                                          Online

                                          11.7k

                                          Users

                                          11.2k

                                          Topics

                                          113.0k

                                          Posts


                                          Copyright 2019 TBD   |   Forum Guidelines   |   Privacy Policy   |   Terms of Service
                                          • Login

                                          • Don't have an account? Register

                                          • Login or register to search.
                                          • First post
                                            Last post
                                          0
                                          • MySensors
                                          • OpenHardware.io
                                          • Categories
                                          • Recent
                                          • Tags
                                          • Popular