We have moved at community.getvera.com

Author Topic: Multiple Relay Irrigation Controller Arduino  (Read 21730 times)

Offline waynehead99

  • Sr. Member
  • ****
  • Posts: 296
  • Karma: +16/-1
Re: 8 Relay Board on Nano
« Reply #15 on: June 02, 2014, 06:12:56 pm »
That is clear. Thanks for that... really helps clean up what I wanted to program in pleg too with those added options.

Would this work for those of us to don't care to solder very much?

http://www.ebay.com/itm/Dual-Shift-Register-Breakout-Board-x2-74HC595-Arduino-Picaxe-Raspberry-Pi-/261486371179?pt=UK_BOI_Electrical_Components_Supplies_ET&hash=item3ce1cd616b

Offline BulldogLowell

  • Hero Member
  • *****
  • Posts: 1576
  • Karma: +191/-85
Re: 8 Relay Board on Nano
« Reply #16 on: June 02, 2014, 06:30:05 pm »
That is clear. Thanks for that... really helps clean up what I wanted to program in pleg too with those added options.

Would this work for those of us to don't care to solder very much?

http://www.ebay.com/itm/Dual-Shift-Register-Breakout-Board-x2-74HC595-Arduino-Picaxe-Raspberry-Pi-/261486371179?pt=UK_BOI_Electrical_Components_Supplies_ET&hash=item3ce1cd616b

Yes, that board will be fine.  Realize that you will only use one of the two on-board shift registers, though.

That is a very nice board because it includes the capacitors wired close to the SR, which you will want in this application.  The relays will draw some power.

I chose this way to program because I have one zone that I like to run an extra day when we are in the dry season.

Let us know how it works out.

ps

Wayne, would you mind to re-title this post something like" Multiple Relay Irrigation Controller Arduino" so that folks will find it if they search?


« Last Edit: June 02, 2014, 06:32:46 pm by BulldogLowell »

Offline waynehead99

  • Sr. Member
  • ****
  • Posts: 296
  • Karma: +16/-1
Re: 8 Relay Board on Nano
« Reply #17 on: June 02, 2014, 11:30:01 pm »
Another ask if it's possible. Is there a way to had function to the sketch so that I could add a button to my box that could send an event to trigger a cycle on the sprinklers? Even if it was to just switch a virtual switch I could trigger off of. Be nice to have a manual way to kick of the sprinklers without my phone.

Offline BulldogLowell

  • Hero Member
  • *****
  • Posts: 1576
  • Karma: +191/-85
Re: 8 Relay Board on Nano
« Reply #18 on: June 02, 2014, 11:50:16 pm »
Okay.

Good idea. Maybe a status led too

Offline waynehead99

  • Sr. Member
  • ****
  • Posts: 296
  • Karma: +16/-1
Re: Multiple Relay Irrigation Controller Arduino
« Reply #19 on: June 03, 2014, 12:06:28 am »
Thanks again. Really appreciate the help. I have updated the first post heading as well.

Offline BulldogLowell

  • Hero Member
  • *****
  • Posts: 1576
  • Karma: +191/-85
Re: Multiple Relay Irrigation Controller Arduino
« Reply #20 on: June 03, 2014, 09:29:26 am »
Thanks again. Really appreciate the help. I have updated the first post heading as well.
thanks!

I added the pushbutton activator and a status LED.  I have not tried to load onto a smaller Arduino yet, I tested on my UNO and it works.  The code got flabby so I'll have a run at that in V2.0 later on, after I get feedback and do more testing.

Let me know if you find any bugs.

I am aware that the status
Code: [Select]
gw.sendVariable( valveNumber, V_LIGHT, 0)
has a challenge getting to the VERA, but rest assured if they don't indicate properly, it is functioning OK.  I'll look into getting that working really well, too.  I have to check with the MySensors team on that.

added: You may need a capacitor and a resistor for the pushbutton.  Should be a snap to get that wired in.  diagram attached.

Code: [Select]
/*
Arduino Sprinkler Controller
 
 June 3, 2014
 
 Version 1.1    added F() macro to Serial.prints, storing text constants to flash memory
                     added pushbutton activation on interrupt 1 (pin 3) to cycle state 1
                     added LED status indicator
 Version 1.0     
 
 Arduino Multi-Zone Sprinkler Control
 
 Utilizing your Vera home automation controller and the MySensors.org gateway you can
 control up to an eight zone irrigation system with only three digital pins.  This sketch
 will create n+1 devices on your Vera controller
 
 This sketch features the following:
 
 * Allows you to cycle through All zones or individual zone control. 
 * Use the (n+1)th controller 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 below 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 times 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
 * Pushbutton activation
 * LED status indicator
 
 by Jim (BulldogLowell@gmail.com) for free public use
 
 */
