Designing a code to keep relay on despite loop resetting

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;
    }
    }
}

Post the complete code please. Click Auto-Format in the IDE before you copy it.

1 Like
#include "max6675.h"
#include <SPI.h>
#include "Adafruit_MAX31855.h"
 byte Z3_STATE = LOW;
 byte Z4_STATE = LOW;
 byte Z5_STATE = LOW;

const byte Z3_PIN = 2;
const byte Z4_PIN = 3;
const byte Z5_PIN = 4;
const byte Z6_PIN = 5;
const byte thermoDO = 6;
const byte thermoCLK = 7;
const byte thermoC3 = 8;
const byte thermoC4 = 9;
const byte thermoC5 = 10;
//MAX6675 thermocouple3(thermoCLK, thermoC3, thermoDO);
MAX6675 thermocouple4(thermoCLK, thermoC4, thermoDO);
//MAX6675 thermocouple5(thermoCLK, thermoC5, thermoDO);
Adafruit_MAX31855 thermocouple3(thermoCLK, thermoC3, thermoDO);
Adafruit_MAX31855 thermocouple5(thermoCLK, thermoC5, thermoDO);
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;
    }
    }
 
delay(200);
Serial.print("3:");
Serial.print(thermocouple3.readCelsius());
Serial.print(";4:");
delay(200);
Serial.print(thermocouple4.readCelsius());
Serial.print(";5:");
delay(200);
Serial.println(2*thermocouple5.readCelsius());
delay(200);


}
1 Like

Thanks!

1 Like

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.

Yes!!!!! Thank you! that worked. I changed default to Case 'S' and use that as my stop_all case.

1 Like

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