How would you program this?

Topics specific to RUG9/RUG5 hardware
Forum rules
To help everyone get the most out of the forums and keep this a friendly place containing useful information please keep in mind the following guidelines:

-Keep discussions professional and on topic.

-If you need immediate support please email While support forums are monitored they are not the primary means of getting support. However chances are that someone else may have/had your same question.

-Any personal attacks, insults, rants, bad language or abuse of support staff or other forum users will result in deletion of such posts and immediate termination of forum access.

-Smile and have a great day!

How would you program this?

Postby SLOweather » Mon Mar 10, 2014 12:05 pm


This is a water system with 5 wells on one raw water line feeding to the treatment plant and storage tanks.

Filter plant
| | | | |
| | | | |
Well A Well B Well 4 Well 5 Well 7

The distances between the wells varies. The wells run in rotation, A, B, 4, 5, 7, rotating the lead after every shutdown on full tanks. Lead, lag,second lag, third lag etc are separated by 6" in tank level. If a well is off-line for any reason, the next well in rotation will be called after 6" more fall in tank level.

Wells A and B were the original wells on the system, with 4, 5, and 7 added later. Wells A and B require treatment at the tank site for Fe and Mn removal by KMnO4 injection. This was not a problem originally. The chemical feed equipment simply ran when the wells ran.

After connection of 4, 5, and 7, a similar scheme was programmed by the original system installer, only running KMnO4 injection when the the A or B wells ran.

However, this led to the occasional iron odor or pink water complaint. I did some thinking and calculations on the system and realized that:

If 7 was the last pump to run, and A was called lead next, the KMnO4 pump would run even though the line from A to the tanks was full of water not needing treatment. Outcome, a potential pink water complaint until the Well A water replaced the Well 7 water in the line.

Conversely, if Well B was the last to run, and Well 4 was called lead next, the line from B to the tanks was full of iron-y water that should be treated until the water from Well 4 replaced it. Potential outcome, an odor complaint.

Making this even more interesting is the fact that each well has an different pumping rate. So, for instance, if B was last to run, and 4 pumps less GPM than 5, 4 will take longer to clear the iron-y water from the line than 5 will.

I have measured the GPMs of each well, and calculated the transit time to the tanks for each one.

What I'm working on now is a scheme to know which well ran last, which one was just called, and determine:

If last to run was A or B, and current pump is A or B, run the KMnO4 pump with the pump

If last to run was A or B, and current pump is 4, 5, or 7 turn on the KMnO4 pump with the well for X minutes
where x equals the transit time from A or B at the current well's pumping rate.

If last to run was 4, 5, or 7, and current well is 4, 5, or 7, don't run the KMnO4 pump.

If last to run was 4, 5, or 7, and current well is A or B, delay KMnO4 pump turn-on for x minutes, where
x equals the transit time from A or B.

I think I have that all correct. I found a misinterpretation as I was writing that up, which I fixed. However, feel free to point out any flaws or inconsistency in the logic.

Any suggestions welcome! :)
Posts: 6
Joined: Fri Mar 07, 2014 9:22 pm

Re: How would you program this?

Postby Brady » Mon Mar 10, 2014 4:06 pm

Do you need to take into consideration running the KMnO4 pump when say, for example, B and 4 are running simultaneously?
Posts: 1
Joined: Thu Feb 27, 2014 12:01 pm

Re: How would you program this?

Postby SLOweather » Tue Mar 11, 2014 9:27 am

Yes, any time A or B are running, the chem pump should run.
Posts: 6
Joined: Fri Mar 07, 2014 9:22 pm

Re: How would you program this?

Postby Cheryl » Tue Mar 18, 2014 4:08 pm

Sorry it took so long to respond to this programming question but I've come up with a solution as I understand your problem.

First I want to state that I made the assumption that only 1 pump will be running at a time. I think the logic becomes more complicated if A or B and 4, 5, or 7 are running simultaneously because then do you need to control the dosing in relation to the ratio of water that comes from each?

Anyhow, to the program. Using the built-in modules I would go about it like this:

The first thing that comes to mind is to use a table that would keep track of the transit time for each of the 5 pumps. I wouldn’t code the transit times into the program so that they could be tweaked onsite should you find that they need to be adjusted slightly.

Detecting last & current:
First I will assume you are using the LeadLagSeq module for sequencing your pumps and rotating the lead pump. This module has pump call status outputs. By routing these status outputs to a LatchOnBitChange module, detecting on an OFF to ON transition, we can use the trigger on change output of this module to detect and trigger when a pump ON status has changed. For this specific application you can consider that there are two different well/pump groups, A&B, for which KMnO4 treatment is used and 4, 5, & 7 for which KMnO4 treatment is not used. Knowing this we will latch two statuses of which grouping was the old and which grouping is the current. The status itself is simply the output of an OR gate…. A or B and the status is 1, otherwise (4, 5, & 7) the status is 0. So using the trigger on change output of the LatchOnBitChange, we will first latch what is now the old pump group, this is simply transferring the value that is the current latch into the old latch. Using the trigger output from the LatchInt module after we have latched the new old status, we can now trigger to latch the current status.