#include <Relay.h>
#include <SPI.h>
#include <EEPROM.h>
#include <RF24.h>
//
#define NUMBER_OF_VALVES 8 // Change this to set you valve count.
#define RESET_TIME 5000    // Change this (in milliseconds) for the time you need your valves to change state
unsigned long valveTime [NUMBER_OF_VALVES + 1];
unsigned long valveSoloTime [NUMBER_OF_VALVES + 1];
byte valveByte [8] = { 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80 };  // Storing eight bytes
int valveNumber;
byte state = 0;
unsigned long startMillis;
const unsigned long ledInterval = 100;
const unsigned long slowLedInterval = 1250;
unsigned long ledTimer;
unsigned long slowLedTimer;
//
int latchPin = 8;
int clockPin = 4;
int dataPin  = 7;
int pin = 3;//interrupt 1
int ledPin = 5; // LED status blinks fast while changing a valve; steady during valve open or initialization; and slow blink when in stand-by
boolean buttonPushed = false;
//
Sensor gw;
//
void setup()
{
  Serial.begin(115200);
  gw.begin();
  //
  pinMode(latchPin, OUTPUT);
  pinMode(clockPin, OUTPUT);
  pinMode(dataPin, OUTPUT);
  pinMode(pin, OUTPUT);
  pinMode(ledPin, OUTPUT);
  attachInterrupt(1, PushButton, CHANGE);
  //
  digitalWrite (ledPin, HIGH);
  gw.sendSketchInfo("Sprinkler", "1.1");
  //
  for (int i=0; i<NUMBER_OF_VALVES + 1; i++) // Register all valves to gw (they will be created as child devices)
  {
    gw.sendSensorPresentation(i, S_LIGHT);
  }
  Serial.println(F("Sensor Presentation Complete"));
  //
  updateRelays(0x00);
  Serial.println(F("All Valves OFF"));
  //
  for (int i = 0; i < NUMBER_OF_VALVES + 1; i++)
  {
    gw.sendVariable( i, V_LIGHT, 0); //Display each Valve OFF
    valveTime [i] = atol(gw.getStatus( i, V_VAR1));// Get each Valve Cycle time
    valveSoloTime [i] = atol(gw.getStatus( i, V_VAR2));// Get each Valve Solo time
    Serial.print(F("Watering times collected from device: "));
    Serial.println(i);
  }
  for (int i = 0; i < NUMBER_OF_VALVES; i++)
  {
    Serial.print(F("Valve "));
    Serial.print(i);
    Serial.print(F(" cycle time="));
    Serial.println(valveTime[i]);
    Serial.print(F("Valve "));
    Serial.print(i);
    Serial.print(F(" solo  time="));
    Serial.println(valveSoloTime[i]);
  }
  digitalWrite (ledPin, LOW);
  Serial.println(F("READY"));
}
//
void loop()
{
  if (gw.messageAvailable())
  {
    message_s message = gw.getMessage();
    setValveStatus(message);
  }
  if (buttonPushed)
  {
    state = 1;
    valveNumber = 0;
    gw.sendVariable( valveNumber, V_LIGHT, 1);
    gw.sendVariable( NUMBER_OF_VALVES, V_LIGHT,1);
    startMillis = millis();
    buttonPushed = false;
    Serial.println(F("Cycling through ALL Valves"));
    Serial.print(F("state = "));
    Serial.println(state);     
  }

  if (state == 0)
  {
    unsigned long nowMillis=millis();
    if ( (nowMillis - slowLedTimer) >= slowLedInterval)
    {
      slowToggleLED ();
    }
    updateRelays(0x00);
  }//End of state 0
  if (state == 1) //Cycle throug all Valves
  {
    unsigned long nowMillis = millis();
    if (nowMillis - startMillis < RESET_TIME)
    {
      if ( (nowMillis - ledTimer) >= ledInterval)
      {
        toggleLED ();
      }
      updateRelays(0x00);
    }
    else if (nowMillis - startMillis < (valveTime[valveNumber] * 60000UL))
    {
      digitalWrite (ledPin, HIGH);
      updateRelays(valveByte [valveNumber]);
    }
    else if (nowMillis - startMillis > (valveTime [valveNumber]  * 60000UL))
    {
      updateRelays(0x00);
      startMillis = millis();
      gw.sendVariable( valveNumber, V_LIGHT, 0);
      valveNumber++;
      gw.sendVariable( valveNumber, V_LIGHT, 1);
      if (valveNumber > NUMBER_OF_VALVES)
      {
        state = 0;
        digitalWrite (ledPin, LOW);
        Serial.print(F("State = "));
        Serial.println(state);
        gw.sendVariable( NUMBER_OF_VALVES + 1, V_LIGHT, 0);
      }
    }
  }//End of State 1
  //
  if (state == 2)// Run single valve
  {
    unsigned long nowMillis = millis();
    if (nowMillis - startMillis < RESET_TIME)
    {
      if ( (nowMillis - ledTimer) >= ledInterval)
      {
        toggleLED ();
      }
      updateRelays(0x00);
    }
    else if (nowMillis - startMillis < (valveSoloTime [valveNumber] * 60000UL))
    {
      digitalWrite (ledPin, HIGH);
      updateRelays(valveByte [valveNumber]);
    }
    else if (nowMillis - startMillis > (valveSoloTime [valveNumber] * 60000UL))
    {
      updateRelays(0x00);
      ;
      state = 0;
      digitalWrite (ledPin, LOW);
      Serial.print(F("State = "));
      Serial.println(state);
      gw.sendVariable( valveNumber, V_LIGHT, 0);
    }
  }//End of State 2
}//End of loop ()
//
void setValveStatus(message_s message)
{
  if (message.header.messageType==M_SET_VARIABLE && message.header.type==V_LIGHT)
  {
    valveNumber = message.header.childId;
    int incomingRelayStatus = atoi(message.data);
    if (incomingRelayStatus != 1)
    {
      state = 0;
      Serial.println(F("All Valves Off"));
      Serial.print(F("state = "));
      Serial.println(state);
    }
    else
    {
      if (valveNumber < NUMBER_OF_VALVES)
      {
        state = 2;
        startMillis = millis();
        Serial.print(F("Cycling Valve #: "));
        Serial.println(valveNumber);
        Serial.print(F("state = "));
        Serial.println(state);
      }
      else
      {
        state = 1;
        valveNumber = 0;
        gw.sendVariable( valveNumber, V_LIGHT, 1);
        startMillis = millis();
        Serial.println(F("Cycling through ALL Valves"));
        Serial.print(F("state = "));
        Serial.println(state);
      }
    }
  }
}
//
void updateRelays(byte value)
{
  digitalWrite(latchPin, LOW);
  shiftOut(dataPin, clockPin, LSBFIRST, 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)
  {
    Serial.println(F("interrupt"));
    buttonPushed = true;
  }
  last_interrupt_time = interrupt_time;
}
//
void toggleLED ()
{
  if (digitalRead (ledPin) == LOW)
    digitalWrite (ledPin, HIGH);
  else
    digitalWrite (ledPin, LOW);
  ledTimer = millis (); 
}
//
void slowToggleLED ()
{
  if (digitalRead (ledPin) == LOW)
    digitalWrite (ledPin, HIGH);
  else
    digitalWrite (ledPin, LOW);
  slowLedTimer = millis (); 
}
« Last Edit: June 06, 2014, 12:11:17 pm by BulldogLowell »

