Hi, I am using an Arduino Mega to control 4 relays (on a shield) with 7 inputs. The inputs are controlled by breaking a 5V line with 24V relays that i swithed also externally by limit switches.
The problem that I am having is once I connect power to the system the third 5V relay on the relay shield instantly comes on (switches), and when I simulate the inputs all four relays flickeron and off.
Here is the code that I used:
int Relay1 = 25;
int Relay2 = 27;
int Relay3 = 29;
int Relay4 = 31;
int Pot1 = A0;
int Pot2 = A1;
int Sig1 = 48;
int Sig2 = 46;
int Sig3 = 44;
int Sig4 = 42;
int Sig5 = 40;
int Sig6 = 38;
int Sig7 = 36;
int val1 = 0;
int val2 = 0;
int val3 = 0;
int val4 = 0;
int val5 = 0;
int val6 = 0;
int val7 = 0;
int Pot1val = 0;
int Pot2val = 0;
void setup() {
pinMode(Relay1, OUTPUT);
pinMode(Relay2, OUTPUT);
pinMode(Relay3, OUTPUT);
pinMode(Relay4, OUTPUT);
pinMode(Sig1, INPUT);
pinMode(Sig2, INPUT);
pinMode(Sig4, INPUT);
pinMode(Sig5, INPUT);
pinMode(Sig6, INPUT);
pinMode(Sig7, INPUT);
pinMode(HDL1, INPUT);
pinMode(FDL2, INPUT);
}
void loop() {
val1 = digitalRead(Sig1);
val2 = digitalRead(Sig2);
val4 = digitalRead(Sig4);
val5 = digitalRead(Sig5);
val6 = digitalRead(Sig6);
val7 = digitalRead(Sig7);
Pot1val = analogRead(Pot1);
Pot2val = analogRead(Pot2);
if ((val1 == HIGH) && (val6 == HIGH) && (val2 == LOW)) {
digitalWrite(Relay1, HIGH);
}
delay(10);
if ((val1 == HIGH) && (val6 == HIGH) && (val2 == HIGH)) {
digitalWrite(Relay2, HIGH);
}
delay(10);
if ((val1 == HIGH) && (val5 == HIGH) && (val2 == HIGH)) {
digitalWrite(Relay2, LOW);
delay(Pot1val * 30);
digitalWrite(Relay3, HIGH);
}
delay(10);
if ((val1 == HIGH) && (val6 == HIGH) && (val2 == HIGH)) {
digitalWrite(Relay3, LOW);
digitalWrite(Relay1, LOW);
}
delay(10);
if ((val1 == HIGH) && (val6 == HIGH) && (val2 == LOW)) {
digitalWrite(Relay2, HIGH);
}
delay(10);
if ((val1 == HIGH) && (val4 == HIGH) && (val2 == LOW)) {
delay(Pot2val * 30);
digitalWrite(Relay4, HIGH);
} else if (val7 == HIGH) {
digitalWrite(Relay4, LOW);
}
delay(10);
}
I am attaching a drawing that I made of the circuit, unfortunately I don't know how to insert it in the post sorry.
Please let me know if I need to supply any additional information.
you have information available in the serial monitor about what goes into the Arduinos decision making process. you may have noticed that almost every sketch you see has:
Serial.begin(115200); in void setup()
that sets up the serial monitor. the number in the middle box at the bottom of the serial monitor must match the number in the parentheses. you can adjust either, but they must match
Hi Geek Emeritus, thank you for the advice! We are going to add the Serial.println lines in now and see what the board is reading. We have tested the external 5V that we use as a signal, and it is 5V.
Thank you so much for all the info larryd, do you think our code could cause any clashes with each other? We would like each of the if statements to run in sequence after one another.
Here is the link to the 24VDC relays we are using to break and give signal to the Arduino Mega: Rhomberg R5502 8 Pin Relay
Thank you both for all your help so far, we really do appreciate your time.
There maybe interference from the birds nest of wires that is causing your problems.
Confirm the coils on the Sig1-7 relays have kickback diodes.
These diodes absorb switching spikes when the Sig relays operate, 1N4007 would work.
Example:
D11 (across relay coil K5) protects Q3 in the circuit below.