How to Implement the AND Comparison/Functionality (and other logic) in Advanced Apex Programming

drblank1

Active Member
View Badges
Joined
Nov 25, 2020
Messages
271
Reaction score
140
Location
Cincinnati
Rating - 0%
0   0   0
Introduction
I’ve noticed folks asking about being able to include an “AND” statement/comparison/functionality for their advanced Apex Fusion programming but haven’t seen a lot of information on the subject. I put my electrical engineering hat on and thought long and hard about this problem. Not only did I figure out how to include AND functionality, but also OR, NAND (the opposite output of AND), NOR, (the opposite output of “OR”). You do need to use Virtual Outputs to use these comparisons.

How To:

The following are the concepts to understand the below information:

Logic States
0 = OPEN or OFF
1 = CLOSED or ON

The 0’s and 1’s below can represent the state of an apex switch input, outlet, or virtual output. I will first show the logic for the comparison and then the corresponding Apex code.

For simplicity, the Apex code below represents a virtual output comparing switch inputs.

AND Logic
Input 1 + Input 2 = Virtual Output
0 + 0 = 0
1 + 0 = 0
0 + 1 = 0
1 + 1 = 1

Set ON
If SW1 = OPEN Then OFF
If SW2 = OPEN Then OFF

OR Logic
Input 1 + Input 2 = Virtual Output
0 + 0 = 0
1 + 0 = 1
0 + 1 = 1
1 + 1 = 1

Set OFF
If SW1 = CLOSED Then ON
If SW2 = CLOSED Then ON

NAND Logic
Input 1 + Input 2 = Virtual Output
0 + 0 = 1
1 + 0 = 1
0 + 1 = 1
1 + 1 = 0

Set OFF
If SW1 = OPEN Then ON
If SW2 = OPEN Then ON

NOR Logic
Input 1 + Input 2 = Virtual Output
0 + 0 = 1
1 + 0 = 0
0 + 1 = 0
1 + 1 = 0

Set ON
If SW1 = CLOSED Then OFF
If SW2 = CLOSED Then OFF

More than 2 Comparisons
You can expand the comparisons to 3 or more items too. For instance, below is the logic and code for an 3 input AND comparison.

Logic
Input 1 + Input 2 + Input 3 = Virtual Output
0 + 0 + 0 = 0
1 + 0 + 0 = 0
0 + 1 + 0 = 0
1 + 1 + 0 = 0
0 + 0 + 1 = 0
1 + 0 + 1 = 0
0 + 1 + 1 = 0
1 + 1 + 1 = 1

Set ON
If SW1 = OPEN Then OFF
If SW2 = OPEN Then OFF
If SW3 = OPEN Then OFF

BONUS – VERY Advanced Programming (XOR)
XOR logic is a comparison state that turns ON the output if the inputs do not match. Otherwise, the output is OFF. This is a bit complicated and will take good organized/planned-out coding.

XOR Logic
Input 1 + Input 2 = Virtual Output
0 + 0 = 0
1 + 0 = 1
0 + 1 = 1
1 + 1 = 0

First create a virtual output (let’s call it voNAND) and use a NAND comparison on Input 1 + Input 2.
Then create a 2nd virtual output (let’s call in voOR) and use an OR comparison on Input 1 + Input 2.
Finally create a 3rd virtual output (voANDFinalOutput) and use an AND comparison on voNAND + voNOR.

If you follow the logic, this is how you get the XOR output.
NAND + OR = AND
1 + 0 = 0
1 + 1 = 1
1 + 1 = 1
0 + 1 = 0

Conclusion
My hope is this knowledge advances our hobby and gives us better control over our systems.

In the next few days, I will post how I use this type of programming to get 7 manual functions (i.e. turn on my LEDs to 5%, turn off the skimmer, etc…) using only 3 Apex switch inputs.

#apexusers
 
Last edited:
OP
OP
drblank1

drblank1

Active Member
View Badges
Joined
Nov 25, 2020
Messages
271
Reaction score
140
Location
Cincinnati
Rating - 0%
0   0   0
Brilliant! So you have any actual working code so I can better understand everything with some examples?
Yes, in couple of days, I am going to post similar coding that shows how I use only 3 switch inputs to create 7 different modes on the Apex.
 

AngryInch

New Member
View Badges
Joined
Nov 1, 2020
Messages
17
Reaction score
1
Location
Romeoville
Rating - 0%
0   0   0
Nice, If my salinity control ATO doesn't funtion properly, I will need to incorporate this. Coincidence that you just posted this and is what I was looking for.
 

fehenry

New Member
View Badges
Joined
Mar 19, 2014
Messages
12
Reaction score
0
Rating - 0%
0   0   0
Nice, If my salinity control ATO doesn't funtion properly, I will need to incorporate this. Coincidence that you just posted this and is what I was looking for

I’m also looking into adding salinity control to my ATO. Can you share what you’ve done? Thanks
 
OP
OP
drblank1

drblank1

