The next stage in creating my three zone temperature controller with frost protection during silent hours. I have removed the previously include 'while' functions. The sytem now functions, however every three seconds it changes from normal control to frost protection and then back again. I do have two three second delays built into the sketch and presume as the loop continues, it reacts to these delays and then changes to the other temperature control function.
I have tried various ways of correcting this from using the 'while' function to attempting unsuccessfully to create sub routines.
I am hoping that someone might take the time to view the code and be kind enough to suggest what adjustment I should make.
// 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 to include frost protection.
// 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 = 25.0; // C
const float temperature_setpoint_airgun = 25.0; // C
const float frost_setpoint = 25.5; // C
const float deadzone = 0.75; // C
DallasTemperature sensor_range(&oneWire_range);
DallasTemperature sensor_club(&oneWire_club);
DallasTemperature sensor_airgun(&oneWire_airgun);
#define Sunday 0 // Unchangeable parameters
#define Monday 1
#define Tuesday 2
#define Wednesday 3
#define Thursday 4
#define Friday 5
#define Saturday 6
const int OnHour_Normal = 16; // Timeswitch settings
const int OnMin_Normal = 00;
const int OffHour_Normal = 21;
const int OffMin_Normal = 30;
const int OnHour_Sunday = 16;
const int OnMin_Sunday = 00;
const int OffHour_Sunday = 17;
const int OffMin_Sunday = 30;
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 (digitalRead(normal_heat_in) == HIGH)
// ;
// while (digitalRead(frost_heat_in) == HIGH)
// ;
}
//---------------------------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(") ");
}
//---------------------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 (digitalRead(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");
}
//------------------------------Date display--------------------------//
// void display() {
lcd.setCursor(0, 0);
lcd.print(daysOfTheWeek[now.dayOfTheWeek()]);
lcd.setCursor(3, 0);
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(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);
//-----------------------Temperature display - -----------------------//
{
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(3000);
// }
//----------------------Normal temperature control--------------------//
// while (digitalRead(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");
}
// }
{
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);
}
//------------------------------Date display--------------------------//
lcd.setCursor(0, 0);
lcd.print(daysOfTheWeek[now.dayOfTheWeek()]);
lcd.setCursor(3, 0);
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(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);
//-----------------------Temperature display - -----------------------//
{
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(3000);
}
}
}
3 posts - 2 participants