Determining when to turn KMnO4 pump on/off:
After the last and current groups are known, determining when to turn the KMnO4 pump on and off is done by a series of logic AND/OR gates (using ladder logic is the easiest way to program this but it can be done with the AND and OR gate modules) and a timer (TriggerDelay module). For the timer we pull the time from the table. The table col selector comes by using a LookupSwitch module and the .Img output of the LatchOnBitChange module (which is the integer value of the bits currently on). By using a LookupSwitch I was able to give a column selector in the range of 1-5, vs. the .Img output of the LatchOnBitChange would give a value of 1, 2, 4, 8 or 16. Now back to the timer…The TriggerDelay module has an output called ‘Status armed’. This status turns on once the input trigger has occurred and is on while the timer is running. Once the timer has stopped running this status turns off. So using this status is the time for while the pipe is being cleared.

There are two separate phases of the pumping, while the pipe is being cleared and after the pipe has been cleared. To determine if the KMnO4 pump needs to run while the pipe is being cleared, AND the timer running status with the last group status…ie, if the last group was A or B and the timer is running the KMnO4 pump should be running to treat the water that is being cleared out of the pipe. If the timer is running and the last group was 4, 5, or 7 no need to run the KMnO4 pump because the water in the pipe doesn’t need treatment.

The second phase is handled similarly to the first… if the timer is NOT running (we’ve already cleared out the pipe)… so AND the timer NOT running with the current group status…ie, if the current group is A or B and the timer is not running, the KMnO4 pump should be running because the pipe has been cleared out and now the water coming through should be treated. If the timer is not running and the current group is 4, 5, or 7 no need to run the pump.

These two AND gates should then be OR’ed together so that either scenario can control the KMnO4 pump. Finally, because a table is being used and a column must always be selected, to make sure the KMnO4 pump only runs while a pump is running, OR together all the LeadLag pump call outputs so we know if any pump is running and then AND that with the output of your OR gates from above.

Sorry if my description for the ladder logic is confusing, so I'm attaching a .jpg which is the logic order for controlling the KMnO4 treatment.

Another way you could program this would be to write a BASIC module in which you could have a timer as well as doing logical tests with IF statements.

I'm also attaching my sample program I wrote for figuring this out. I created myself a dummy LeadLagSequencer module which I just control the pump calls by keystrokes to toggle them on/off from the MAIN display. The program also has a few string switches to easily show on the display the current status of things but that aren't necessary to the functionality of controlling the KMnO4 treatment.

I hope this helps, I apologize if my explanation is confusing, hopefully having a copy of the program helps. :)

(11.84 KiB) Downloaded 1403 times
Posts: 9
Joined: Thu Feb 27, 2014 11:38 am

Re: How would you program this?

Postby SLOweather » Mon Mar 24, 2014 9:29 am

Hi Cheryl,

Thanks for posting this detailed explanation. I'll print it out and study it.

As I think about the entire situation, I believe it's indeed immaterial whether the chem feed pump runs if more than one well is running, one with iron, one without.

First, while there is still the same absolute amount of iron in the water which the permanganate would remove, the dilution factor of the second well makes it less objectionable.

Second, in the interest of simplicity, in my original description, I left out the 6th well pump, which is connected to the system via an entirely different line from the other 5 wells. Therefore, it's not part of the problem.

Then there is the complicated rotation scheme we cooked up, and the goofy naming of the wells I inherited that make it all the more confusing.

6 wells...

A (iron)
B (iron)

and 8 on the separate line.

8 is the biggest producer, so it's programmed to run every other time. :)

I won't reproduce the entire rotation table here, jut the lead and first lag well pumps

Lead Lag
B 8
8 A
A 8
8 4
4 8
8 5
5 8
8 7
7 8
8 B

So, in reality, the only time A or B and 4, 5, or 7 would be on together, is a situation with 8 also running, and one well in second lag.

Not that it matters to the problem at hand , but the wells all shut off at 16' of water in the tanks, and turn on in half foot increments:

Lead 15.5'
1st Lag 15.0'
2nd Lag 14.5'
3rd Lag 14.0' (low water alarm)
4th Lag 13.5'
5th Lag 13.0'

During high irrigation use (hot, dry weather)we can sometimes get into a 2nd lag situation, but rarely, if ever, a 3rd lag, unless there is a break, fire flow etc.

One thing I've worked with Brady on and is in my next revision is using the FlowContainer module to give me a faster indication of a high flow situation, like a hydrant break or fire flow. The way it is now, a 1000 gpm flow could take 15 minutes to get to the low water alarm.

(120,000 gallons/16' = 7.500 gallons per foot. 2' = 15,000 gallons)

With a properly implemented FlowContainer, I could get the alarm in a couple of minutes.

But I digress...
Posts: 6
Joined: Fri Mar 07, 2014 9:22 pm

Return to RUG9/RUG5

Who is online

Users browsing this forum: No registered users and 1 guest