Help with logic to call all-but-one

Need help with programming logic.

I have 8 outputs (relays), have 8 inputs (switch), each relay has corresponding switch or trigger.
When switch 4 closed it activates every relay except 4.
Here is the tricky part: Relays default state must be Open which is digitalWrite(relay, LOW); once corresponding switch is activated Arduino has to keep corresponding relay open and all others closed.
If at the same time another switch is activated it has to keep both active switches corresponding relays open and rest closed.

switch 4 triggered :arrow_forward: relay 4 open, relay 1-3 + 5-8 closed.
15 sec later...
switch 8 triggered :arrow_forward: keep relay 4 open and relay 8 open, relays 1-7 closed
sometime later..
all switches off :arrow_forward: all relays open

I think it is important to keep relays open, once opened, otherwise if not taking that into the consideration in the loop it will just cycle these two.

so far I thought to use array but can't figure out how would I implement "not-this-one or everyone-but-this-one" logic

sketch.ino :arrow_down:

#include "relays.h"

void setup() {
  
  for( int i = 9; i < 17; i++ )  // initialize pin 9 - 16 for relays
  {
    pinMode(i, OUTPUT);
  }

}

int relay[8] = { 9, 10, 11, 12, 13, 14, 15, 16 }; // array with relays pins

void loop() {

  for( int i = 0; i < 4; i++ )      // this activates relays, CLOSE cycle
  {
    int relayNum = ( relay[i] );
    digitalWrite( relayNum, HIGH );
    delay(100);
  }

srelays.h :arrow_down: // I also could not figure out how to initiate all relays with function.

#ifndef RELAYS_H
#define RELAYS_H

//relays
const int relay1 = 9;   
const int relay2 = 10;  
const int relay3 = 11;   
const int relay4 = 12;    
const int relay5 = 13;
const int relay6 = 14;
const int relay7 = 15;
const int relay8 = 16;

#endif

you have 8 relays why do you only handle 4?

As for your question, I’m not sure I got the ask.

is contraductionary to

what does the word "triggered" mean?

  • switch opened?
    or
  • switch closed?

How are your switches connected?

  • using pullup-resistor connected to ground?
    or
  • using a pulldown-resistor connected to Vcc?

You are working on an informatic project. And what is most needed in an informatic project is precise and detailed information

imagine you have to have to go to the hospital for a surgery and the nurse would be just shouting to the doctor "make the surgery"

  • which patient?
  • which kind of surgery?
    oh cut out part of the lung

which side of the lung??

best regards Stefan

What board, sorry if I missed it?

Oh, I just copied the code while I was testing only with 4 at hand.

Once the switch#X flipped relay#X stays in LOW state while all other relays switch to HIGH.

Arduino MEGA 2560, however I am doing hand on testing with Arduino UNO 3 today-tomorrow

I envision you have something like this?

Please clarify.

How is your relay module powered?
Are you using a separate DC supply?
Can you show us a schematic diagram or picture?

The High/Low jumpers select how each relay responds its input signal. For example, High is "High Level Trigger" which means the relay is energized when the input signal is high.

It sounds like you need most of the relays energized, but if you connect to COM and NC, then the relays can be normally off. Then energizing a relay will turn off its load.

What us the purpose of the 8-relay module (what are you controlling)?

If you don’t want to remember which relay is flipped, then Switch them all first to HIGH and then the chosen one to LOW?

Just about the logic:

  1. Consider changing the pins so they are all on the same port
  2. Write a byte containing the relay values to that port as a single byte
  3. Create an array with whatever configurations of relays that you need or use logical and (&) / or(|) to set the bits
  4. Listen to @LarryD he's awesome

Exactly like this. All channels have high/low level trigger. All set to high. (energized once arduino applies power).
Relay is powered by 24v power supply. These relays power solenoids 12v, powered by separate PS. Ground tied together with 5v PS and Arduino ground. 5v supply used to power other sensors.

In the final build it is 2 of 8 relay modules. Controlling 2 way 5 position pneumatic solenoids valves which in turn move pneumatic cylinders, which move blast gate to Open or Close position for a dust collection system.

This might not be an option, I need to keep at least one gate open. They will control blast gates for dust collection system and one has to stay open for safety, it might destroy ductwork if all closed for a short period of time.

Like a few micro second ?

Anyway in that case remember the last one and handle appropriately.

That would be the black one.

I think you mean controls 24V power?
Hopefully your DC solenoids have flyback diodes installed.

I highly recommend taking advantage of the built-in opto isolators.

Thank you for pointing out, fixed it. I meant to say when switch 4 closed all relays except relay 4 are activated.

switch closed

well.. there will be few switches connected using pullup resistor connected to ground. However I am not sure yet, as I have to route them approximately 40` away and don't know if this is going to work properly. Will test with wires tomorrow.
And there will be current sensors with threshold values to activate corresponding relay.
I am trying to figure out the logic first and will plug "triggers" in it.

Yes, I meant to say relay module powered by 24v supply and solenoids by 12v. However I think to simplify things and get 12v relay modules for 12v solenoids.

Yes I installed flyback diodes, Arduino was going crazy without them, but survived.
And for extra safety I run separate V- to solenoids.

Low level trigger: digitalWrite(relayPin, LOW) will energize the relay
High level trigger: digitalWrite(relayPin, HIGH) will energize the relay

To take advantage of the built-in opto isolators, you'll need a separate supply for the Arduino. Need to see the schematic for your relay board to verify connections.

Yes, I have that option on my board. However I run wires from Arduino to relay modules ~30' and don't know how it will affect it by keeping it all energized.

However in my final design to activate Open Solenoid cycle:

digitalWrite( solenoid1, HIGH);
delay(100);
digitalWrite(solenoid1, LOW):

as it need just a momentary activation for pressurized air to move piston.

The long control wires shouldn't be an issue if you take advantage of the opto isolation. Ideally, the optos would be at the Arduino end of the control wires, but using the ones on the relay module is still helpful.

Keeping the control wires energized isn't a power issue, its only about 10mA per wire (relay).

I will do the schematic to post here.
Right now I have:
12v PS powers Arduino and solenoids with some 4-20mA-toVolt converter.
24v PS powers relay module and another relay along with Volt-to-4-20mA converter and 4-20mA-to-Volt converter.
5v PS powers LCD screens, sensors.
V- from all power supplies tied together with Arduino V-. ( this was suggested to me to have good readings from sensors)

Do you suggest to get separate power supply for solenoids and isolate V+ and V- from the system?

I will have ~30' of wire running form Arduino to relay board, they supply 5v to activate relays. I figured voltage drop with small current even on 30` is minimal (0.089%) 4.9955v at the end with 18AWG and 4.982v with 0.36% drop with 24AWG(CAT6).

would that affect the board in any way?

Marked on each relay should be:
SRD-12VDC-SL-C (for 12V supply)
not
SRD-05VDC-SL-C (for 5V supply)

No, its only the IRLED (infra-red LED) control circuit for the relay module's opto isolators.
The IRLEDs only drop about 1.2V, leaving 3.8V.