/*testing one sensor, freezer*/
#include <DallasTemperature.h> //library for DS18B20 sensors & it includes LCD
#include <LiquidCrystal_I2C.h> //library for LCD
OneWire oneWire3(22);
DallasTemperature sensors3(&oneWire3);
const int compressorRelay = 23;
const int defrostRelay = 40;
unsigned long fail=millis();
unsigned long duration = 0.5*60*1000;
const int failCool=53;
bool failure=false;
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;
int totalColumns = 16;
int totalRows = 2;
LiquidCrystal_I2C lcd(0x27, totalColumns, totalRows);
String scrollingMessage = "Three temperature sensors 26/26/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, row);
lcd.print(message.substring(position, position + totalColumns));
delay(delayTime);
}
}
void setup(void)
{
Serial.begin(9600);
lcd.init();
lcd.backlight();
Serial.println("Measuring 3 sensors' temperatures");
pinMode( compressorRelay, OUTPUT);
pinMode( defrostRelay,OUTPUT);
pinMode( failCool,OUTPUT);
sensors3.begin();
}
void loop(void)
{
digitalWrite( compressorRelay
, LOW);
digitalWrite (defrostRelay, LOW);
delay(5000); // a delay to absorb any power fluctuations on startup
label:
float freezer= sensors3.getTempCByIndex(0);
//digitalWrite (failCool,LOW);
if(freezer>10);{
if(!failure) {fail=millis();
failure=true;
}
if(millis()-fail>=duration){
digitalWrite(failCool,HIGH);
//delay(5000);
}
else{failure=false;
}
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();}
sensors3.requestTemperatures();
float freezer= sensors3.getTempCByIndex(0);
//sensor reading display delay
if((freezer>=0) && digitalRead(defrostRelay) == LOW)
{digitalWrite( compressorRelay
, HIGH);}
if((freezer<-26 && freezer<0) && digitalRead(defrostRelay) == LOW )
{digitalWrite( compressorRelay
, HIGH);}
if((freezer<-26 ))
{digitalWrite( compressorRelay
, LOW);}
if( currentMillis - previousMillisDefrosting >= 2000){
enablereading = !enablereading;
previousMillisDefrosting = currentMillis;
}
if(enablereading){
Serial.print("freezer=");
Serial.print(freezer);
Serial.print("total time=");
Serial.print(totaltime/60000);
Serial.print("min");
Serial.println();
lcd.setCursor(0, 0);
scrollMessage(1, scrollingMessage, 250, totalColumns);
lcd.setCursor(0,0);
lcd.print("Time= minutes");
lcd.setCursor(14,1);
lcd.print((char)223);
lcd.setCursor(0,1);
lcd.print("Temperature= C");
lcd.setCursor(14,1); //column,row
lcd.print((char)223);
delay(3000);
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);
}
if(!enablereading) {
lcd.setCursor(14,0);
lcd.print((char)223);
lcd.setCursor(8,0);
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);
}
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);
}
if((freezer<-26 && freezer<0) && digitalRead(defrostRelay) == LOW )
{digitalWrite( compressorRelay
, HIGH);}
if((freezer<-26 ))
{digitalWrite( compressorRelay
, LOW);}
if( Startdefrosting)
{
digitalWrite( compressorRelay ,LOW);
digitalWrite( defrostRelay,HIGH);
}
goto label;
}}
freezer
fridge
room
cooling
defrosting