Heating: Fireplace heath exchanger Solar puffer

Hi,
I have fireplace with a water heater, the water can heat the solar accumulator (puffer).
Basically the water goes in an heater exchanger and the cold side heat the puffer.
To extract the maximum amount of heat from the fireplace the heat exchanger "cold side" must run when the hot side is hotter (otherwise the puffer will cool down).
An arduino nano sense if the fireplace is on (thermostat on the side) and with 4 temperature sensor i read the temperature in and out of each side of the heat exchanger and according to the values it turns on the pumps.
It work quite well but sometime it freeze, the temperatures aren't right or a pump stay on, but a restart fix it.

It's one of my first sketch in Arduino so feel free to expose my mistake, I'm sure experienced people have better solutions than mine.

#include <LiquidCrystal.h>
#include <LiquidCrystal_I2C.h>
#include <OneWire.h>
#include <DallasTemperature.h>
#include <ArduinoJson.h>

#define ONE_WIRE_BUS 7
#define Rl_Ph A1
#define Rl_Pc A2
#define Rl_kill A0
#define SetMode_Button A3
///
#define pinClk_M 2
#define pinDt_M  8
#define pinClk_K 3
#define pinDt_K  9
#define pinSw_K  4
#define pin_caminetto 5

LiquidCrystal_I2C lcd(0x27, 20 , 4);
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);

// Addresses of DS18B20 sensors connected to the 1-Wire bus
byte Sensor_Hi[8] = {0x28, 0xE6, 0x61, 0x75, 0xD0, 0x01, 0x3C, 0xB0};
byte Sensor_Ho[8] = {0x28, 0xE1, 0xAB, 0x75, 0xD0, 0x01, 0x3C, 0xAA};
byte Sensor_Ci[8] = {0x28, 0xC9, 0x8A, 0x75, 0xD0, 0x01, 0x3C, 0x88};
byte Sensor_Co[8] = {0x28, 0xD5, 0x6F, 0x75, 0xD0, 0x01, 0x3C, 0xA6};

unsigned long lastTX = millis();

unsigned long Now = millis();

unsigned long DisplayRefreshTime = 500;
unsigned long t_DisplayRefresh = millis();
unsigned long algRefreshTime = 500;
unsigned long t_algRefresh = millis();

unsigned long t_CambioStato = millis();
unsigned long t_LastRefresh = millis(); // refresh sensori
unsigned long t_T2 = millis();
unsigned long t_T3 = millis();
///

int menu = 1;
//int menu_H = 1;

char *desc[] = {"null"
                , "Min delta C          "
                , "Min Hi - Ci          "
                , "t kill caldaia       "
                , "t assestamento       "
                , "Isteresi             "
                , "t forzatura T3       "
                , "t min in T3          "
               };
char *D_Tier[] = {" T0>------ "
                  , " T0>T1>--- "
                  , " T0>T1>T2> "
                 };
String Row_H0 , Row_H1 , Row_H2 , Row_H3, Row_S0, Row_S1 , Row_S2 , Row_S3 ;

unsigned long lastOKButtonRead = millis();

float counter;
float current;
float stepper;

// Parametri di sistema //

float TH_delta_C = 2.0; // Soglia su delta C
float TH_Differenziale = 5.0; // Differenza minima tra Hi e Ci
float TH_kill = 50.0;
int Tempo_assestamento = 1;
float isteresi = 1;


// variabili //
float temp_Hi;
float temp_Ho;
float temp_Ci;
float temp_Co;
float delta_H;
float delta_C;
float transmit;
bool Caminetto;


int Tier = 0;
volatile byte INTR_M = 0;
volatile byte INTR_K = 0;
int SetupState = 0;

void setup() {
  lcd.init();
  lcd.backlight();
  Serial.begin(115200);
  sensors.begin();
  pinMode(pin_caminetto, INPUT_PULLUP);
  pinMode(SetMode_Button, INPUT_PULLUP);

  pinMode(Rl_kill, OUTPUT);
  pinMode(Rl_Ph, OUTPUT);
  pinMode(Rl_Pc, OUTPUT);
  //
  pinMode(pinClk_M, INPUT);
  pinMode(pinDt_M, INPUT);
  pinMode(pinClk_K, INPUT);
  pinMode(pinDt_K, INPUT);
  pinMode(pinSw_K, INPUT);
  attachInterrupt(0, RefreshRE_M, RISING ); //
  attachInterrupt(1, RefreshRE_K, RISING );
  menu_setings();
  lcd.clear();
}

void loop() {
  Now = millis();
  RefreshSensori(750);
  f_tier ();
  SetupState = digitalRead(SetMode_Button);
  if (INTR_M)   {
    RefreshRE_M();
    INTR_M = 0;
  }
  if (INTR_K)   {
    RefreshRE_K();
    INTR_K = 0;
  }
  if (SetupState == LOW) {
    Display_S();
    OK_Button(50);
  } else {
    Display_H();
  }
}


