controlling (leds or valves) at random or patterns

I am building a splash pad (water play area) in my backyard and need an intelligent controller. I ordered a Sparkfun kit. I copied your code from “Re: How can I make LED's blink at random” it is just what I was looking for. However I would like to add a few elements. Maybe you can give me some direction.
• Pushbutton Switch - Start/Stop - start and stop a 15min cycle timer
• Pushbutton Switch - Event trigger – pin high for 2sec
My Components
• Arduino Ono
• Opto-Isolated 8 Channel Relay Board
• Pump Relay 24VAC – Controls pool pump
• 5 Sprinkler Valves 24VAC
• 24VAC Power Supply
• 5VDC Power Supply
My goal is to press a Start/Stop button and have a pump run for 15min and if the button is pressed while running it will stop the pump. While running have 5 sprinkler valves open and close at random, or seam random (patterns will work). While running if Event Trigger button is pressed a secondary routine will run then return to the normal routine.
When one valve is open the spay height is at its highest and lowers with each additional valve opening. This gives a dancing effect.

If it was me I would work with LEDs for my valves pump etc. until I had my program giving the correct behaviour. Then I would add the interfaces to the real equipment.

Presumably the control is going to outside so you are going to need to get some suitable waterproof enclosure for that and figure how you are going to get connections in and out of it. You seem to be running everything at 24VAC but I would still look carefully at how the water is kept out of the electrics and what limits current - there must be some standards somewhere that show how wiring should be done to electrical equipment in a pool, best to find those and follow them. Kids will also pull, bite, and stick fingers in things so bear that in mind.

Yes several good points. My enclosure is similar to what the telephone companies use. The buttons I will be using are air controlled remote switches. All electrical components will be 20 to 25’ away form the wet area. Any ways I will look into electrical requirements for pools and spas too.

Any code help? I have not done any programming since GW basic in 1992.

Anyone know if when using a 24VAC transformer, if the GFCI function will work on the 24v side or if it stops at the transformer?

if the GFCI function will work on the 24v side or if it stops at the transformer?

If the ground wire is continuous it won't stop at the transformer.

Any code help? I have not done any programming since GW basic in 1992.

Good time to learn, it is the only way to get it to do exactly what you want.
Start off by looking at the learning section of this site for a good start.

http://arduino.cc/hu/Tutorial/HomePage

The buttons I will be using are air controlled remote switches.

I was just watching a video where a (slightly mad) person had used an air pressure switch to allow him to operate some electrical equipment safely remotely. I think he said he salvaged the part from a washing machine or something similar. Anyway I thought the idea was really neat because he had a single rubber bulb/pad and by increasing the pressure on it he triggered three different actions e.g.
squeeze - action 1 happens
squeeze a bit harder - action 2 happens
squeeze harder still - action 3 happens

So one quite robust input device, well suited to a watery environment, was providing a fairly complex input.

There are lots of people here who will help with code problems if you are making an effort but get stuck.

Here are a couple of ideas to think about.

I am fairly sure that if you blow air into the leg of a Y tube it will come out of only one of the arms (the angle and diameter of the tube, along with the pressure may be important - not sure). Put your finger briefly over the hole the air is coming out of and it will start coming out the other arm. In other words the Y tube acts as a sort of pneumatic flip-flop. My suggestion is that this might be possible with water and you might be able to attach Y tubes to the arms of Y tubes. The result might be a fun game where stopping the water coming out of one hole makes it come out of another, all with no moving parts or controller apart from the water pump.

The other idea is to support a tap on a perspex pipe up its spout. Pump water up the pipe and let it flow down the outside. The tap appears to float in the air with water appearing by magic

This is called fluid logic. It only works with linear laminar air flow so blowing is right out. The junction has to be precisely machined, any old y connector will not work.

It only works with linear laminar air flow so blowing is right out

Are you sure about that? I thought I saw it demonstrated, in a film, by somebody puffing a cigarette down some off the peg laboratory glassware. I did think that the dimensions, flow rate might be important. If you are right its a real pity because kids would love putting their hands over the pipes trying to "chase" the water.