Offline waynehead99

  • Sr. Member
  • ****
  • Posts: 296
  • Karma: +16/-1
Re: Multiple Relay Irrigation Controller Arduino
« Reply #21 on: June 03, 2014, 09:32:15 am »
Thanks. I ordered the extra parts yesterday and should receive next week. I will update once I have it together.

Offline waynehead99

  • Sr. Member
  • ****
  • Posts: 296
  • Karma: +16/-1
Re: Multiple Relay Irrigation Controller Arduino
« Reply #22 on: June 15, 2014, 11:50:43 am »
So I received my parts and put everything together. I am not sure if it's because I bought a different shift register but when I plug everything in all my relays turn on and won't turn off. I used the sketch from mysensors that you provided as well. Changing the toggles in the Vera UI has no affect either. What things could I look at here to troubleshoot.

Offline BulldogLowell

  • Hero Member
  • *****
  • Posts: 1576
  • Karma: +191/-85
Re: Multiple Relay Irrigation Controller Arduino
« Reply #23 on: June 15, 2014, 12:43:55 pm »
So I received my parts and put everything together. I am not sure if it's because I bought a different shift register but when I plug everything in all my relays turn on and won't turn off. I used the sketch from mysensors that you provided as well. Changing the toggles in the Vera UI has no affect either. What things could I look at here to troubleshoot.

