/*on 03/10/2023, this is a three (room, fridge, freezer) temperature sensor (DS18B20)code
to monitor & control compressor & defrost operations for a fridge  
the three sensors can be connected to one bus input to arduino as each
should have its own IP address according to its datasheet, yet in wokwi
simulator it doesn't so, I connected three separate sensors.
Any -127 reading means a faulty connection*/
/*unsigned long millis function is used to overcome delay() function which suspends the
code until time elapses*/
/*also, a reset (every < 1176 hours) of unsigned long is installed to avoid
overflow of this function, otherwise the code will not perform well after 49 days*/ 
/*the delay functions in this code are intended to easily read the LCD display*/
/*you can edit the wokwi right hand diagram by inserting text in diagram.json
within any location using below code while changing "text3,4,5 etc." & "top" & "left"
values & new text name
      "type": "wokwi-text",
      "id": "text2",
      "top": -369.45,
      "left": 857.75,
      "attrs": { "text": "fridge" } */
/*while in run mode, you can click on any sensor & change the temperature
to monitor the real effect within the code instructions*/
/*you can copy the code sketch.ino & diagram.json & paste both in a blank
new sheet to test other changes while the orignal is saved*/ 
/*click on any component & you can rotate it by ctrl & r*/


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

#include <LiquidCrystal_I2C.h> //library for LCD

LiquidCrystal_I2C lcd(0x27, 16, 2);

OneWire oneWire1(10);

DallasTemperature sensors1(&oneWire1);

OneWire oneWire2(11);

DallasTemperature sensors2(&oneWire2);

OneWire oneWire3(12);

DallasTemperature sensors3(&oneWire3);

const int   compressorRelay        = 13;

const int  defrostRelay      = 14;  

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;

          //float totalcounter;

extern volatile unsigned long timer0_millis;

void setup(void)

{

  Serial.begin(9600);
 
 lcd.init();

lcd.backlight();



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

pinMode( compressorRelay, OUTPUT);    

  sensors1.begin();

  sensors2.begin();

  sensors3.begin();

}

void loop(void)

{

  digitalWrite( compressorRelay
, LOW);

  digitalWrite (defrostRelay, LOW);

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((freezer>=0) && digitalRead(defrostRelay) == LOW)

    {digitalWrite( compressorRelay
  , HIGH);}

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

    {digitalWrite( compressorRelay
  , HIGH);}

    if((freezer<-10 ))

    {digitalWrite( compressorRelay
  , LOW);}

 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.setCursor(0,0);
   lcd.print("Time=    minutes");
   lcd.setCursor(14,1);

   lcd.print((char)223);
   lcd.setCursor(0,1);
lcd.print("Temperature=");
   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   =     min");
                 
                  lcd.setCursor(8,1); //column/row
                  
                  lcd.print(totaltime/60000);
                  delay(5000);


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

                  lcd.print("time   =     min");
                 
                  lcd.setCursor(8,0); //column/row
                  
                 lcd.print(totaltime/60000);
                 // 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   =     min");
                 
                  lcd.setCursor(8,1); //column/row
                  
                 lcd.print(totaltime/60000);

                 delay(3000);

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

                  lcd.print("time   =     min");
                 
                  lcd.setCursor(8,0); //column/row
                  
                 lcd.print(totaltime/60000);



                 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 >= 10000){
    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);
      }

    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);  
 
  }
goto label;

}
NOCOMNCVCCGNDINLED1PWRRelay Module
freezer
fridge
room
cooling
defrosting