Need help with logic in floor heating controller

So, I made a controller with Arduino Mega 2560, ENC28j60 Ethernet Shield, and 8 relay module.
I also have sensors in my rooms that send to Mega temperature (Wemos D1 with BNP180).

My problem is, when the controller changes from heating mode to a normal mode, relays get to HIGH if actualTemp is greater than setTemp - hysteresis. How can I make it to stay relay at LOW after a change to normal until actualTemp value gets to SetTemp. All work at Blynk app.

void heating(){
    for(int i=0; i<4; i++) {
        digitalWrite(r[i], LOW);
        p[i] = 1;
    }
    digitalWrite(r[7], LOW);
    digitalWrite(r[6], LOW);
    led1.on();
    led2.on();
    mode = 1;
    heat = 1;

}


void normal(){
  if(heat == 1){
      for(int i=0; i<4; i++) {
        digitalWrite(r[i], HIGH);
        p[i] = 0;
        digitalWrite(r[7], LOW);
        led1.on();
      }
        
    }
    digitalWrite(r[6], HIGH);
    led2.off();
    led1.off();
    heat = 0;
  }
  if(heat == 0){ 
    for(int i=0; i<4; i++) {
        if(actualTemp[i] < setTemp[i] - histeresis){
            digitalWrite(r[i], LOW);
            p[i] = 1;
        }
        if(actualTemp[i] >= setTemp[i]){
            digitalWrite(r[i], HIGH);
            p[i] = 0;
        }
    }
        if(p[0] == 1 || p[1] == 1 || p[2] == 1 || p[3] == 1){
            digitalWrite(r[7], LOW);
            led1.on();
        }else{
            digitalWrite(r[7], HIGH);
            led1.off();
        }
    digitalWrite(r[6], LOW);
    led2.on();
    mode = 2;
  }
}

And all code here :

#define BLYNK_PRINT Serial

#include <UIPEthernet.h>
#include <BlynkSimpleUIPEthernet.h>
#include <TimeLib.h>
#include <EEPROM.h>
#include <WidgetRTC.h>

BlynkTimer timer;
WidgetRTC rtc;
WidgetLED led1(V30);
WidgetLED led2(V31);
WidgetLED ledr0(V32);
WidgetLED ledr1(V33);
WidgetLED ledr2(V34);
WidgetLED ledr3(V35);
WidgetTerminal terminal(V40);

char auth[] = "123123123";

const int r[8] = {30, 31, 32, 33, 34, 35, 36, 37};
const float histeresis = 2.00f;

int p[4] = {0};
float setTemp[4] = {0.00f};
float actualTemp[4] = {1.00f};
float H = 0.00f;
float M = 0.00f;
float T = 0.00f;
float m = 0.00f;
float t[4] = {0.00f};
int mode, stopHeat;
int heat = 1;

BLYNK_CONNECTED() {
  rtc.begin();
}

void setup(){
    Serial.begin(9600);
    Blynk.begin(auth);
    
    EEPROM.get(0, t[0]);
    EEPROM.get(10, t[1]);
    EEPROM.get(20, t[2]);
    EEPROM.get(30, t[3]);
    
    Serial.println("Witam! Zaczynam dziaƂanie ;)");
    Serial.println("t0: " + String(t[0]));
    Serial.println("t1: " + String(t[1]));
    Serial.println("t2: " + String(t[2]));
    Serial.println("t3: " + String(t[3]));

    for(int i = 30; i < 38; i++){
        pinMode(i, OUTPUT);
        digitalWrite(i, LOW);
        delay(200);
        digitalWrite(i, HIGH);
    }
    timer.setInterval(60000L, clockDisplay);
    led1.off();
    led2.off();
    setSyncInterval(10 * 60);
    Blynk.run();
}

void loop(){
    if(Blynk.connected()) {
        Blynk.run();
        timer.run();
    } else {
        Blynk.connect(3333);
    }
    m = minute();
    H = hour();
    M = m/60;
    T = H + M;

    if(T >= t[0] && T < (t[0] + 1.00) || T >= t[2] && T < (t[2] + 1.00))heating();

    if(T >= (t[0] + 1.00) && T < t[1] || T >= (t[2] + 1.00) && T < t[3])normal();

    if(T >= t[1] && T < t[2] || T >= t[3] && T < 24.00 || T >= 0.00 && T < t[0])pause();

    if(p[0] == 1){ledr0.on();}
    if(p[1] == 1){ledr1.on();}
    if(p[2] == 1){ledr2.on();}
    if(p[3] == 1){ledr3.on();}
    if(p[0] == 0){ledr0.off();}
    if(p[1] == 0){ledr1.off();}
    if(p[2] == 0){ledr2.off();}
    if(p[3] == 0){ledr3.off();}
    
}

