# To many If statements. Need it simplified

The code works but seems like the reaction time is slow. Like 4 second lag before relays trigger.
Id like to have 1/2 a second lag max.

Here is the theory of the code. What im trying to achieve.

The selector switch can be turned to selections 0 , 25 , 50 , 75 , 100

The logic is If switch is 0 than turn all relays OFF

If switch is 25 than turn all relays OFF except the 25 ones.

and same concept with the 50 , 75 , and 100

Here is the code attached as text file.

Please guide me or give me pointers on how to simplify the 100 If statements i have
so the arduino would go through the loop a lot quicker.

void loop() {
// read the state of the Selector Switch value:

// Selector Switch Logic:
// Check 0 Yard Selection:

if (SS0State == HIGH) {
// turn All Lights Off and All Cameras OFF and turn on 0 Yard ABB Ref:
digitalWrite(RC25, LOW);
digitalWrite(RC50, LOW);
digitalWrite(RC75, LOW);
digitalWrite(RC100, LOW);
digitalWrite(RL25, LOW);
digitalWrite(RL50, LOW);
digitalWrite(RL75, LOW);
digitalWrite(RL100, LOW);
digitalWrite(ABB0, HIGH);
digitalWrite(ABB25, LOW);
digitalWrite(ABB50, LOW);
digitalWrite(ABB75, LOW);
digitalWrite(ABB100, LOW);

} else {
// turn LED off:
digitalWrite(ABB0, LOW);
}

// Check 25 Yard Selection:
if (SS25State == HIGH) {
// turn 25Y Light ON and 25Y Camera ON and turn on 25 Yard ABB Ref:
digitalWrite(RC25, HIGH);
digitalWrite(RC50, LOW);
digitalWrite(RC75, LOW);
digitalWrite(RC100, LOW);
digitalWrite(RL25, HIGH);
digitalWrite(RL50, LOW);
digitalWrite(RL75, LOW);
digitalWrite(RL100, LOW);
digitalWrite(ABB0, LOW);
digitalWrite(ABB25, HIGH);
digitalWrite(ABB50, LOW);
digitalWrite(ABB75, LOW);
digitalWrite(ABB100, LOW);

} else {
// turn 25Y camera and lights off:
digitalWrite(RC25, LOW);
digitalWrite(RL25, LOW);
digitalWrite(ABB25, LOW);
}

// Check 50 Yard Selection:
if (SS50State == HIGH) {
// turn 50Y Light ON and 50Y Camera ON and turn on 50 Yard ABB Ref:
digitalWrite(RC25, LOW);
digitalWrite(RC50, HIGH);
digitalWrite(RC75, LOW);
digitalWrite(RC100, LOW);
digitalWrite(RL25, LOW);
digitalWrite(RL50, HIGH);
digitalWrite(RL75, LOW);
digitalWrite(RL100, LOW);
digitalWrite(ABB0, LOW);
digitalWrite(ABB25, LOW);
digitalWrite(ABB50, HIGH);
digitalWrite(ABB75, LOW);
digitalWrite(ABB100, LOW);

} else {
// turn 50Y camera and lights off:
digitalWrite(RC50, LOW);
digitalWrite(RL50, LOW);
digitalWrite(ABB50, LOW);
}

// Check 75 Yard Selection:
if (SS75State == HIGH) {
// turn 75Y Light ON and 75Y Camera ON and turn on 75 Yard ABB Ref:
digitalWrite(RC25, LOW);
digitalWrite(RC50, LOW);
digitalWrite(RC75, HIGH);
digitalWrite(RC100, LOW);
digitalWrite(RL25, LOW);
digitalWrite(RL50, LOW);
digitalWrite(RL75, HIGH);
digitalWrite(RL100, LOW);
digitalWrite(ABB0, LOW);
digitalWrite(ABB25, LOW);
digitalWrite(ABB50, LOW);
digitalWrite(ABB75, HIGH);
digitalWrite(ABB100, LOW);

} else {
// turn 75Y camera and lights off:
digitalWrite(RC75, LOW);
digitalWrite(RL75, LOW);
digitalWrite(ABB75, LOW);
}

// Check 100 Yard Selection:
if (SS100State == HIGH) {
// turn 100Y Light ON and 100Y Camera ON and turn on 100 Yard ABB Ref:
digitalWrite(RC25, LOW);
digitalWrite(RC50, LOW);
digitalWrite(RC75, LOW);
digitalWrite(RC100, HIGH);
digitalWrite(RL25, LOW);
digitalWrite(RL50, LOW);
digitalWrite(RL75, LOW);
digitalWrite(RL100, HIGH);
digitalWrite(ABB0, LOW);
digitalWrite(ABB25, LOW);
digitalWrite(ABB50, LOW);
digitalWrite(ABB75, LOW);
digitalWrite(ABB100, HIGH);

} else {
// turn 100Y camera and lights off:
digitalWrite(RC100, LOW);
digitalWrite(RL100, LOW);
digitalWrite(ABB100, LOW);
}

}