bool CfrTemp(float temperatura, float confronto, int TargetTier) {
  bool result;

  if (Tier >= TargetTier) {
    if (temperatura > confronto) {
      result = true;
    }
    else if (fabs(temperatura - confronto) < isteresi) {
      result = true;
    }
    else {
      result = false;
    }
  }
  else {
    if (temperatura > confronto) {
      result = true;
    }
    else {
      result = false;
    }
  }

  return result;
}

bool CfrKill() {
  bool result = false;
  if (digitalRead(Rl_kill)) {
    if (temp_Co >= TH_kill) {
      result = true;
    } else {
      result = false;
    }
  } else {
    if (fabs(temp_Co - TH_kill) < isteresi) {
      result = true;
    } else if (temp_Co >= TH_kill) {
      result = true;
    } else {
      result = false;
    }
  }
}

void RefreshSensori(unsigned long RefreshTime) {
  if (fabs(Now -  t_LastRefresh) > RefreshTime) {
    sensors.requestTemperatures();
    temp_Hi = sensors.getTempC(Sensor_Hi);
    temp_Ho = sensors.getTempC(Sensor_Ho);
    temp_Ci = sensors.getTempC(Sensor_Ci);
    temp_Co = sensors.getTempC(Sensor_Co);
    if (digitalRead(pin_caminetto) == HIGH) {
      Caminetto = false;
    } else {
      Caminetto = true;
    }
    delta_H = constrain((temp_Hi - temp_Ho), 0, 50);
    delta_C = constrain((temp_Co - temp_Ci), 0, 50);
    int trs = (int) ((delta_C / delta_H) * 100) ;
    transmit = constrain(trs, 0, 100);

    t_LastRefresh = Now;
  }
}

bool ctrl_TAssestamento() {
  unsigned long Tempo_assestamento_m = (unsigned long) Tempo_assestamento * 1000;
  bool result;
  if (fabs(Now - t_CambioStato) > Tempo_assestamento_m) {
    result = false;
  } else {
    result = true;
  }
  return result;
}

bool Cond_T1() {

  if (Caminetto) {
    return true;
  } else {
    return false;
  }
}

bool Cond_T2() {
  if (CfrTemp(temp_Hi, (temp_Ci + TH_Differenziale), 2)) {
    return true;
  } else {
    return false;
  }
}


void f_Tier_0 () {
  if (Cond_T1()) {
    Tier = 1;
    t_CambioStato = Now;

  } else {
    digitalWrite(Rl_Ph, HIGH);
    digitalWrite(Rl_Pc, HIGH);
    digitalWrite(Rl_kill, HIGH);
    Tier = 0;
  }
}

void f_Tier_1 () {
  if (Cond_T2()) {
    Tier = 2;
    t_CambioStato = Now;
    t_T2 = Now;
  } else if (Cond_T1()) {
    Tier = 1;
  } else {
    Tier = 0;
    t_CambioStato = Now;

  }
}

void f_Tier_2 () {

  if (Cond_T2()) {
    Tier = 2;
  } else {
    Tier = 0;
    t_CambioStato = Now;
  }
}

void f_tier() {
  if (abs(Now -  t_algRefresh) > algRefreshTime) {
    switch (Tier) {
      case 0:
        digitalWrite(Rl_Ph, HIGH);
        digitalWrite(Rl_Pc, HIGH);
        digitalWrite(Rl_kill, HIGH);
        f_Tier_0();
        break;
      case 1:
        digitalWrite(Rl_Ph, LOW);
        digitalWrite(Rl_Pc, HIGH);
        digitalWrite(Rl_kill, HIGH);
        f_Tier_1();
        break;
      case 2:
        digitalWrite(Rl_Ph, LOW);
        digitalWrite(Rl_Pc, LOW);
        if (CfrKill()) {
          digitalWrite(Rl_kill, LOW);
        } else {
          digitalWrite(Rl_kill, HIGH);
        }
        f_Tier_2();
        break;
      default:
        digitalWrite(Rl_Ph, HIGH);
        digitalWrite(Rl_Pc, HIGH);
        digitalWrite(Rl_kill, HIGH);
        f_Tier_0();
        Tier = 0;
        break;
    }
    t_algRefresh = Now;
  }
}

void Display_H() {
  if ((Now -  t_DisplayRefresh) > DisplayRefreshTime) {
    Row_H0 = "# " + String(D_Tier[Tier]) + " (" + String(transmit, 0) + "%)  " ;
    Row_H1 = "Hot =I:" + String(temp_Hi, 1) + ";O:" + String(temp_Ho, 1) ;
    Row_H2 = "Cold=I:" + String(temp_Ci, 1) + ";O:" + String(temp_Co, 1) ;
    Row_H3 = "Dh=" + String(delta_H, 1) + " ; Dc=" + String(delta_C, 1) + "   " ;

    lcd.setCursor(0, 0);
    lcd.print(Row_H0.substring(0, 20));
    lcd.setCursor(0, 1);
    lcd.print(Row_H1.substring(0, 20));
    lcd.setCursor(0, 2);
    lcd.print(Row_H2.substring(0, 20));
    lcd.setCursor(0, 3);
    lcd.print(Row_H3.substring(0, 20));
    t_DisplayRefresh = Now;

  }
}