void heating(){
    for(int i=0; i<4; i++) {
        digitalWrite(r[i], LOW);
        p[i] = 1;
    }
    digitalWrite(r[7], LOW);
    digitalWrite(r[6], LOW);
    led1.on();
    led2.on();
    mode = 1;
    heat = 1;

}


void normal(){
  if(heat == 1){
      for(int i=0; i<4; i++) {
        digitalWrite(r[i], HIGH);
        p[i] = 0;
        digitalWrite(r[7], LOW);
        led1.on();
      }
        
    }
    digitalWrite(r[6], HIGH);
    led2.off();
    led1.off();
    heat = 0;
  }
  if(heat == 0){ 
    for(int i=0; i<4; i++) {
        if(actualTemp[i] < setTemp[i] - histeresis){
            digitalWrite(r[i], LOW);
            p[i] = 1;
        }
        if(actualTemp[i] >= setTemp[i]){
            digitalWrite(r[i], HIGH);
            p[i] = 0;
        }
    }
        if(p[0] == 1 || p[1] == 1 || p[2] == 1 || p[3] == 1){
            digitalWrite(r[7], LOW);
            led1.on();
        }else{
            digitalWrite(r[7], HIGH);
            led1.off();
        }
    digitalWrite(r[6], LOW);
    led2.on();
    mode = 2;
  }
}

void pause(){
    for(int i=0; i<4; i++) {
        digitalWrite(r[i], HIGH);
        p[i] = 0;
    }
    digitalWrite(r[7], HIGH);
    digitalWrite(r[6], HIGH);
    led1.off();
    led2.off();
    mode = 3;
}

void clockDisplay() {
    if(mode == 1){
      Serial.println(String(hour()) + ":" + String(minute()) + "     grzanie     " + String(T));
      terminal.println(String(hour()) + ":" + String(minute()) + "     grzanie     " + String(T));      
      }
    if(mode == 2){
      Serial.println(String(hour()) + ":" + String(minute()) + "   normalny    " + String(T));
      terminal.println(String(hour()) + ":" + String(minute()) + "    normalny    " + String(T));      
      }
    if(mode == 3){
      Serial.println(String(hour()) + ":" + String(minute()) + "     przerwa     " + String(T));
      terminal.println(String(hour()) + ":" + String(minute()) + "     przerwa     " + String(T));
      }

}

BLYNK_WRITE(V20){
    float x = param.asFloat();
    t[0] = x;
    EEPROM.put(0, t[0]);
    Serial.println(t[0]);
    terminal.println(t[0]);
}

BLYNK_WRITE(V21){
    float x = param.asFloat();
    t[1] = x;
    EEPROM.put(10, t[1]);
    Serial.println(t[1]);
    terminal.println(t[1]);
}

BLYNK_WRITE(V22){
    float x = param.asFloat();
    t[2] = x;
    EEPROM.put(20, t[2]);
    Serial.println(t[2]);
    terminal.println(t[2]);
}

BLYNK_WRITE(V23){
    float x = param.asFloat();
    t[3] = x;
    EEPROM.put(30, t[3]);
    Serial.println(t[3]);
    terminal.println(t[3]);
}


//sensor0
BLYNK_WRITE(V10) {
  float x = param[0].asFloat();
  float y = param[1].asFloat();
  setTemp[0] = x;
  actualTemp[0] = y;
}
//sensor1
BLYNK_WRITE(V11) {
  float x = param[0].asFloat();
  float y = param[1].asFloat();
  setTemp[1] = x;
  actualTemp[1] = y;
}
//sensor2
BLYNK_WRITE(V12) {
  float x = param[0].asFloat();
  float y = param[1].asFloat();
  setTemp[2] = x;
  actualTemp[2] = y;
}
//sensor3
BLYNK_WRITE(V13) {
  float x = param[0].asFloat();
  float y = param[1].asFloat();
  setTemp[3] = x;
  actualTemp[3] = y;
}



void(* resetFunc) (void) = 0;
BLYNK_WRITE(V100)  // Reset
{
if (param.asInt()==1) {
delay(100);
resetFunc();
  } 
}

Sorry for not having great English, and thanks for all suggestions. :wink:

The thing that concerns me here is that sizeof(p) != sizeof(r)

@anon73444976 r[i] is a relay pin and p[i] i use to send to app if the relay is on LOW or HIGH state.

I don't care.
The size difference is still concerning

So, r[0] to r[3] are used to an electric valve, 4,5 are just not used, 7,8 are pumps, that's what you were concerned about?

R[8] does not exist... Index 8 points out in the unknown... You likely mean index 6 and 7.

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