I got this program. It controls a refrigeration room using a ds1280 temperature probe.
The thing is, sometimes the probe reach 7.00°C then the refrigerattion cycle start (refrigeration cycle is on a switch case statment) and then the probe read 6.75. So the refrigeration don't complete its start and leaves part of the system in wrong mode.
How can I add some guard to ... once the switch case statment don't being interrupted by anything unless complete the on in this case or the off in other case.
Here are the most resembling parts of the code.
void turnOnCompressor(){
/*
This subrutine is intended to stop the refrigeration cycle
in the most ordered way, and keeping the refrigerant on
the high side of the system.
*/
// boolean refrigerattion = false; // var to handle ref status only once
// byte compOn = 0;
// byte pascomOn = 0; //to chek if the variable has updated.
// unsigned long comOnTime = 0;
// unsigend long comOnDelay = 1500;
// unsigend long comOnCT = 0; //the current time of the compressor.
//------for debug only
//Serial.print("comOnCT = "); Serial.println(comOnCT);
//Serial.print("comOnCT - millis() = "); Serial.println(millis() - comOnTime );
// Serial.println("INICIANDO REFRIGERACION");
if(refrigerationBegin == true )
{
if(refrigerattion == true && millis()- comOnTime >= comOnDelay ){
compOn ++;
comOnTime = millis();
/*
//--------for debug only
Serial.println("---------------------");
Serial.print("Var compOn "); Serial.println(compOn);
Serial.print("comoON "); Serial.println(compOn);
Serial.print("pascomON"); Serial.println(pascomOn);
Serial.println("---------------------");
*/
}
if(compOn > pascomOn ){
switch(compOn){
case(1):
lcd.setCursor(0,0); lcd.print(F(" CICLO DE "));
lcd.setCursor(0,1); lcd.print(F(" REFRIGERACION "));
lcd.setCursor(0,2); lcd.print(F(" "));
break;
case(2):
lcd.setCursor(0,0); lcd.print(F(" REFRIGERACION "));
lcd.setCursor(0,1); lcd.print(F(" ENCENDIENDO "));
lcd.setCursor(0,2); lcd.print(F(" VENT 1 "));
digitalWrite(dif1, LOW);
break;
case(3):
lcd.setCursor(0,0); lcd.print(F(" REFRIGERACION "));
lcd.setCursor(0,1); lcd.print(F(" ENCENDIENDO "));
lcd.setCursor(0,2); lcd.print(F(" VENT 2 "));
digitalWrite(dif2, LOW);
break;
case(4):
lcd.setCursor(0,0); lcd.print(F(" REFRIGERACION "));
lcd.setCursor(0,1); lcd.print(F(" ENCENDIENDO "));
lcd.setCursor(0,2); lcd.print(F(" SOLENOIDE "));
digitalWrite(solenoid, LOW);
break;
case(5):
lcd.setCursor(0,0); lcd.print(F(" REFRIGERACION "));
lcd.setCursor(0,1); lcd.print(F(" ENCENDIENDO "));
lcd.setCursor(0,2); lcd.print(F(" COMPRESOR "));
digitalWrite(compressor, LOW);
break;
case(6):
lcd.setCursor(0,1); lcd.print(F(" "));
lcd.setCursor(0,2); lcd.print(F(" "));
refrigerattion = false;
refOff = true ; // to cycle only once the set of the instructions per cycle.
compOn = 0;
cycleFansvar = false; //bool to chek the refrigerattion has stop
//--------------------------------
//refrigerattion = true; add this variable at the end of the off routine to cycle the
//operation otherwise will just start once
}
}
}
refrigerationBegin = false;
}
void turnOnCompressor(){
/*
This subrutine is intended to stop the refrigeration cycle
in the most ordered way, and keeping the refrigerant on
the high side of the system.
*/
// boolean refrigerattion = false; // var to handle ref status only once
// byte compOn = 0;
// byte pascomOn = 0; //to chek if the variable has updated.
// unsigned long comOnTime = 0;
// unsigend long comOnDelay = 1500;
// unsigend long comOnCT = 0; //the current time of the compressor.
//------for debug only
//Serial.print("comOnCT = "); Serial.println(comOnCT);
//Serial.print("comOnCT - millis() = "); Serial.println(millis() - comOnTime );
// Serial.println("INICIANDO REFRIGERACION");
if(refrigerationBegin == true )
{
if(refrigerattion == true && millis()- comOnTime >= comOnDelay ){
compOn ++;
comOnTime = millis();
/*
//--------for debug only
Serial.println("---------------------");
Serial.print("Var compOn "); Serial.println(compOn);
Serial.print("comoON "); Serial.println(compOn);
Serial.print("pascomON"); Serial.println(pascomOn);
Serial.println("---------------------");
*/
}
if(compOn > pascomOn ){
switch(compOn){
case(1):
lcd.setCursor(0,0); lcd.print(F(" CICLO DE "));
lcd.setCursor(0,1); lcd.print(F(" REFRIGERACION "));
lcd.setCursor(0,2); lcd.print(F(" "));
break;
case(2):
lcd.setCursor(0,0); lcd.print(F(" REFRIGERACION "));
lcd.setCursor(0,1); lcd.print(F(" ENCENDIENDO "));
lcd.setCursor(0,2); lcd.print(F(" VENT 1 "));
digitalWrite(dif1, LOW);
break;
case(3):
lcd.setCursor(0,0); lcd.print(F(" REFRIGERACION "));
lcd.setCursor(0,1); lcd.print(F(" ENCENDIENDO "));
lcd.setCursor(0,2); lcd.print(F(" VENT 2 "));
digitalWrite(dif2, LOW);
break;
case(4):
lcd.setCursor(0,0); lcd.print(F(" REFRIGERACION "));
lcd.setCursor(0,1); lcd.print(F(" ENCENDIENDO "));
lcd.setCursor(0,2); lcd.print(F(" SOLENOIDE "));
digitalWrite(solenoid, LOW);
break;
case(5):
lcd.setCursor(0,0); lcd.print(F(" REFRIGERACION "));
lcd.setCursor(0,1); lcd.print(F(" ENCENDIENDO "));
lcd.setCursor(0,2); lcd.print(F(" COMPRESOR "));
digitalWrite(compressor, LOW);
break;
case(6):
lcd.setCursor(0,1); lcd.print(F(" "));
lcd.setCursor(0,2); lcd.print(F(" "));
refrigerattion = false;
refOff = true ; // to cycle only once the set of the instructions per cycle.
compOn = 0;
cycleFansvar = false; //bool to chek the refrigerattion has stop
//--------------------------------
//refrigerattion = true; add this variable at the end of the off routine to cycle the
//operation otherwise will just start once
}
}
}
refrigerationBegin = false;
}
-
Sorry for the horrible indentation... this thing paste like thathAlejandro Santiago– Alejandro Santiago2015年12月03日 01:07:44 +00:00Commented Dec 3, 2015 at 1:07
-
Nope, you can use block controls to paste code correctly. I've cleaned up a bit the formatting, you can finish it up and add proper indentation.Igor Stoppa– Igor Stoppa2015年12月03日 01:32:05 +00:00Commented Dec 3, 2015 at 1:32
-
1I suggest you write (indented!) a simple representation of what you would like to do (the algorithm) in plain English.Igor Stoppa– Igor Stoppa2015年12月03日 01:36:04 +00:00Commented Dec 3, 2015 at 1:36
-
Try to avoid complex test cases inside "if" tests, especially if they can have side effects and if you do use complex conditions, check that the operator precedence meets your expectations. But I would advise to avoid complex tests altogether.Igor Stoppa– Igor Stoppa2015年12月03日 01:39:00 +00:00Commented Dec 3, 2015 at 1:39
-
Sorry the first time un here... Yes... Is a sistem drive by temperature.... The one secon to add switch statement... For say.. Tunr one fan then wait a second. . turn another fan... Wait a second. Turn compressor on.Alejandro Santiago– Alejandro Santiago2015年12月03日 03:26:30 +00:00Commented Dec 3, 2015 at 3:26
1 Answer 1
Are you trying to control by temperature or by time?
(I'm finding the code hard to follow. There seem to be 2 copies of it, and the comments in turnOnCompressor() seem to contradict the function name. I also don't see any code that mentions temperature.)
If this is temperature controlled, you need a dead-band - a temperature range bounded by a high- and a low setpoint. If you have a single setpoint, the system will jitter as air-circulation and electrical noise make the temperature appear to alternate above and below setpoint. The turn-on setpoint needs to be higher than the turn-off setpoint. You'll need to experiment to find out how far apart they need to be for the system to be stable.