Back for a little more advice. I have created a sketch that will eventually provide pre-set timed thermistor control of three heating zones. I have the timed section now functioning correctly and the thermistor control section also functions correctly. I am trying to use a While function to ensure that either normal heating is provided or frost protection at a lower temperature at all other times.
At present the system is stuck in the frost protection mode. I have tried various changes to the While function, with no positive results.
// Author : John Marchant G0RJM
// Created : 25/01/2024
// Description : Display the current date, time and temperature on the 20x4 LCD screen and create a thermostatic heating control of three zones
// Inspired from this code : File => Examples => RTClib => ds3231 along with parts borrowed from several other files/sketches in collaboration with several other authors
#include <LiquidCrystal_I2C.h>
#include <Wire.h>
#include "RTClib.h"
#include <OneWire.h>
#include <DallasTemperature.h>
#define ONE_WIRE_BUS_1 3
#define ONE_WIRE_BUS_2 4
#define ONE_WIRE_BUS_3 5
OneWire oneWire_range(ONE_WIRE_BUS_1);
OneWire oneWire_club(ONE_WIRE_BUS_2);
OneWire oneWire_airgun(ONE_WIRE_BUS_3);
const byte range_relay = 9; // Relay outputs
const byte club_relay = 10;
const byte airgun_relay = 11;
const byte boiler_relay = 12;
const byte normal_heat_out = A0;
const byte normal_heat_in = A1;
const byte frost_heat_out = A2;
const byte frost_heat_in = A3;
float range_temperature;
float club_temperature;
float airgun_temperature;
const float temperature_setpoint_range = 20.0; // C
const float temperature_setpoint_club = 20.0; // C
const float temperature_setpoint_airgun = 20.0; // C
const float frost_setpoint = 24; // C
const float deadzone = 0.75; // C
DallasTemperature sensor_range(&oneWire_range);
DallasTemperature sensor_club(&oneWire_club);
DallasTemperature sensor_airgun(&oneWire_airgun);
// UNCHANGABLE PARAMATERS
#define Sunday 0
#define Monday 1
#define Tuesday 2
#define Wednesday 3
#define Thursday 4
#define Friday 5
#define Saturday 6
// byte Weekday[] = { 1, 2, 3, 4, 5, 6 };
// byte Sunday[] = { 0 };
const int OnHour_Normal = 16; // Timeswitch settings
const int OnMin_Normal = 14;
const int OffHour_Normal = 22;
const int OffMin_Normal = 30;
const int OnHour_Sunday = 12;
const int OnMin_Sunday = 00;
const int OffHour_Sunday = 18;
const int OffMin_Sunday = 00;
LiquidCrystal_I2C lcd(0X27, 20, 4);
RTC_DS3231 rtc;
char daysOfTheWeek[7][12] = { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" };
byte char_temp[8] = { B00100, B01010, B01010, B01110, B01110, B11111, B11111, B01110 }; // for thermometer icon
void setup() {
pinMode(range_relay, OUTPUT);
pinMode(club_relay, OUTPUT);
pinMode(airgun_relay, OUTPUT);
pinMode(boiler_relay, OUTPUT);
pinMode(normal_heat_out, OUTPUT);
pinMode(normal_heat_in, INPUT);
pinMode(frost_heat_out, OUTPUT);
pinMode(frost_heat_in, INPUT);
rtc.begin();
lcd.init();
lcd.backlight();
lcd.createChar(0, char_temp);
lcd.setCursor(4, 0);
lcd.print("Take it easy!");
lcd.setCursor(4, 1);
lcd.print("Working on it");
lcd.setCursor(2, 2);
lcd.print("G0RJM Three Zone");
lcd.setCursor(1, 3);
lcd.print("Temperature Control");
delay(3000);
lcd.clear();
if (rtc.lostPower()) {
// When the time needs to be set up on a new device, or after a power loss, uncomment the
// following line. This sets the RTC to the date & time this sketch was compiled
// rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
}
{
Serial.begin(9600);
Serial.println("G0RJM Three zone Temperature Control");
sensor_range.begin();
sensor_club.begin();
sensor_airgun.begin();
}
while (normal_heat_in == HIGH)
;
while (frost_heat_in == HIGH)
;
}
//------------------------------Temperature control-----------------------------//
void loop() {
DateTime now = rtc.now();
{
// Serial.print(now.year(), DEC);
// Serial.print('/');
Serial.print(now.month(), DEC);
Serial.print('/');
Serial.print(now.day(), DEC);
Serial.print(" (");
Serial.print(daysOfTheWeek[now.dayOfTheWeek()]);
Serial.print(") ");
// Serial.print(now.hour(), DEC);
// Serial.print(':');
// Serial.print(now.minute(), DEC);
// Serial.print(':');
// Serial.print(now.second(), DEC);
// Serial.println();
}
//-------------------Timed heating setting----------------------------//
if ((now.hour() >= OffHour_Normal) && (now.minute() >= OffMin_Normal) && (now.dayOfTheWeek() != Sunday)
|| (now.hour() >= OffHour_Sunday) && (now.minute() >= OffMin_Sunday) && (now.dayOfTheWeek() == Sunday)) {
digitalWrite(normal_heat_out, LOW);
digitalWrite(frost_heat_out, HIGH);
} else {
if ((now.hour() >= OnHour_Normal) && (now.minute() >= OnMin_Normal) && (now.dayOfTheWeek() != Sunday)
|| (now.hour() >= OnHour_Sunday) && (now.minute() >= OnMin_Sunday) && (now.dayOfTheWeek() == Sunday))
digitalWrite(normal_heat_out, HIGH);
digitalWrite(frost_heat_out, LOW);
}
//--------------------Frost protection control-----------------------//
while (frost_heat_in == HIGH) {
if ((sensor_range.getTempCByIndex(0) < (frost_setpoint - deadzone)) && (digitalRead(frost_heat_in == HIGH))) {
digitalWrite(range_relay, HIGH);
lcd.setCursor(15, 1);
lcd.print("ON F");
} else {
if ((sensor_range.getTempCByIndex(0) > (frost_setpoint + deadzone)) && (digitalRead(frost_heat_in == HIGH)))
digitalWrite(range_relay, LOW);
lcd.setCursor(15, 1);
lcd.print(" OFF");
}
if ((sensor_club.getTempCByIndex(0) < (frost_setpoint - deadzone)) && (digitalRead(frost_heat_in == HIGH))) {
digitalWrite(club_relay, HIGH);
lcd.setCursor(15, 2);
lcd.print("ON F");
} else {
if ((sensor_club.getTempCByIndex(0) > (frost_setpoint + deadzone)) && (digitalRead(frost_heat_in == HIGH)))
digitalWrite(club_relay, LOW);
lcd.setCursor(15, 2);
lcd.print(" OFF");
}
if ((sensor_airgun.getTempCByIndex(0) < (frost_setpoint - deadzone)) && (digitalRead(frost_heat_in == HIGH))) {
digitalWrite(airgun_relay, HIGH);
lcd.setCursor(15, 3);
lcd.print("ON F");
} else {
if ((sensor_airgun.getTempCByIndex(0) > (frost_setpoint + deadzone)) && (digitalRead(frost_heat_in == HIGH)))
digitalWrite(airgun_relay, LOW);
lcd.setCursor(15, 3);
lcd.print(" OFF");
}
}
//--------------------Normal temperature control--------------------//
// while (normal_heat_in == HIGH) {
if ((sensor_range.getTempCByIndex(0) < (temperature_setpoint_range - deadzone)) && (digitalRead(normal_heat_in == HIGH))) {
digitalWrite(range_relay, HIGH);
lcd.setCursor(15, 1);
lcd.print("ON T");
} else {
if ((sensor_range.getTempCByIndex(0) > (temperature_setpoint_range + deadzone)) && (digitalRead(normal_heat_in == HIGH)))
digitalWrite(range_relay, LOW);
lcd.setCursor(15, 1);
lcd.print(" OFF");
}
if ((sensor_club.getTempCByIndex(0) < (temperature_setpoint_club - deadzone)) && (digitalRead(normal_heat_in == HIGH))) {
digitalWrite(club_relay, HIGH);
lcd.setCursor(15, 2);
lcd.print("ON T");
} else {
if ((sensor_club.getTempCByIndex(0) > (temperature_setpoint_club + deadzone)) && (digitalRead(normal_heat_in == HIGH)))
digitalWrite(club_relay, LOW);
lcd.setCursor(15, 2);
lcd.print(" OFF");
}
if ((sensor_airgun.getTempCByIndex(0) < (temperature_setpoint_airgun - deadzone)) && (digitalRead(normal_heat_in == HIGH))) {
digitalWrite(airgun_relay, HIGH);
lcd.setCursor(15, 3);
lcd.print("ON T");
} else {
if ((sensor_airgun.getTempCByIndex(0) > (temperature_setpoint_airgun + deadzone)) && (digitalRead(normal_heat_in == HIGH)))
digitalWrite(airgun_relay, LOW);
lcd.setCursor(15, 3);
lcd.print(" OFF");
}
// }
//lcd.setCursor(column,row);
//------------------------------Date display-----------------------------//
lcd.setCursor(0, 0);
lcd.print(daysOfTheWeek[now.dayOfTheWeek()]);
lcd.setCursor(3, 0);
// lcd.print("Date:");
lcd.print(":");
lcd.setCursor(4, 0);
if (now.day() <= 9) {
lcd.print("0");
lcd.setCursor(5, 0);
lcd.print(now.day(), DEC);
} else {
lcd.print(now.day(), DEC);
}
lcd.setCursor(6, 0);
lcd.print(":");
lcd.setCursor(7, 0);
if (now.month() <= 9) {
lcd.print("0");
lcd.setCursor(8, 0);
lcd.print(now.month(), DEC);
} else {
lcd.print(now.month(), DEC);
}
lcd.setCursor(9, 0);
lcd.print(":");
lcd.setCursor(10, 0);
lcd.print(now.year(), DEC);
//------------------------------Time display-----------------------------//
// lcd.setCursor(0, 1);
// lcd.print("Time:");
lcd.setCursor(15, 0);
if (now.hour() <= 9) {
lcd.print("0");
lcd.setCursor(16, 0);
lcd.print(now.hour(), DEC);
} else {
lcd.print(now.hour(), DEC);
}
lcd.setCursor(17, 0);
lcd.print(":");
lcd.setCursor(18, 0);
if (now.minute() <= 9) {
lcd.print("0");
lcd.setCursor(19, 0);
lcd.print(now.minute(), DEC);
} else {
lcd.print(now.minute(), DEC);
}
lcd.setCursor(17, 0);
lcd.print(":");
lcd.setCursor(18, 0);
// if (now.second() <=9 )
// {
// lcd.print("0");
// lcd.setCursor(13,1);
// lcd.print(now.second(),DEC);
// }
// else {lcd.print(now.second(),DEC);}
//------------------------------Temperature display - -----------------------------//
{
Serial.print("Requesting temperatures...");
sensor_range.requestTemperatures();
sensor_club.requestTemperatures();
sensor_airgun.requestTemperatures();
Serial.println(" done");
Serial.print("Range: ");
Serial.println(sensor_range.getTempCByIndex(0));
Serial.print("Club: ");
Serial.println(sensor_club.getTempCByIndex(0));
Serial.print("Airgun: ");
Serial.println(sensor_airgun.getTempCByIndex(0));
Serial.print("Frost In ");
Serial.println(frost_heat_in);
Serial.print("Heat In ");
Serial.println(normal_heat_in);
}
{
lcd.setCursor(0, 1); // Range
lcd.print("Range:");
lcd.setCursor(7, 1);
// lcd.print(rtc.getTemperature());
lcd.print(sensor_range.getTempCByIndex(0));
lcd.setCursor(12, 1);
lcd.write((char)223);
lcd.setCursor(13, 1);
lcd.print("C");
// lcd.setCursor(18, 1);
// lcd.print(char(0));
lcd.setCursor(0, 2); // Clubroom
lcd.print("Club:");
lcd.setCursor(7, 2);
// lcd.print(rtc.getTemperature());
lcd.print(sensor_club.getTempCByIndex(0));
lcd.setCursor(12, 2);
lcd.write((char)223);
lcd.setCursor(13, 2);
lcd.print("C");
// lcd.setCursor(14, 2);
// lcd.print(" ");
// lcd.print(char(0));
lcd.setCursor(0, 3); // Airgun Range
lcd.print("Airgun:");
lcd.setCursor(7, 3);
// lcd.print(rtc.getTemperature());
lcd.print(sensor_airgun.getTempCByIndex(0));
lcd.setCursor(12, 3);
lcd.write((char)223);
lcd.setCursor(13, 3);
lcd.print("C");
// lcd.setCursor(18, 3);
// lcd.print(char(0));
delay(5000);
}
}