1 button 2 relays

Hi all. I need help. I have 2 relays. It should work from one button. When the button is pressed for the first time, relay # 1 should work and turn off after 2 seconds. When pressed again, relay # 2 should work and turn off after 2 seconds. If relay # 1 is working, relay # 2 should not work in the same way when the opposite is true. Thanks everyone for the help and sorry for my English. :sweat_smile:

I was just given access to help with some of the administration of the site & noticed this post blocked by the spam filter. It was posted a couple months ago, but is only showing up now. Sorry for the delay @aset.

Point 1: If you want the relay ON for 2 seconds after button is pressed, you do not want the ‘not’ node between delay and relay. You want the relay on while the delay is ACT. If you want the relay OFF for 2 seconds when button is pushed, then your code is correct.

Point 2: You do not want the button to do anything while either delay is active. You can do this by placing a ‘gate’ node between the button and delay nodes. If either delay is active, then you want to close the gate. This means you want to ‘or’ the 2 delay-ACT outputs, then negate it using ‘not’. To use this output to control the gate above delay nodes, you have to run a wire “up” the patch. This always requires a ‘defer’ node to help XOD resolve loops. This produces something like this:

Point 3: You want to switch between the two relays with each button press. Since there are only two relays, one option is to use a ‘branch’ node. This allows you to send the signal for button press in one of two directions. If branch-GATE is true, the signal comes out the T pin; otherwise it will come out the F pin. The branch node will go between the gate node and the relays. To control branch-GATE, we need to remember which relay we should activate next. Again, there are only 2 options, so a flip-flop can be used to remember. If it is important which relay is activated on 1st button press, you will need to know that flip-flop will default to 0/False, so the relay that should activate first should be tied to branch-F. This little bit will look something like this:
You still need to control the flip-flop, but that is actually pretty easy. When the first relay activates (delay-ACT), you want to make the flip-flop True (flip-flop-SET). When the 2nd relay activates, you want to make the flip-flop False (flip-flop-RST). These links need to go “up” the patch, so you will need defer nodes again.

Having a bunch of wires going “up” the patch can quickly get messy. One way to clean them up is to use buses. This is not a reference public transportation, but an electrical term. A “bus bar” in electrical is a metal bar that allows lots of connections to it. In XOD, a bus allows you to have “invisible” wires so they don’t clutter your program. Each bus can have one input (to-bus) and any number of outputs (from-bus). The label you give the to-bus and from-bus determines how they connect. Any bus nodes with the same label are connected together. Since I need relay1 and relay2 status looped back to the top of the program, I’ll use bus labels R1 and R2.

Now I can put this all together into one program. Since looping back to the top always needs a defer node, I’m going to take delay-ACT to a defer node, then to a to-bus; otherwise I will need more defer nodes at the top of the program.

If you don’t like the wire hidden by the ‘or’ node, you can use multiple from-bus nodes with the same label to clean it up:

This actually turns on R2 1st. If you need the reverse, you can swap the port #s on the relays, or put a ‘not’ between ‘flip-flop’ and ‘branch’.

To review: Pressing the button will send a signal to ‘gate’. If either relay is active, the signal is blocked and nothing happens (but if you hold the button down until the relay is no longer active, the button press will pass through the gate as soon as relay turns off). Once a signal gets through ‘gate’, ‘branch’ will determine if it goes to relay1 or relay2 based on value of flip-flop. When R1 goes true, it resets the flip-flop and disables the gate until delay timer ends. When R2 goes true, it sets the flip-flop and also disables the gate until the delay timer ends.

Note that if you add a ‘not’ between flip-flop and branch, you will need to swap which relay goes to SET & RST; otherwise you will always go to the same relay instead of switching between them. It is important to tie the branch-F relay to the flip-flop pin that will change the branch-GATE value to True. You could avoid this by using the flip-flop-TGL pin tied to the output of the ‘or’ node to toggle the flip-flop every time a relay goes active. This changes the top-right part of the program to look like this:
Normally, I discourage using TGL instead of explicit SET/RST because your flip-flop can get out-of-sync with “reality”, but in this case that is not really an issue, and it simplifies your program.

Now you only need the output of ‘or’ at the top of the program to know when a relay is active instead of both relay signals, so you could rearrange your code to something like this:

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.