Another thing I think I saw was a ping pong ball balancing in a stream of air. I suppose I have got that wrong too :~
You would think I would be difficult but the air flow wraps round the ball and keeps it stabilised, if it starts to move off centre the flow pulls it back in. Works with air, but probably not with water unless the ball is quite dense and submerged? I think lasers are being used to manipulate small particles in a somewhat similar way?

Anyway, starting to drift way off topic.

Sounds easy to test, I will give it a try. Thanks all for the input.

Follow-up - The controller is coming together. The code if null at this point but will dig in to it next.

Any guidance on how to do a reset with code? I am trying to have the controller rest every 12 hours if left on. This will be like a hibernation mode. My modes are like this…

Filter – run pump for 15min then next mode
Run – while running Filter mode and cycle valves in various patterns
Hibernation – Wait 12hr then reset
Error - Flash Led/buzzer on error

All those states could be done with a state variable and timer code. Here’s a skeleton. You’ll have to put in all the real numbers for pins and timers and such, and add all the missing states, but it should give you an idea how the syntax can be done. And the timer events.

const byte startButtonPin = 2;    // FixMe.  Put real numbers in here
const byte eventButtonPin = 3;
const byte filterPumpPin = 4;
const byte valvePins[5] = {5, 6, 7, 8, 9};
const byte buzzerPin = 10;

enum State {Off, FilterMode, EventMode, HibernationMode};

State currentState = Off;

unsigned long filterModeTimeStamp, eventModeTimeStamp, hibernationModeTimeStamp;

unsigned long valveTimeStamps[5];         // When the sprinkler valve was turned on or off
unsigned long valveDurations[5];          // How long to leave it on or off

void setup()
{
  pinMode(startButtonPin, INPUT_PULLUP);  // Assuming it is active low.
  pinMode(eventButtonPin, INPUT_PULLUP);
  for (byte i = 0; i < 5; ++i)
    pinMode(valvePins[i], OUTPUT);
  pinMode(buzzerPin, OUTPUT);
}

void loop()
{
  if (currentState == Off)
  {
    if (digitalRead(startButtonPin) == LOW)        // Assuming the start button is active LOW
    {
      delay(50);                                   // Debounce
      while (digitalRead(startButtonPin) == LOW);  // Do nothing until they let off the button
      delay(50);
      currentState = FilterMode;
      filterModeTimeStamp = millis();
      digitalWrite(filterPumpPin, HIGH);           // Turn on the Filter Pump
      for (byte i = 0; i < 5; ++i)
      {
        valveTimeStamps[i] = millis();
        valveDurations[i] = random(5000, 60000);   // Some time between 5 and 60 seconds.
        if (random(1))                             // Start on half the time 
          digitalWrite(valvePins[i], HIGH);
      }
    }
  }
  else if (currentState == FilterMode)
  {
    if (digitalRead(startButtonPin) == LOW)        // The start button also turns us off
    {
      delay(50);
      while(digitalRead(startButtonPin) == LOW);
      delay(50);
      currentState = Off;
      for (byte i = 0; i < 5; ++i)
        digitalWrite(valvePins[i], LOW);
      digitalWrite(filterPumpPin, LOW);
    }
    else if (millis() - filterModeTimeStamp >= 15000L * 60)      // We have been in FilterMode for 15 minutes.
    {
      currentState = Off;
      for (byte i = 0; i < 5; ++i)
        digitalWrite(valvePins[i], LOW);
      digitalWrite(filterPumpPin, LOW);
    }
    else
    {
      for (byte i = 0; i < 5; ++i)
        if (millis() - valveTimeStamps[i] >= valveDurations[i])   // Check to see if any of the valves' duration is up
        {
          valveTimeStamps[i] = millis();
          valveDurations[i] = random(5000, 60000L);               // New random time
          digitalWrite(valvePins[i], !digitalRead(valvePins[i])); // Toggle off/on
        }
    }
  }
  else if (currentState == HibernationMode)  // Not sure how to get into hibernation mode.
  {
    if (millis() - hibernationModeTimeStamp >= 12 * 60 * 60000L) // 12 hours
    {
      currentState = Off;
      // Not sure what else we have to turn off.  Exactly what hibernation mode is wasn't clear.
    }
  }
}

