Temperature sensor condition met

Hi friends,

With the assistance of more experienced members, I finally achieved what I aimed to.
I am using three sensors DS18B20 to monitor temperature at different locations.

The code & hardware are running satisfactorily faultless for several days so far.
Having the Arduino & coding with unlimited functions, I wish to add one more task to the code.

New condition:-
While the loop is running, if at anytime the freezer temperature measured ≥5°C for more than 5 minutes, the code will change the status of a free output (53,HIGH). This output condition should remain HIGH until dropping < 5°C to become (53,LOW) & so on.

With my limited experience & avoiding to mess-up the code, I need your help to validate the above mentioned new condition among the below code.

The code without the new condition:-

#include <DallasTemperature.h> //library for DS18B20 sensors & it includes LCD

OneWire oneWire1(52); //room temperature sensor
DallasTemperature sensors1(&oneWire1);

OneWire oneWire2(24); //fridge temperature sensor
DallasTemperature sensors2(&oneWire2);


OneWire oneWire3(22);//freezer temperature sensor

DallasTemperature sensors3(&oneWire3);

const int   compressorRelay        = 25;

const int  defrostRelay      = 23;  

unsigned long previousMillisStartdefrosting;

unsigned long previousMillisDefrosting;

unsigned long previousMilliscounter;

bool Startdefrosting = false;

bool Defrosting = false;

bool enablereading = false;

int counter =0; //one counter with two actions
float countertime=0;
float totaltime;


#include <LiquidCrystal.h>
//LCD keypad shield setup pins
const int pin_RS = 8; 
const int pin_EN = 9; 
const int pin_d4 = 4; 
const int pin_d5 = 5; 
const int pin_d6 = 6; 
const int pin_d7 = 7; 
const int pin_BL = 10; 
LiquidCrystal lcd( pin_RS,  pin_EN,  pin_d4,  pin_d5,  pin_d6,  pin_d7);


extern volatile unsigned long timer0_millis;


int totalColumns = 16;
int totalRows = 2;


String scrollingMessage = "Three temperature sensors 10/10/2023;room, fridge, freezer & total operating time controller...";

void scrollMessage(int row, String message, int delayTime, int totalColumns) {
  for (int i=0; i < totalColumns; i++) {
    message = " " + message;  
  } 
  message = message + " "; 
  for (int position = 0; position < message.length(); position++) {
    lcd.setCursor(0, 0);
    lcd.print(message.substring(position, position + totalColumns));
    delay(delayTime);
  }
}

void setup(void)

{

  Serial.begin(9600);
 
lcd.begin(16, 2);

lcd.setCursor(0, 0);
  lcd.print("SETUP CONTROLLER");
delay(1500);


lcd.setCursor(0, 1);
 
  lcd.print("......WAIT......");
delay(1500);

 Serial.println("Measuring 3 sensors' temperatures");

pinMode( compressorRelay, OUTPUT);  
pinMode (defrostRelay, OUTPUT);  

  sensors1.begin();

  sensors2.begin();

  sensors3.begin();

}

void loop(void)

