Values randomly resetting

Hello, i have built a temperature controller using controllino, ds18b20 and nextion HMI and am having problems when i set temeprature it resets other variables. Any suggestions are welcome.

#define Output_J2 CONTROLLINO_R1
#define Output_J3 CONTROLLINO_R2
#define Output_P CONTROLLINO_R3
#define Error CONTROLLINO_IN0
#include "EasyNextionLibrary.h" 
EasyNex myNex(Serial2);

//temp sonde
#include <SPI.h>
#include <Controllino.h>
#include <OneWire.h>
#include <DallasTemperature.h>
#define ONE_WIRE_BUS1 42
OneWire oneWire1(ONE_WIRE_BUS1); 
DallasTemperature sensor1(&oneWire1);
#define ONE_WIRE_BUS2 43
OneWire oneWire2(ONE_WIRE_BUS2); 
DallasTemperature sensor2(&oneWire2);
#define ONE_WIRE_BUS3 44
OneWire oneWire3(ONE_WIRE_BUS3); 
DallasTemperature sensor3(&oneWire3);
#define ONE_WIRE_BUSP 45
OneWire oneWireP(ONE_WIRE_BUSP); 
DallasTemperature sensorP(&oneWireP);

const long interval = 2000;
unsigned long previousMillis = 0;

bool napaka=0;


//reg pisarna
int poz=0;
bool aut=0;
double SetpointP, tempP;
//reg J1
long time1_start;
long time1_stop;
uint32_t time1_set;
uint32_t temp1_1;
uint32_t temp1_2;
bool state1 = 0;
double Setpoint1,temp1, temp1_old, temp1_new;

//reg J2
long time2_start;
long time2_stop;
uint32_t time2_set;
uint32_t temp2_1;
uint32_t temp2_2;
bool state2 = 0;
double Setpoint2, temp2, temp2_old, temp2_new;
//ref J3
long time3_start;
long time3_stop;
uint32_t time3_set;
uint32_t temp3_1;
uint32_t temp3_2;
bool state3 = 0;
double Setpoint3,temp3, temp3_old, temp3_new;

bool on1=0;
bool on2=0;
bool on3=0;

//time conversion to s
long (time_to_s()){
  long seconds;
  seconds=(Controllino_GetWeekDay()+1)*86400 + Controllino_GetHour()*3600 + Controllino_GetMinute()*60 + Controllino_GetSecond();
  return seconds;
}
void setup(void)
{    
    Serial.begin(9600);
    Controllino_RTC_init(0);
    sensor1.begin();
    sensor1.setResolution(9);
    sensor2.begin();
    sensor2.setResolution(9);
    sensor3.begin();
    sensor3.setResolution(9);
    sensorP.begin();
    sensorP.setResolution(9);
    myNex.begin(9600);
    pinMode(Error, INPUT_PULLUP);
    pinMode(Output_J1, OUTPUT);
    pinMode(Output_J2, OUTPUT);
    pinMode(Output_J3, OUTPUT);;
    pinMode(Output_P, OUTPUT);
    attachInterrupt(digitalPinToInterrupt(Error), error_, FALLING);
    
}
void loop(void)
{   
  while(Serial2.available()){
    myNex.NextionListen();
  }
    meritve();
    if(on1 == 1){
      reg_J1();
    }
    if(on2 == 1){
      reg_J2();
    }
    if(on3 == 1){
      reg_J3();
    }
    if(aut == 1){
      reg_P();
    }
   
    
    
    
    if(napaka==1){
    myNex.writeNum("error.val",1);  
    }
}
void meritve()
{
  unsigned long currentMillis = millis();
  if (currentMillis - previousMillis >= interval) {

    previousMillis = currentMillis;

    sensor1.requestTemperatures();
    temp1_new=sensor1.getTempCByIndex(0);
    if(temp1_new == -127){
      temp1=temp1_old;
    }else{
      temp1=temp1_new;
      temp1_old=temp1_new;
    }
    

    sensor2.requestTemperatures();
    temp2_new=sensor2.getTempCByIndex(0);
    if(temp2_new == -127){
      temp2=temp2_old;
    }else{
      temp2=temp2_new;
      temp2_old=temp2_new;
    }

    sensor3.requestTemperatures();
    temp3_new=sensor3.getTempCByIndex(0);
    if(temp3_new == -127){
      temp3=temp3_old;
    }else{
      temp3=temp3_new;
      temp3_old=temp3_new;
    }
    
    sensorP.requestTemperatures();
    tempP=sensorP.getTempCByIndex(0);
    if(tempP==-127.0){
      tempP=35;
    }
    
    myNex.writeNum("x1.val",temp1*10);
    
    myNex.writeNum("x3.val",temp2*10);
    
    myNex.writeNum("x5.val",temp3*10);
    myNex.writeNum("x7.val",tempP*10);
    
    Serial.print(Setpoint1);
    Serial.print(" , ");
    Serial.print(temp1);
    Serial.print("...");
    Serial.print(Setpoint2);
    Serial.print(" , ");
    Serial.print(temp2);
    Serial.print("...");
    Serial.print(Setpoint3);
    Serial.print(" , ");
    Serial.print(temp3);
    Serial.println();
 }

}
void reg_J1(){
  if(state1==1){
  if(temp1 >= Setpoint1){
    time1_stop=long(time_to_s())+long(time1_set)*3600;
    state1=0;
  }
  }
  if(time_to_s()==time1_stop){
    Setpoint1=temp1_2;
    myNex.writeNum("Tset1.val",Setpoint1*10);
  }
  if(Setpoint1 <= temp1){
    digitalWrite(Output_J1, LOW);
  }else{
    digitalWrite(Output_J1, HIGH);
  }

}
void reg_J2(){
  if(state2==1){
  if(temp2 >= Setpoint2){
    time2_stop=long(time_to_s())+long(time2_set)*3600;
    
    state2=0;
  }
  }
  if(time_to_s()==time2_stop){
    Setpoint2=temp2_2;
    myNex.writeNum("x2.val",Setpoint2*10);
  }
  if(Setpoint2 <= temp2){
    digitalWrite(Output_J2, LOW);
  }else{
    digitalWrite(Output_J2, HIGH);
  }

}
void reg_J3(){
  if(state3==1){
  if(temp3 >= Setpoint3){
    time3_stop=long(time_to_s())+long(time3_set)*3600;
    
    state3=0;
  }
  }
  if(time_to_s()==time3_stop){
    Setpoint3=temp3_2;
    myNex.writeNum("x4.val",Setpoint3*10);
  }
  if(Setpoint3 <= temp3){
    digitalWrite(Output_J3, LOW);
  }else{
    digitalWrite(Output_J3, HIGH);
  }

}
void reg_P(){
  if(SetpointP <= tempP){
    digitalWrite(Output_P, LOW);
    
  }else{
    digitalWrite(Output_P, HIGH);
  }
  Serial.print(SetpointP);
  Serial.print(" , ");
  Serial.print(tempP);
  Serial.println();
  

}