That is awesome. Thanks for the help. With minimal effort I was able to get this to work. However I have a few clarifications/changes…

Hibernation will not be needed, returning to off state will work

I have added 2 additional valves
-change “const byte valvePins[7] = {5, 6, 7, 8, 9, 10, 11};”
-change “…byte i = 0; i < 7…” in several location
-added “pinMode(filterPumpPin, OUTPUT);”

On/Off – will be electrical switch and cuts all power (115vac power, 5vdc power supply & 24vac power supply). The system is designed to be turned off when not in use. This deleted the need for a second button.
Add water level sensor on pin 3

When turn on
-Power up Arduino
-go to filter mode and return to off
-if start button is pushed go to event mode and return to off

After 12hr in off go to filter mode

While in filter mode or event mode check water level, if low go to error mode

Filter mode runs pump for 15min

Event mode runs pump and opens valves at random for 15min

When you get it wired up and working, link us to a video. I would like to see it in action.

The idea of a pattern could be fun. Add a pin for turning on the stereo, and see if you can do some cool effects to music. Stars and Stripes Forever could be a fun one.

Bench test went well. It took some time to discover that the relays i'm using are NC on high and NO on low. So I have to make some minor changes. I have a few hours of wiring and should get a test done today. I will post a link to a video. Thinks for the code it is way simpler than what I had started on.

const byte startButtonPin = 2;    
// const byte waterLevelSensorPin = 3;    //add water level sensor
const byte filterPumpPin = 4;    
const byte valvePins[6] = {5, 6, 7, 8, 9, 10,};    //added extra valve
// const byte buzzerPin = 12;    //reserved for future use 

enum State {Off, FilterMode};

State currentState = Off;

unsigned long filterModeTimeStamp, eventModeTimeStamp;
unsigned long valveTimeStamps[6];         // When the sprinkler valve was turned on or off
unsigned long valveDurations[6];          // How long to leave it on or off

void setup()
{
  pinMode(startButtonPin, INPUT_PULLUP);  // Assuming it is active low.
  for (byte i = 0; i < 6; ++i)
  pinMode(valvePins[i], OUTPUT);
  pinMode(filterPumpPin, OUTPUT);
   for (byte i = 0; i < 6; ++i)
    digitalWrite(valvePins[i], HIGH);  // Use High to stet relay to NC
  digitalWrite(filterPumpPin, HIGH);  // Use High to stet relay to NC
  
}

void loop()
{
  if (currentState == Off)
  {
    if (digitalRead(startButtonPin) == LOW)        // Assuming the start button is active LOW
    {
      delay(50);                                   // Debounce
      while (digitalRead(startButtonPin) == LOW);  // Do nothing until they let off the button
      delay(50);
      currentState = FilterMode;
      filterModeTimeStamp = millis();
      digitalWrite(filterPumpPin, LOW);           // Turn on the Filter Pump, Use Low to set relay to NO
      for (byte i = 0; i < 6; ++i)
      {
        valveTimeStamps[i] = millis();
        valveDurations[i] = random(1000, 30000);   // Some time between 1 and 30 seconds.
        if (random(1))                             // Start on half the time 
          digitalWrite(valvePins[i], LOW);         // Open Valve, Use Low to set relay to NO
      }
    }
  }
  else if (currentState == FilterMode)
  {
    if (digitalRead(startButtonPin) == LOW)        // The start button also turns us off
    {
      delay(50);
      while(digitalRead(startButtonPin) == LOW);
      delay(50);
      currentState = Off;
      for (byte i = 0; i < 6; ++i)
        digitalWrite(valvePins[i], HIGH);  // Close valve, Use High to set relay to NC
      digitalWrite(filterPumpPin, HIGH);  // Turn off the Filter Pump, Use High to set relay to NC
    }
    else if (millis() - filterModeTimeStamp >= 15000L * 60)      // We have been in FilterMode for 15 minutes.
    {
      currentState = Off;
      for (byte i = 0; i < 6; ++i)
        digitalWrite(valvePins[i], HIGH);  // Close valve, Use High to set relay to NC
      digitalWrite(filterPumpPin, HIGH);  // Turn off the Filter Pump, Use High to set relay to NC
    }
    else
    {
      for (byte i = 0; i < 6; ++i)
        if (millis() - valveTimeStamps[i] >= valveDurations[i])   // Check to see if any of the valves' duration is up
        {
          valveTimeStamps[i] = millis();
          valveDurations[i] = random(500, 6000L);               // New random time between .5 and 60 seconds.
          digitalWrite(valvePins[i], !digitalRead(valvePins[i])); // Toggle off/on
        }
    }
  }
 }