{
 digitalWrite( compressorRelay, LOW);

  digitalWrite (defrostRelay, LOW);
  //delay(500);

delay(5000); // a delay to absorb any power fluctuations on startup

label:

unsigned long currentMillis = millis();

totaltime = (countertime * 300000) +millis(); 

//millis reset to avoid overflow (to be 20 days in milliseconds)

if(millis() >= 300000)

 {noInterrupts ();
 countertime ++;
  timer0_millis=0;

 previousMilliscounter=0;

 previousMillisDefrosting=0;

 previousMillisStartdefrosting=0;

 currentMillis=0;

 previousMillisDefrosting=0;

 interrupts();}

sensors1.requestTemperatures();

sensors2.requestTemperatures();

sensors3.requestTemperatures();

  float room = sensors1.getTempCByIndex(0);

  float fridge = sensors2.getTempCByIndex(0);

  float freezer= sensors3.getTempCByIndex(0);

//sensor reading display delay


 if( currentMillis - previousMillisDefrosting >= 2000){

  enablereading = !enablereading;

  previousMillisDefrosting = currentMillis;

  }

  if(enablereading){

   Serial.print("room=");

   Serial.print(room);

   Serial.print(", ");

   Serial.print("fridge=");

   Serial.print(fridge);

   Serial.print(", ");

   Serial.print("freezer=");

   Serial.print(freezer);

   Serial.print(", ");

   Serial.print("m=");

   Serial.print(millis());

   Serial.print(", ");

   Serial.print("cm=");

   Serial.print(currentMillis);

   Serial.print(", ");

   Serial.print("pmsd=");

   Serial.print(previousMillisStartdefrosting);

   Serial.print(", ");

   Serial.print("pmd=");

   Serial.print(previousMillisDefrosting);

   Serial.print(", ");

   Serial.print("sd=");

   Serial.print(Startdefrosting);
    Serial.print(", ");
   
   Serial.print("total time=");

   Serial.print(totaltime/60000);
   Serial.print("min");
 
 
   Serial.println();

lcd.clear();
 lcd.setCursor(0, 0);
  lcd.print("....GOOD DAY....");
delay(500);
lcd.clear();
delay(500);
lcd.print("....GOOD DAY....");
delay(500);
lcd.clear();
delay(500);
lcd.print("....GOOD DAY....");

 
delay(500);
lcd.clear();
delay(500);
lcd.print("....GOOD DAY....");
delay(500);
lcd.clear();
delay(500);
lcd.print("....GOOD DAY....");
 
delay(500);
 
 lcd.setCursor(0, 0);
  
  scrollMessage(1, scrollingMessage, 250, totalColumns); //scrollMessage(int row, String message, int delayTime, int totalColumns)
  
lcd.setCursor(0,0);
   lcd.print("Time in hours    ");
   lcd.setCursor(14,1);

   //lcd.print((char)223);
   lcd.setCursor(0,1);
lcd.print("Temp.-55to+125 C");
lcd.setCursor(14,1); //column,row
   lcd.print((char)223);
   delay(3000);

   lcd.setCursor(0,0);
   lcd.print("Room   =       C");
   lcd.setCursor(14,0); //column,row
   lcd.print((char)223);
   lcd.setCursor(8,0);
   lcd.print(room);
   
   lcd.setCursor(0,1);
   lcd.print("Fridge =       C");
   lcd.setCursor(14,1);
   lcd.print((char)223);
   lcd.setCursor(8,1);
  lcd.print(fridge);

delay(5000);

   lcd.setCursor(0,0);
   lcd.print("Fridge =       C");
   lcd.setCursor(14,0);
   lcd.print((char)223);
   lcd.setCursor(8,0);
   lcd.print(fridge);

   lcd.setCursor(0,1);
   lcd.print("Freezer=       C");
   lcd.setCursor(14,1);
   lcd.print((char)223);
   lcd.setCursor(8,1);
   lcd.print(freezer);

delay(5000);

lcd.setCursor(0,0);
   lcd.print("Freezer=       C");
   lcd.setCursor(14,0);
   lcd.print((char)223);
   lcd.setCursor(8,0);
   lcd.print(freezer);

 lcd.setCursor(0,1);; //column/row

                  lcd.print("Time=         Hr");
                 
                  lcd.setCursor(5,1); //column/row
                  
                  lcd.print(totaltime/3600000);
                  delay(5000);


lcd.setCursor(0,0); //column/row

                  lcd.print("Time=         Hr");
                 
                  lcd.setCursor(5,0); //column/row
                  
                 lcd.print(totaltime/3600000);
                 // delay(5000);

lcd.setCursor(0,1);
   lcd.print("Room   =       C");
   lcd.setCursor(14,1); //column,row
   lcd.print((char)223);
   lcd.setCursor(8,1);
   lcd.print(room);

  delay(2000);
   }

  if(!enablereading)  {

   lcd.setCursor(0,0);

   lcd.print("Room   =       C");

   lcd.setCursor(14,0);
   lcd.print((char)223);
   lcd.setCursor(8,0);
   lcd.print(room);
    //delay(2000);
   lcd.setCursor(0,1);
   lcd.print("Fridge =       C");
   lcd.setCursor(14,1);
   lcd.print((char)223);
   lcd.setCursor(8,1);
   lcd.print(fridge);
 delay(2000);

    lcd.setCursor(0,0);
   lcd.print("Fridge =       C");
   lcd.setCursor(14,0);
   lcd.print((char)223);
   lcd.setCursor(8,0);
   lcd.print(fridge);
lcd.setCursor(0,1); //column/row

                  lcd.setCursor(0,1);
    lcd.print("Freezer=       C");
   lcd.setCursor(14,1);
   lcd.print((char)223);
   lcd.setCursor(8,1);
   lcd.print(freezer);
 delay(2000);


lcd.setCursor(0,0); //column/row

                  lcd.setCursor(0,0);
    lcd.print("Freezer=       C");
   lcd.setCursor(14,0);
   lcd.print((char)223);
   lcd.setCursor(8,0);
   lcd.print(freezer);
 //delay(2000);

lcd.setCursor(0,1); //column/row

                  lcd.print("Time=         Hr");
                 
                  lcd.setCursor(5,1); //column/row
                  
                 lcd.print(totaltime/3600000);

                 delay(3000);

lcd.setCursor(0,0); //column/row

                  lcd.print("Time=         Hr");
                 
                  lcd.setCursor(5,0); //column/row
                  
                 lcd.print(totaltime/3600000);

                 lcd.setCursor(0,1);
   lcd.print("Room   =       C");
   lcd.setCursor(14,1); //column,row
   lcd.print((char)223);
   lcd.setCursor(8,1);
   lcd.print(room);

   delay(3000);

   }

/*energize defrostRelay led once every 24 hours each for ??? minutes
without affecting lcd display scrolling text or
 compressorRelay
 led functionality
below line represents the 24 hour timer during which an action will start & stop once
& one counter to count two actions on/off*/

 if(currentMillis - previousMilliscounter >= 26000){
    counter = 2;
    previousMilliscounter= currentMillis;}

 // defrostRelay led ON period

if ( currentMillis - previousMillisStartdefrosting >= 2000 && (counter == 2 || counter == 1) )

   {

    counter --;
    digitalWrite( compressorRelay
  , LOW);
    previousMillisStartdefrosting = currentMillis;

    Startdefrosting = !Startdefrosting;      

   }

   if( !Startdefrosting)

    {

      digitalWrite( defrostRelay, LOW);    }
      
      if((freezer>=0)) 

    {
      digitalWrite(defrostRelay,LOW);
      digitalWrite( compressorRelay
    , HIGH);
      }
lcd.clear();
lcd.setCursor(0,0); //column,row


                  lcd.print("..COOLING DOWN..");
                  delay(300);
                  lcd.clear();
delay(300);
                  lcd.print("..COOLING DOWN..");
                  delay(300);
                  lcd.clear();
delay(300);
                  lcd.print("..COOLING DOWN..");
                  delay(300);
                  lcd.clear();
delay(300);
                  lcd.print("..COOLING DOWN..");
                  delay(300);
                  lcd.clear();
delay(300);
                  lcd.print("..COOLING DOWN..");
                  delay(300);
                  lcd.clear();
                  delay(300);

                   lcd.print("..COOLING DOWN..");
                  delay(300);
                  lcd.clear();
delay(300);

 lcd.print("..COOLING DOWN..");
                  delay(300);
                  lcd.clear();
delay(300);

 lcd.print("..COOLING DOWN..");
                  delay(300);
                  lcd.clear();
delay(300);

 lcd.print("..COOLING DOWN..");
                  delay(300);
                  lcd.clear();
delay(300);

 lcd.print("..COOLING DOWN..");
                  delay(300);
                  lcd.clear();
delay(300);
                  lcd.print("..COOLING DOWN..");

                  lcd.print("....COOLING....");
                  delay(1500);
                 // lcd.setCursor(0,0); //column,row

                  //lcd.print("Time=         Hr");

    if((freezer<-10 && freezer<0) && digitalRead(defrostRelay) == LOW )

    {digitalWrite( compressorRelay  , HIGH);}

    if((freezer<-10 ))

    {digitalWrite( compressorRelay  , LOW);}

    if( Startdefrosting)                    

  {
   digitalWrite( compressorRelay  ,LOW);
    digitalWrite( defrostRelay,HIGH);  
 
  }

  lcd.clear();
lcd.setCursor(0,0); //column/row
 lcd.print("...DEFROSTING...");
                  delay(300);
                  lcd.clear();
                  delay(300);
                  lcd.print("...DEFROSTING...");
                  delay(300);
                  lcd.clear();
                  delay(300);
                  lcd.print("...DEFROSTING...");
                  delay(300);
                  lcd.clear();
                  delay(300);
                  lcd.print("...DEFROSTING...");
                  delay(300);
                  lcd.clear();
                  delay(300);
                  lcd.print("...DEFROSTING...");
                  delay(300);
                  lcd.clear();
                  delay(300);
                  lcd.print("...DEFROSTING...");

                  
goto label;

}

