Hi,
I am working on a code that uses a solid state relay module to turn on/off different zones of heaters. I am using Serial read to disable/enable respective zones. Everything is good but I'm guessing after every iteration of the loop, my relay forgets its state and turns off. I was wondering how can I latch my relay to stay in that state whether or not the arduino switches on or off .
Using Arduino Nano with a 4ch SSR relay module, The relay module has a separate power source so as to not overload the Arduino.
Here is most of my code. Am I doing something wrong? any help is appreciated.
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
pinMode(Z3_PIN, OUTPUT);
pinMode(Z4_PIN, OUTPUT);
pinMode(Z5_PIN, OUTPUT);
pinMode(Z6_PIN, OUTPUT);
}
void loop() {
// put your main code here, to run repeatedly:
if (Serial.available() > 0)
{
char incomingByte = Serial.read();
switch (incomingByte) {
case '3':
if (Z3_STATE == LOW) {
Z3_STATE = HIGH;
digitalWrite(Z3_PIN, HIGH);
} else {
Z3_STATE = LOW;
digitalWrite(Z3_PIN, LOW);
}
break;
case '4':
if (Z4_STATE == LOW) {
Z4_STATE = HIGH;
digitalWrite(Z4_PIN, HIGH);
} else {
Z4_STATE = LOW;
digitalWrite(Z4_PIN, LOW);
}
break;
case '5':
if (Z5_STATE == LOW) {
Z5_STATE = HIGH;
digitalWrite(Z5_PIN, HIGH);
} else {
Z5_STATE = LOW;
digitalWrite(Z5_PIN, LOW);
}
break;
default:
Z3_STATE = LOW;
Z4_STATE = LOW;
Z5_STATE = LOW;
digitalWrite(Z3_PIN, LOW);
digitalWrite(Z4_PIN, LOW);
digitalWrite(Z5_PIN, LOW);
break;
}
}
}
My guess is that there might be additional characters like a carriage return or line feed after your commands that also get received and invoke this default code:
You could prove/disprove this guess by adding a Serial.print(incomingByte), or by changing the 'default:' label to case('R'): or something similar.