The pluming, pump, & reservoir work as I hoped, the controller is a bit confusing. A first seemed to work but when the relays began to switch everything stopped. Push the button, starts fine after a few relays start switching it stops. The bench test works perfectly but when I apply the load I start getting issues.

I have looked at isolating the relays module with its own power supply - Same problem
-Remove the GND for Ardunio to the relay module
-Keep VCC, IN1,…IN8 connected to Ardunio
-Removed Jumper from VCC & JD-VCC and applied +5v from power supply to the VCC & -5v to GND

Did some simple code to see if I am overloading the power supplies by switching all relay a one time - OK

const byte startButtonPin = 2;    
const byte filterPumpPin = 4;    
const byte valvePins[6] = {5, 6, 7, 8, 9, 10,};    //added extra valve

void setup()
{
  for (byte i = 0; i < 6; ++i)
    pinMode(valvePins[i], OUTPUT);
  pinMode(filterPumpPin, OUTPUT);  
}

void loop()
{
  digitalWrite(filterPumpPin, LOW);           // Turn on the Filter Pump, Use Low to set relay to NO
  for (byte i = 0; i < 6; ++i)
    digitalWrite(valvePins[i], LOW);         // Open Valve, Use Low to set relay to NO
}


The pluming, pump, & reservoir work as I hoped, the controller is a bit confusing.  A first seemed to work but when the relays began to switch everything stopped.  Push the button, starts fine after a few relays start switching it stops.  The bench test works perfectly but when I apply the load I start getting issues.  
I have looked at isolating the relays module with its own power supply - Same problem    
Remove the GND for Ardunio to the relay module
Keep VCC, IN1,…IN8 connected to Ardunio
Removed Jumper from VCC & JD-VCC and applied +5v from power supply to the VCC & -5v to GND
Did some simple code to see if I am overloading the power supplies by switching all relay a one time - OK
const byte startButtonPin = 2;    
const byte filterPumpPin = 4;    
const byte valvePins[6] = {5, 6, 7, 8, 9, 10,};    //added extra valve

void setup()
{
  for (byte i = 0; i < 6; ++i)
    pinMode(valvePins[i], OUTPUT);
  pinMode(filterPumpPin, OUTPUT);  
}

void loop()
{
  digitalWrite(filterPumpPin, LOW);           // Turn on the Filter Pump, Use Low to set relay to NO
  for (byte i = 0; i < 6; ++i)
    digitalWrite(valvePins[i], LOW);         // Open Valve, Use Low to set relay to NO
}

Any ideas I’m stuck. Should the +5v from Arduno to the relay module have a resistor or even the pinout to the relay module? :roll_eyes:

The bench test works perfectly but when I apply the load I start getting issues.

Classic lack of decoupling.

http://www.thebox.myzen.co.uk/Tutorial/De-coupling.html

Thinks for the nudge. Should decoupling be applied to the 24vac power supply at the relays?

This is a bench test.

Maybe you don't need that many capacitors and I would also use some ceramic 0.1uF capacitors to remove the high frequency stuff. However, the main thing you should do is to put some diodes across those solenoids because the back EMF when they turn off can generate interference voltage spikes of several hundred volts.

Thanks again, this project is getting above my head now.

So the…
Electrolytic Capacitor 47uF 37V filters out low hz
Ceramic Capacitor 0.1uF 35V filters out high hz
Diodes block backwards voltage spikes

I am using 24VAC 40VA transformer to power the sprinkler valves. They require > 18vac additionally specs indicate the will need @24vac 8.5VA, .35Amps to open and 5.5VA, .23Amps to stay open. At a glance does it look like i have the correct size capacitors and diodes?

Thanks, any help will be appreciated.