to your question :

one challenge you have is that the code is full of delays... for example you can spend 6 seconds in blinking "..COOLING DOWN.." on the LCD). While you do that, you can't check anything

to achieve what you do with somewhat not to precise time management, you do the following

  • you have a global variable bool freezerAlarm set to false initially bool freezerAlarm = false;
  • you have a global variable unsigned long freezerAlarmStartTime;

In the loop

  • when you notice the measured freezer temperature goes above 5°C and freezerAlarm is false, then you set freezerAlarm to true and make note of the time freezerAlarmStartTime = millis();. if the temperature is below 5°C and the alarm was raised, you set pin 53 LOW

  • if freezerAlarm is true, then you check if 5 minutes have elapsed since `freezerAlartmStartTimeù and if so you trigger pin 53 HIGH (and possibly make a note the alarm is raised).

give it a try


side notes

really, they told you to use goto?

why don't you have this in the setup if you only want it to be done once

 digitalWrite( compressorRelay, LOW);
 digitalWrite (defrostRelay, LOW);
 delay(5000); // a delay to absorb any power fluctuations on startup

this way you can get rid of the label and goto and let the loop loops (which is a good idea as the main function might need to do other stuff depending on which arduino you run).


why do you have a section with noInterrupts ();? you don't use interrupts yourself, so nothing will mess around with your variables...


when you want to blink your text on the screen, don't repeat the code

  lcd.print("..COOLING DOWN..");
  delay(300);
  lcd.clear();
  delay(300);
  lcd.print("..COOLING DOWN..");
  delay(300);
  lcd.clear();
  delay(300);
  lcd.print("..COOLING DOWN..");
  delay(300);
  lcd.clear();
  delay(300);
  lcd.print("..COOLING DOWN..");
  delay(300);
  lcd.clear();
  delay(300);
  lcd.print("..COOLING DOWN..");
  delay(300);
  lcd.clear();
  delay(300);

  lcd.print("..COOLING DOWN..");
  delay(300);
  lcd.clear();
  delay(300);

  lcd.print("..COOLING DOWN..");
  delay(300);
  lcd.clear();
  delay(300);

  lcd.print("..COOLING DOWN..");
  delay(300);
  lcd.clear();
  delay(300);

  lcd.print("..COOLING DOWN..");
  delay(300);
  lcd.clear();
  delay(300);

  lcd.print("..COOLING DOWN..");
  delay(300);
  lcd.clear();
  delay(300);
  lcd.print("..COOLING DOWN..");

use a for loop

for (byte n = 0; n < 10; n++) { // blink 10 times "COOLING DOWN"
  lcd.print("..COOLING DOWN..");
  delay(300);
  lcd.clear();
  delay(300);
}

the code will be much shorter and adjusting the number of blink is easy, just change 10 into what you want.


(this would benefit being rewritten as a state machine)

Thanks Jackson,
I was sure that the code is not professional at all in lot of aspects.
I will rewrite the easy part (blinking, & go-to issues) as per your advises but am not sure about the main question if I can achieve with further assistance.

can you give it a try following this?

Hi Jackson,
Thanks again.
The required additional control is added as per your advise, & code is looping well.
Next step will be the hardware.
Should be OK.
Thanks for your help.

good have fun

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