please double check the wiring for the Shift Register and post which one you used.  I'll take a look.

I can write a quick debug for the Shift Register for you if you tell me the three pins you are using and what they are assigned to.  latch pin, data pin and clock pin

Sorry to see you are struggling here. it can be frustrating, I know.

EDIT:  I forgot you told me already... apologies for that!

let me know for sure you wired it correctly.  It is essentially the same register, different manufacturer.
« Last Edit: June 15, 2014, 01:30:27 pm by BulldogLowell »

Offline BulldogLowell

  • Hero Member
  • *****
  • Posts: 1576
  • Karma: +191/-85
Re: Multiple Relay Irrigation Controller Arduino
« Reply #24 on: June 15, 2014, 08:19:20 pm »
So I received my parts and put everything together. I am not sure if it's because I bought a different shift register but when I plug everything in all my relays turn on and won't turn off. I used the sketch from mysensors that you provided as well. Changing the toggles in the Vera UI has no affect either. What things could I look at here to troubleshoot.

Once you verified your wiring, try this sketch.

You should hear the relays click sequentially  and quickly left to right, and right to left (knight rider example).

increase the delay if you need, or use 8 LEDs and be sure.

Code: [Select]
int latchPin = 8;
int clockPin = 4;
int dataPin  = 7;

const byte Leds [8] = { 0x01, 0x02, 0x04 , 0x08, 0x10, 0x20, 0x40, 0x80};

void setup()
{
  Serial.begin(115200);
  pinMode(latchPin, OUTPUT);
  pinMode(dataPin, OUTPUT);
  pinMode(clockPin, OUTPUT);
}

void loop()
{
  for (int i=0; i<8; i++ )
  {
    digitalWrite(latchPin, LOW);
    shiftOut(dataPin, clockPin, LSBFIRST, Leds[i]);   
    digitalWrite(latchPin, HIGH);
    delay(100);
  }
  for (int i=0; i<8; i++ )
  {
    digitalWrite(latchPin, LOW);
    shiftOut(dataPin, clockPin, LSBFIRST, Leds[ 7-i ]);   
    digitalWrite(latchPin, HIGH);
    delay(100);
  }
}

if this works, the sketch should work too.

if not, let me know...


Offline waynehead99

  • Sr. Member
  • ****
  • Posts: 296
  • Karma: +16/-1
Re: Multiple Relay Irrigation Controller Arduino
« Reply #25 on: June 15, 2014, 10:35:58 pm »
So I have st_cp on pin 8, ds on pin 7 and sh_cp on pin 4. Trying your sketch now. (http://img.tapatalk.com/d/14/06/16/uzapara3.jpg)

Offline waynehead99

  • Sr. Member
  • ****
  • Posts: 296
  • Karma: +16/-1
Re: Multiple Relay Irrigation Controller Arduino
« Reply #26 on: June 15, 2014, 10:53:53 pm »
Just tried your sketch and it did exactly as you said it would (pretty cool looking after a couple drinks :)). So I am even more confused now as to why the other sketch isn't working. I guess at least i know my wiring is working (and soldering as I had a surprise at the mail when I saw the shift register was not put together).

Offline BulldogLowell

  • Hero Member
  • *****
  • Posts: 1576
  • Karma: +191/-85
Re: Multiple Relay Irrigation Controller Arduino
« Reply #27 on: June 15, 2014, 11:02:12 pm »
Can you run the serial monitor and let me know how the sketch reports as it progresses?


Offline waynehead99

  • Sr. Member
  • ****
  • Posts: 296
  • Karma: +16/-1
Re: Multiple Relay Irrigation Controller Arduino
« Reply #28 on: June 15, 2014, 11:21:54 pm »
The new sketch you gave me doesn't write anything for me on the serial monitor. The other sketch shows this:

Sensor Presentation Complete
All Valves OFF

But all the relays are on and don't do anything when I toggle them in vera. But I do see in the serial monitor when I toggle that it does send a command:

Watering times collected from device: 0

Now one thing about your sketch where it goes back and forth, all the relays are on and it toggles them off, not sure if it should've been the other way around.

Offline BulldogLowell

  • Hero Member
  • *****
  • Posts: 1576
  • Karma: +191/-85
Re: Multiple Relay Irrigation Controller Arduino
« Reply #29 on: June 15, 2014, 11:25:46 pm »
So when you turn on the vera 'light' button the serial monitor shows that vera sent the message to turn on the valves, right?





Sent from my iPhone using Tapatalk