Controlling water valves with DC latching solenoids

Need help getting my project over some bumps in the road, any help appreciated.

Equipment:
Arduino UNO
4 - E18-D80NK avoidance sensors (not in use yet)
4 - DC latching solenoids (2 irritrol, 2 Orbit)
2 - L298N Motor control boards
1 - 12 volt battery

Overall objective:
Standalone battery powered box shaped object with water nozzles on 4 sides that will spray water in two different modes, random and motion sensor driven.

Detail:
I realize this would be much easier if i ran 24 v valves on AC power but the whole point of this is to be stand alone and safe from higher AC voltage, after all it is for dogs to play in! :grinning:

The DC latching solenoid valves require a pulse of 9-30 volts DC to operate so i decided on a H-bridge ssr circuit and why not use a prebuilt board ie. L298N boards.

I was having a a problem testing with valves attached so i’m going back to running with UNO attached only to make sure the coding runs correctly. I’m trying to tackle the random mode first before starting with all the input sensors.

First Question:

  1. My code seems pretty straight forward but i can’t figure why its not running correctly, after powering on i run a quick 5 second pause for someone to turn on the water if they haven’t already then it just waits for someone to push one of the buttons to start a mode, random mode in this case, once pressed i assign time stamp to each valve and proceed to turn each valve on just to check on the next cycle if the duration of the valve has been met and turn off. My problem starts after i enter random mode and assign the time stamps it doesn’t do anything after that, it’s also repeating valve 2 for some reason, i threw in some serial monitoring to try and debug and will attach for review. Can anyone help with this?

Serial output:

15:55:55.584 -> Close valves function
15:55:56.710 -> Valve 1 turned off
15:55:56.850 -> Valve 2 turned off
15:55:56.943 -> Valve 2 turned off
15:55:57.037 -> Valve 3 turned off
15:55:57.130 -> Valve 4 turned off
15:56:17.518 -> Random mode selected
15:56:17.518 -> 02807
15:56:17.611 -> Valve 1 turned on
15:56:17.704 -> Valve 2 turned on
15:56:17.750 -> 15249
15:56:17.797 -> Valve 2 turned on
15:56:17.844 -> 24073
15:56:17.890 -> Valve 3 turned on
15:56:18.030 -> Valve 4 turned on
15:56:18.030 -> 39658
15:56:47.571 -> Mode timeout
15:56:47.571 -> Close valves function
15:56:48.699 -> Valve 1 turned off
15:56:48.793 -> Valve 2 turned off
15:56:48.887 -> Valve 2 turned off
15:56:49.028 -> Valve 3 turned off
15:56:49.120 -> Valve 4 turned off

Project code is attached

code_7202020.txt (8.65 KB)

Please read the first topics telling "How to use the Forum" and "How to attach code". Look for "code tag".
No helper wants to fill up his equipment by downloading files every day.

Railroader:
Please read the first topics telling “How to use the Forum” and “How to attach code”. Look for “code tag”.
No helper wants to fill up his equipment by downloading files every day.

I did, i received the “9000 characters” error so further investigation said to attach as file.

Okey. Past midnigjt I'm using the phone and neither. txt nor. ino work. Let's wait for the next helper to step in.

Railroader:
Okey. Past midnigjt I'm using the phone and neither. txt nor. ino work. Let's wait for the next helper to step in.

I would choose sleep myself.

I found a quick code error right after i posted so i want to update things real quick:
My switch statement was looking at the wrong param (“Valve”) and should have been index (“i”)

if (millis() - ValveTimeStamps[i] >= ValveDurations[i])    // Check if valves have been on longer than duration and turn off
        {
          ValveTimeStamps[i] = millis();
          ValveDurations[i] = random(2000, 10000);    // New random time between 2 and 10 seconds.
          switch (Valve) { 
            case 1:    // Toggle on/off
              if (Valve1Status)

Corrected to:

if (millis() - ValveTimeStamps[i] >= ValveDurations[i])    // Check if valves have been on longer than duration and turn off
        {
          ValveTimeStamps[i] = millis();
          ValveDurations[i] = random(2000, 10000);    // New random time between 2 and 10 seconds.
          switch (i) { 
            case 1:    // Toggle on/off
              if (Valve1Status)

Here is what the current serial output looks like after fixing the switch statement:

18:18:55.562 -> Close valves function
18:18:56.687 -> Valve 1 turned off
18:18:56.826 -> Valve 2 turned off
18:18:56.920 -> Valve 2 turned off
18:18:57.012 -> Valve 3 turned off
18:18:57.105 -> Valve 4 turned off
18:19:07.372 -> Random mode selected
18:19:07.419 -> 02807
18:19:07.466 -> Valve 1 turned on
18:19:07.558 -> Valve 2 turned on
18:19:07.604 -> 15249
18:19:07.698 -> Valve 2 turned on
18:19:07.698 -> 24073
18:19:07.790 -> Valve 3 turned on
18:19:07.884 -> Valve 4 turned on
18:19:07.884 -> 39658
18:19:11.736 -> Valve 2 turned off
18:19:12.720 -> Valve 1 turned off
18:19:12.813 -> Valve 2 turned off
18:19:17.046 -> Valve 2 turned on
18:19:17.468 -> Valve 3 turned off
18:19:17.562 -> Valve 4 turned off
18:19:18.265 -> Valve 1 turned on
18:19:18.407 -> Valve 2 turned on
18:19:21.937 -> Valve 2 turned off
18:19:24.752 -> Valve 1 turned off
18:19:24.845 -> Valve 2 turned off
18:19:25.170 -> Valve 3 turned on
18:19:25.263 -> Valve 4 turned on
18:19:28.413 -> Valve 2 turned on
18:19:29.774 -> Valve 1 turned on
18:19:29.867 -> Valve 2 turned on
18:19:32.920 -> Valve 2 turned off
18:19:35.179 -> Valve 3 turned off
18:19:35.271 -> Valve 4 turned off
18:19:35.507 -> Valve 1 turned off
18:19:35.600 -> Valve 2 turned off
18:19:37.481 -> Valve 3 turned on
18:19:37.574 -> Valve 4 turned on
18:19:37.574 -> Mode timeout
18:19:37.621 -> Close valves function
18:19:38.750 -> Valve 1 turned off
18:19:38.844 -> Valve 2 turned off
18:19:38.936 -> Valve 2 turned off
18:19:39.029 -> Valve 3 turned off
18:19:39.124 -> Valve 4 turned off
[code]

Notice it still repeats the Valve 2 duration process when random mode is activated and again when the close valves function is ran, any ideas?

[/code]

code_7202020.txt (8.7 KB)