Active Member
View Badges
Joined
Nov 25, 2020
Messages
271
Reaction score
140
Location
Cincinnati
Rating - 0%
0   0   0
Brilliant! So you have any actual working code so I can better understand everything with some examples?

A quick example. I have a heater tucked away in my display tank to keep temperature when the return pump is off. When doing sump maintenance, I have the display tank powerheads still running. The heater is controlled by an Apex temp probe hidden in the tank. If there is no water movement in my tank, the temp probe is unable to properly measure the water temperature. As the water doesn't move, the temperature around the probe gets colder and the heater will just stay on heating the water around the heater. I have to have at least 1 of 3 water movers running; the left powerhead, right powerhead, or the return pump. So I use "OR" logic.

Virtual Output voWtr-Moving
Set OFF
If Output RETURN-PUMP = ON Then ON
If Output WAV-LEFT = ON Then ON
If Output WAVE-RIGHT = ON Then ON

Then the code for my heater is:
If Output voWtr-Moving = OFF Then OFF

As long as 1 of my water movers is working, I can safely continue to use the heater in the display tank.
 
OP
OP
drblank1

drblank1

Active Member
View Badges
Joined
Nov 25, 2020
Messages
271
Reaction score
140
Location
Cincinnati
Rating - 0%
0   0   0
Apex switch inputs are at a premium when only 6 come on the base unit. If more are needed, a PM1 or PM2 needs to be bought.

What I found over time is its kind of a pain scrolling through the Fusion interface trying to turn something on or off. Especially things I do on a regular basis, like turn off the skimmer for cleaning, transfer water from the RODI holding tank to the saltwater mixing tank, ect… Flipping a couple of switches is much easier for those functions.

I identified 7 functions I needed to manually control, but I had only 6 Apex switch inputs. Plus, I already use an input to top-off my system, 1 for overflow protection, and 1 switch input for when my skimmate waste is full. I had 3 inputs to work with, so I leverage my electrical engineering and software programming experience to develop a solution for this dilemma.

1670521625364.png

A picture of my little switch box I made.

I connected 3 toggle switches to the 3 unused switch inputs on the Apex. If you look at the below list, there are 8 possible ON/OFF switch combinations. I created a total of 7 Virtual Output for each combination below.

I’ve set my switches/functions as follows. 1=ON/Closed, 0=OFF/Open

SW1 SW2 SW3
0 0 0 OFF/No function
1 0 0 vo1RO-PMP-ON: Turn on the RODI transfer pump
0 1 0 vo2SKIMR-OFF: Turn off the skimmer pump
1 1 0 vo3WC-2SUMP: Send made-up saltwater to the sump
0 0 1 vo4RO-2SUMP: Send RODI water to the sump
1 0 1 vo5WC-FLTR-ON: Recirculate made-up saltwater through the 5 micro filter cannister
0 1 1 vo6LED-ON-5: Turn on the LEDS to 5%
1 1 1 vo7SU-PMPS-ON: Drain the sump for water changes

If a 4th Apex input is available (which I now have because I bought a PM2 for more automation), you could have a total of 15 manual functions!

1670521709806.png

The laminated card I posted on my control board for reference.

So I use the following Fusion Advanced coding utilizing the NOR comparison (real example):
vo1RO-PMP-ON
Set ON
If SW1 OPEN Then OFF
If SW2 CLOSED Then OFF
If SW3 CLOSED Then OFF
Defer 000:02 Then ON
When On > 030:00 Then OFF

This is the code for the RODI:
If OUTPUT vo1RO-PMP-ON = ON Then ON

If any of the switches are not in their correct position, the virtual output is turned-off. In this manner, only 1 of the virtual outputs can be turned on at a time.

I use the “Defer” statement to ensure I have the switches in the correct position before any action is started. I have fat-fingered the toggle switches a couple of time and was able to correct the switch positions before anything turned on or off that I did not want.

I added the “When” statement to each virtual output after the fact because sometimes I forget to turn the switches back off. This acts as a “timeout” error and the WHEN statement turns off the virtual output and sends an email warning that I left the switches on. A real example: I like to feed my fish about 15 minutes before the lights turn off. But sometimes I am late and don't get there before my LED lights ramp off. So I need to turn the lights back on so the fish eat. I use my switches to turn the LED lights back on to 5% and feed the fish. Sometime I get distracted and forget to turn the switches back off. Instead of having he LED lights on all night at 5%, the virtual output “times out” and the lights turn off.

In the next few days, I share another way to save on Apex switch inputs.


1670521869169.png

Just for fun, my control board.
 

Looking back to your reefing roots: Did you start with Instant Ocean salt?

  • I started with Instant Ocean salt.

    Votes: 150 75.4%
  • I did not start with Instant Ocean salt, but I have used it at some point.

    Votes: 16 8.0%
  • I did not start with Instant Ocean salt and have not used it.

    Votes: 29 14.6%
  • Other.

    Votes: 4 2.0%
Back
Top