void Display_S() {
  if (Now -  t_DisplayRefresh > DisplayRefreshTime) {
    String Row_S0 = "##### SETTINGS #####" ;
    String Row_S1 = String(desc[menu]) + "   " ;
    String Row_S2 = "Current = " + String(current, 1) + "       ";
    String Row_S3 = "New value = " + String(counter, 1) + "     ";
    lcd.setCursor(0, 0);
    lcd.print(Row_S0.substring(0, 20));
    lcd.setCursor(0, 1);
    lcd.print(Row_S1.substring(0, 20));
    lcd.setCursor(0, 2);
    lcd.print(Row_S2.substring(0, 20));
    lcd.setCursor(0, 3);
    lcd.print(Row_S3.substring(0, 20));
    t_DisplayRefresh = Now;
  }
}

void RefreshRE_M() {
  INTR_M = 1;
  if (digitalRead(pinClk_M) && !digitalRead(pinDt_M)) {
    menu_up();
  }
  if (digitalRead(pinClk_M) && digitalRead(pinDt_M)) {
    menu_down();
  }
  INTR_M = 0;
}

void RefreshRE_K() {
  INTR_K = 1;
  if (digitalRead(pinClk_K) && !digitalRead(pinDt_K)) {
    counter = counter + stepper;
  }
  if (digitalRead(pinClk_K) && digitalRead(pinDt_K)) {
    counter = counter - stepper;
  }
  INTR_K = 0;
}


void menu_up() {
  if (menu < 4) {
    menu = menu + 1;
  } else {
    menu = 1;
  }
  menu_setings();
}
void menu_down() {
  if (menu > 1) {
    menu = menu - 1;
  } else {
    menu = 5;
  }
  menu_setings();
}


void OK_Button(unsigned long RefreshTime) {
  int Ok_button = digitalRead(pinSw_K);
  if (Ok_button == LOW) {
    if ((Now -  lastOKButtonRead) > Ok_button) {
      menu_commit();
    }
    lastOKButtonRead = Now;
  }
}

String form_bool (bool value, bool inversione) {
  String out;
  if (inversione == true) {
    value == !value;
  }
  if (value == true) {
    out = " ON";
  } else {
    out = "OFF";
  }
  return out;
}


void menu_setings() {
  switch (menu) {
    case 1:
      current = TH_delta_C;
      counter = TH_delta_C;
      stepper = 0.5;
      break;
    case 2:
      current = TH_Differenziale;
      counter = TH_Differenziale;
      stepper = 0.5;
      break;
    case 3:
      current = TH_kill;
      counter = TH_kill;
      stepper = 0.5;
      break;
    case 4:
      current = Tempo_assestamento;
      counter = Tempo_assestamento;
      stepper = 0.5;
      break;
    case 5:
      current = isteresi;
      counter = isteresi;
      stepper = 0.5;
      break;
  }
}

void menu_commit() {
  switch (menu) {
    case 1:
      TH_delta_C = (float) counter;
      break;
    case 2:
      TH_Differenziale =  (float) counter;
      break;
    case 3:
      TH_kill =  (float) counter;
      break;
    case 4:
      Tempo_assestamento =  (int) counter;
      break;
    case 5:
      isteresi =  (float) counter;
      break;
  }
  menu_setings();
}



Nicely done. I see some wires are labeled, but not all. I do not see labels on the control panel so someone can guess what the control do.
Important, is the documentation you have done for the existing system. So if you fall over sometime, a person can begin to understand what and why.
Paul

I suggest that you turn up the level of compiler warnings in preferences - there's at least one warning there you should fix, and really getting rid of them all would be good.

It would be interesting to see a schematic - I rather suspect that you have an electrical issue. If I had to guess, I'd start with the connectivity to the relay module - is relay coil power separated from the Arduino?

Edit: I'm not sure what a puffer is in this context.

True I need to add more label, I also need to use a flat cable to connect the screen and rotary encoder.
Thanks!

I suggest that you turn up the level of compiler warnings in preferences - there's at least one warning there you should fix, and really getting rid of them all would be good.

I'll do it so I can post a better code.

It would be interesting to see a schematic - I rather suspect that you have an electrical issue.

As soon i have a little bit of time I will post a scheme, I also need to switch to flat cable for the knob and the display.

I rather suspect that you have an electrical issue. If I had to guess, I'd start with the connectivity to the relay module - is relay coil power separated from the Arduino?

Nice tip, the relays should be optically insulated (is this:https://www.amazon.it/dp/B06XRJ6XBJ/ref=cm_sw_em_r_mt_dp_B2E4Q857C0V6NZZV7Q48?_encoding=UTF8&psc=1 )

I'm not sure what a puffer is in this context

The solar accumulator or puffer is basically a big insulated tank, solar panel, furnace and the fireplace heat this tank and the hot water inside can be used directly for shower etc and to heat the house.

Thank you for the suggestions!

It's hard to be sure, but it looks like the jumper is still in place on the relay board, which suggests that the Arduino is powering the coils.

If so, I'll guess that that is at least part of the problem.

Thanks,
i add a voltage regulator only for the relay and it seems to work properly.
I also add a pic to my original post to show the scheme.