Hi Pete,
Thank you for showing interest.
Had to remove some stuff that isn't relevant but I'm afraid this is still gonna be a long read....
#include <Wire.h> // Comes with Arduino IDE
#include <LiquidCrystal_I2C.h>
#include <OneWire.h>
#include <DallasTemperature.h>
/*-----( Declare Constants )-----*/
const int buttonsPin = A0;
const int fireplacehotPin = A3;
const int bypassPin = 4;
const int pumpPin = 5;
const int solarvalvePin = 6;
const int mainvalvePin = 7;
const int heaterPin = 8;
const int boilervalvePin = 9;
const int overtempvalvePin = 10;
const int fireplacevalvePin = 11;
/*-----( Declare objects )-----*/
OneWire oneWire (2); //create onewire object
// set the LCD address to 0x27 for a 20 chars 4 line display
// Set the pins on the I2C chip used for LCD connections:
// addr, en,rw,rs,d4,d5,d6,d7,bl,blpol
LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE); // Set the LCD I2C address
DallasTemperature sensors(&oneWire);
float TempIn1C; //room temperature
float TempIn2C; //solar temperature
float TempIn3C; //system temperature
float TempIn4C; //boiler output
//float TempIn5C; //heat exchanger not present
/*-----( Declare Variables )-----*/
byte downbutton = false;
byte enterbutton = false;
byte upbutton = false;
byte boilerstate = false;
byte fireplace = false;
byte heaterState = false;
byte solarState = false;
boolean timerStart = false;
boolean idle = true;
boolean overtemp = false;
int buttonValue = 0;
int fireplacehot = 0;
int menuValue = 2;
float setTemp = 20.5;
int deltaT = 10;
int minT = 30;
unsigned long timer = 0;
unsigned long oldtimer = 0;
unsigned long elapsed = 0;
void setup() /*----( SETUP: RUNS ONCE )----*/
{
sensors.begin();
lcd.begin(20,4);
for(int i = 0; i< 3; i++)
{
lcd.backlight();
delay(250);
lcd.noBacklight();
delay(250);
}
//start with outputs off//
digitalWrite (bypassPin, HIGH);
digitalWrite (pumpPin, HIGH);
digitalWrite (solarvalvePin, HIGH);
digitalWrite (mainvalvePin, HIGH);
digitalWrite (heaterPin, HIGH);
digitalWrite (boilervalvePin, HIGH);
digitalWrite (overtempvalvePin, HIGH);
digitalWrite (fireplacevalvePin, HIGH);
//define pins //
pinMode (bypassPin, OUTPUT);
pinMode (pumpPin, OUTPUT);
pinMode (solarvalvePin, OUTPUT);
pinMode (mainvalvePin, OUTPUT);
pinMode (heaterPin, OUTPUT);
pinMode (boilervalvePin, OUTPUT);
pinMode (overtempvalvePin, OUTPUT);
pinMode (fireplacevalvePin, OUTPUT);
lcd.backlight(); // finish with backlight on
//-------- Write initial characters on the display --------
// NOTE: Cursor Position: Lines and Characters start at 0
lcd.setCursor(1,0); //Start at character 1 on line 0
lcd.print("solar " );
lcd.print(" ");
lcd.print("sys");
lcd.setCursor(0,1);
lcd.print("*heating");
lcd.print(" set ");
lcd.print (setTemp,1);
lcd.setCursor(1,2);
lcd.print("boiler");
lcd.setCursor(12,2);
lcd.print("now");
lcd.setCursor(1,3);
lcd.print("fireplace");
}/*--(end setup )---*/
void loop()
{
timer = millis(); //start counting milliseconds
if (timerStart == false)
oldtimer = timer; //prepare for later use
else
elapsed = timer - oldtimer; //calculate elapsed time
if (elapsed >= 6000) //timeout reached
{timerStart = false;
elapsed = 0; //return to default menu value
menuValue = 2; //cleanup display
lcd.setCursor(0,0); //remove * line 0
lcd.print (" ");
lcd.setCursor(0,2); //remove * line 2
lcd.print (" boiler");
lcd.setCursor(0,3); //remove * line 3
lcd.print (" ");
lcd.setCursor(0,1); //write * line 1
lcd.print ("*heating");
lcd.setCursor(10,3);
if (fireplace == true)
lcd.print (" ON ");
else
lcd.print (" OFF");
lcd.setCursor(8,2);
if (boilerstate == true)
lcd.print (" ON");
else
lcd.print ("OFF");
lcd.setCursor(6,0); //remove solar entries
lcd.print (" sys ");}
sensors.requestTemperatures();
//here I remove everything concerning text on the LCD
//the complete sketch is much longer than allowed here
}
lcd.setCursor (19,3);//put a cross in the last display position.
lcd.print ("X");
TempIn1C = sensors.getTempCByIndex(0);
TempIn2C = sensors.getTempCByIndex(1);
TempIn3C = sensors.getTempCByIndex(2);
TempIn4C = sensors.getTempCByIndex(3);
//TempIn5C = sensors.getTempCByIndex(4);//not (yet) installed
lcd.setCursor (19,3);
lcd.print ("+");
/*Here the code starts that actually makes
decisions based on settings and temperatures*/
if (fireplace == true) //open valve and start pump
{
digitalWrite (fireplacevalvePin, LOW); //pin 11
digitalWrite (pumpPin, LOW); //pin 5
lcd.setCursor (16,3);
lcd.print ("P");}
if (fireplace == false && overtemp == false && solarState == false)
{lcd.setCursor (16,3);
lcd.print (" ");
digitalWrite (fireplacevalvePin, HIGH);}
fireplacehot = analogRead (fireplacehotPin);
if (fireplacehot < 500 && fireplace == false)
{fireplace = true;
fireplacewarning ();
}
//conditions for the solar collector to operate
lcd.setCursor (15,3);
if (TempIn2C >= minT && TempIn2C >= TempIn3C + deltaT)
solarState = true;
else
solarState = false;
if (solarState == true) //solar collector must be enabled
{digitalWrite (solarvalvePin, LOW);
lcd.print ("S");
if (heaterState == false)//start pump when heating = off
{delay(2000);
lcd.setCursor (16,3);
digitalWrite (pumpPin, LOW);
lcd.print ("P");}}
if (solarState == false)
{digitalWrite (solarvalvePin, HIGH);
lcd.setCursor (15,3);
lcd.print (" ");}
if (fireplace == false && solarState == false)
{digitalWrite (pumpPin, HIGH); //no pump required
lcd.setCursor (16,3);
lcd.print (" ");
}
//conditions for the boiler to operate
lcd.setCursor (17,3);
if (boilerstate == true)
digitalWrite (bypassPin, LOW); //tapwater from boiler
else
digitalWrite (bypassPin, HIGH); //tapwater from CV
if (boilerstate == true && TempIn3C >= TempIn4C + 5.0)
{digitalWrite (boilervalvePin, LOW);
lcd.print ("B");}
else
{digitalWrite (boilervalvePin, HIGH);
lcd.print (" ");}
//start heating when room temp is below settemp
if (idle == true && TempIn1C < setTemp)
startHeating(); //heater was previously off
//continue heating until 0.1C above settemp
if (idle == false && TempIn1C >= setTemp + 0.1)
stopHeating(); //heater was previously on
//terminate heating when fireplace is used
if (idle == false && fireplace == true)
stopHeating ();
//what to do in case of overheating
if (heaterState == false && TempIn3C >= 85.0)
{overtemp = true;
digitalWrite (fireplacevalvePin, LOW);
digitalWrite (overtempvalvePin, LOW);
digitalWrite (pumpPin, LOW);
lcd.setCursor (18,3);
lcd.print ("!");}
if (overtemp == true && TempIn3C < 85.0)
{digitalWrite (fireplacevalvePin, HIGH);
digitalWrite (overtempvalvePin, HIGH);
digitalWrite (pumpPin, HIGH);
overtemp = false;
lcd.setCursor (18,3);
lcd.print (" ");}
}
//conditions for normal heating
void startHeating()
{if (fireplace == true) //skip operation
return;
digitalWrite (mainvalvePin, LOW);//open valve
delay(4000);
digitalWrite (heaterPin, LOW);//start heating
idle = false;
heaterState = true;
lcd.setCursor (18,3);
lcd.print ("H");}
void stopHeating()
{
digitalWrite (heaterPin, HIGH);//stop heating
delay(10000); //allow heat to disperse
digitalWrite (mainvalvePin, HIGH);//close valve
idle = true;
heaterState = false;
lcd.setCursor (18,3);
lcd.print (" ");}
void fireplacewarning()
{ for(int i = 0; i< 100; i++)
{
lcd.backlight();
delay(100);
lcd.noBacklight();
delay(100);
}
lcd.backlight();
}