void trigger1(){
 temp1_1=myNex.readNumber("Temp1_1.val")/10;
 temp1_2=myNex.readNumber("Temp1_2.val")/10;
 time1_set=myNex.readNumber("Time1.val");
 myNex.writeNum("Tset1.val",Setpoint1*10);
 Setpoint1=temp1_1;
 state1=1;
 on1=1;
}
 void trigger2(){
 Setpoint1=0;
 on1=0;
 digitalWrite(Output_J1, LOW);
 myNex.writeNum("Tset1.val",Setpoint1*10);
}
void trigger3(){
 temp2_1=myNex.readNumber("Temp2_1.val")/10;
 temp2_2=myNex.readNumber("Temp2_2.val")/10;
 time2_set=myNex.readNumber("Time2.val");
 myNex.writeNum("x2.val",Setpoint2*10);
 Setpoint2=temp2_1;
 state2=1;
 on2=1;
}
void trigger4(){
 Setpoint2=0;
 on2=0;
 digitalWrite(Output_J2, LOW);
 myNex.writeNum("x2.val",Setpoint2*10);
}
void trigger5(){
 temp3_1=myNex.readNumber("Temp3_1.val")/10;
 temp3_2=myNex.readNumber("Temp3_2.val")/10;
 time3_set=myNex.readNumber("Time3.val");
 myNex.writeNum("x4.val",Setpoint3*10);
 Setpoint3=temp3_1;
 state3=1;
 on3=1;
}
void trigger6(){
 Setpoint3=0;
 on3=0;
 digitalWrite(Output_J3, LOW);
 myNex.writeNum("x4.val",Setpoint3*10);
}
void trigger7(){
  poz=myNex.readNumber("p0.pic");
  Serial.println(myNex.readNumber("p0.pic"));
  //Serial.println(poz);

  switch(myNex.readNumber("p0.pic")){
    case 5: digitalWrite(Output_P, HIGH);aut=0;
    break;
    case 6: digitalWrite(Output_P, LOW);aut=0;
    break;
    case 7: aut=1;
    break;
    
  }
  SetpointP=myNex.readNumber("x6.val")/10;
}

void error_(){
  napaka=1;
  Serial.println("error");
}
void trigger8(){
  napaka=0;
}```

Which temperature and which variables? Have you used your serial.print() to watch a variable and see when it gets reset?

Which Controllino do you have ? Which software do you use and which build environment do you have ? Where can I read how you installed the specific Controllino things ?

I hope you use the normal Arduino IDE 1.8.19 and have selected a normal Arduino Mega board and the only special thing is a "Controllino" library.

Do you use this EasyNextion library ? https://github.com/Seithan/EasyNextionLibrary

I'm sorry, but I don't see a problem. Could you explain better what the problem is ?

All the values, the first value i set stays but when i try to set second value the first value resets.

I used Arduino IDE and selected Controllino maxi automation as board and for nextion display i use its newest software

Yes i use easynextion library as it works the best.
The problem is that when i try to set second value the first one resets and usually it doesn't even display on the hmi. Maybe the contactors that turn on the pumps couse some interference but the pumps draw less than 1amp 230v ac

Add a serial.print() message in setup() right after you set the baud rate to see if your Arduino is resetting.

2 Likes

Hello MikeM48

Start with a pure Arduino to get a running sketch and than make the portation to the Controllino.

Have a nice day and enjoy programming in C++ and learning.
MIND THE GAP

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