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


user313: 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.

15305: <p>I got this program. It controls a refrigeration room using a ds1280 temperature probe.</p>

<p>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.</p>

<p>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.</p>

<p>Here are the most resembling parts of the code. </p>

<pre><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 &amp;&amp; millis()- comOnTime  &gt;= 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 &gt; 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 &amp;&amp; millis()- comOnTime  &gt;= 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 &gt; 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; 
}
</code></pre>
 313: <p>Are you trying to control by temperature or by time? </p>

<p>(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.)</p>

<p>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.</p>