relay control.txt (3.57 KB)

You are probably doing something else elsewhere consuming the time - this code will only take a few hundred microseconds to run.

As you can't (if I understood well) have your switch selector at 25 and 75 at the same time your if-else structure could be improved (you fall into many else here) you could use a switch statement to make that clearer and have functions for the relays groups that will make code easier to read. You could also remember last state and not do anything at all if the state did not change.

All that is mostly cosmetic though and for the sake of good coding practice - it won't result into sizable human perceivable performance gains in your app when you move the switch

I attached the full code. which only has the additional code in-front of void loop code.

Just the setup.

Nothing really else there that could cause something.
The setup is a selector switch ( a Single Pole 5 position switch)
Than ATMega2560
And Arduino 16 relay board

relay control1.txt (5.52 KB)

I can't see anything in the code that would cause a 4 second delay.

How exactly is the switch wired ? What keeps the switch inputs LOW except when selected ?

Floating inputs?

Please remember to use code tags when posting code.

UKHeliBob:
I can't see anything in the code that would cause a 4 second delay.

How exactly is the switch wired ? What keeps the switch inputs LOW except when selected ?

And is the switch a rotary switch? And is it a type "break before make" or is it "make before break"?

Paul

The switch is a rotary switch ( type break before make)

I have 5 VDC on the Common terminal. That will trigger 5 position output terminals.

I installed 10K ohms resistors on the output pins to ground so the pins wouldn't float, but be 0V until the switch gives 5 VDC.

ardunioaak1:
The switch is a rotary switch ( type break before make)

I have 5 VDC on the Common terminal. That will trigger 5 position output terminals.

I installed 10K ohms resistors on the output pins to ground so the pins wouldn't float, but be 0V until the switch gives 5 VDC.

That seems to be ok. Would be really nice to see the COMPLETE code, properly formatted.
And you could help yourself by Serial.print(the values read from the pins). Then Serial.print the values working on in the next section. Real debugging of software is done that way.

Paul

What everyone else said. There is nothing in this code that should be causing a 4-second delay. The only likely culprit I can see is using INPUT rather than INPUT_PULLUP on your selector switch. There might be some sort of capacitance discharging. I’d be inclined to unplug all the relays and put an LED on your various outputs, just to see if the 4-second delay is still there.

``````struct Cluster {
const byte SSpin;
const byte RCpin;
const byte RLpin;
const byte ABBpin;

byte SSstate;

Cluster(
byte SSattach,
byte RCattach,
byte RLattach,
byte ABBattach
) :
SSpin(SSattach), RCpin(RCattach), RLpin(RLattach), ABBpin(ABBattach)
{}

void setup() {
pinMode(SSpin, INPUT); // not INPUT_PULLUP?
if(RCpin != 0) pinMode(RCpin, OUTPUT);
if(RLpin != 0) pinMode(RLpin, OUTPUT);
pinMode(ABBpin, OUTPUT);
}

if(RCpin != 0) digitalWrite(RCpin, SSstate);
if(RLpin != 0) digitalWrite(RLpin, SSstate);
digitalWrite(ABBpin, SSstate);
}
};

// PINOUT //////

const int CLUSTERS = 5;

Cluster cluster[CLUSTERS] = {
Cluster(2,  0,   0, 39),
Cluster(3, 31, 35, 40),
Cluster(4, 32, 36, 41),
Cluster(5, 33, 37, 42),
Cluster(6, 34, 38, 43)
};

void setup() {
for(int i = 0; i<CLUSTERS; i++) {
cluster[i].setup();
}
}

void loop() {
for(int i = 0; i<CLUSTERS; i++) {
}

}
``````

Oh, and can we please change SSpin, RLpin, RCpin, and ABBpin to selectorPin, lightPin, cameraPin and abbPin ? ‘RL’ doesn’t mean much, except maybe “right/left”.

Are you measuring the "lag" from the behavior you see from your devices connected to the relays? When you power them on or off do they react instantly or they show a similar lag?

Thank you guys for your help.

Changing the Inputs to Input_Pullup solved the issue.
It also simplified my electronics since i don't need extra external resistors.
Also with Pullup there is less chance of interference.

Now the response time is instantaneous

Thank You again