Thanks for the response. And thanks for the tip on the code block.
Yes, the mistake was in the coment where the sign was changed.
I want the pump to turn on when the roomT < RoomTempSP and turn off when the temp is satisfied: roomT > (RoomTempSP+2), with that 2 being the offset so it doesn’t keep flipping.
Thanks for catching that. So the question now is about whether I have the syntax correct for the conditional statement, that the parentheses are properly placed so that all the conditions are considered, especially the && and ||.
I forgot to mention that it publishes to thingSpeak. You can see thecurrent live data feed there at Solar thermal system data
Here is the whole code of the controller:
// Solar Thermal Radiant Floor Controller, Drainback system
#include <Adafruit_DHT.h>
#include <OneWire.h>
#include <Serial_LCD_SparkFun.h>
#include <spark-dallas-temperature.h>
//System Operational Parameters
#define PanelOnTemp 90 // Panels must be this hot to turn on
#define RoomTempSP 69 // Room temperature setpoint
#define PanelOffDifferential 3 // If Panels are only this much warmer than return temp, turn off
#define FREEZE_LIMIT 10 // don't operate if outside temp is below this temperature
#define Print_Delay 4000 // delay in between LCD printouts
#define flowCalibrationFactor 4.5 // how many pulses per gallon
#define panelHot 175 // if panels are hotter than this do not turn on pump
#define DHTTYPE DHT22 // DHT 22 (AM2302)
// DHT22 sensor pinout:
// Pin 1 (on the left): +3.3V
// Pin 2: output
// Pin 4 (on the right): GROUND
#define DHT_SENSOR_PIN D7 //room Temp
DHT dht(DHT_SENSOR_PIN, DHTTYPE);
//
#define Relay D3
#define buttonPin A2
#define PT1000SensorInput A1
#define flowSensor D2
// DS18b20 setup
//one wire bus is D4, can connect multiple DS1820s here
OneWire oneWire(D4 );
// Pass our oneWire reference to Dallas Temperature.
DallasTemperature dallas(&oneWire);
// Create a variables
int rawPanel12bit;
int panelT = 0; //PT1000
int supplyT = 0; //ds1820
int returnT = 0; //ds1820
int osaTemp = 0; //ds1820
int btus = 0;
int roomT = 72;
float flowRate = 0.9;
int pulseCount = 0;
int buttonState = 0; // variable for reading the pushbutton status
float flowGPM = 0;
bool solarFlag = false; //flag for pump startup
int buttonPressFlag = 0;
unsigned long lastTime = 0;
unsigned long lastTime2 = 0;
unsigned long publish_delay=600000;
unsigned long publish_delay_test=30000;
//LCD
Serial_LCD_SparkFun lcd = Serial_LCD_SparkFun();
//
void setup() {
// initialize some stuff
pinMode(Relay, OUTPUT);
pinMode(buttonPin, INPUT);
digitalWrite(Relay, LOW);
pinMode(flowSensor, INPUT);
digitalWrite(flowSensor, HIGH);
//digitalWrite(DHT_5V_PIN, HIGH);
rawPanel12bit = analogRead(PT1000SensorInput);
attachInterrupt(buttonPin, buttonPressInterrupt, RISING);
// Initialize DHT22 sensor
dht.begin();
delay (2000);
}
void loop()
{
// read PT1000 sensor input
rawPanel12bit = analogRead(PT1000SensorInput);
//adjust raw value
panelT = (rawPanel12bit*rawPanel12bit)*0.000264+(rawPanel12bit*0.614173)-396.53;
//DS1820 read supply and Return temps
dallas.begin();
dallas.requestTemperatures();
// get the temperature in Celcius
float sat = dallas.getTempCByIndex(0);
float rat = dallas.getTempCByIndex(1);
float osat = dallas.getTempCByIndex(2);
// convert to double
supplyT = (double)sat;
returnT = (double)rat;
osaTemp = (double)osat;
// convert to Fahrenheit
float tempFs = DallasTemperature::toFahrenheit( sat );
float tempFr = DallasTemperature::toFahrenheit( rat );
float tempOSAT = DallasTemperature::toFahrenheit( osat );
// convert to double
supplyT = (int)tempFs;
returnT = (int)tempFr;
osaTemp = (int)tempOSAT;
//DHT22 read
double temperature = dht.getTempCelcius();
//double humidity = dht.getHumidity();
int tempF = (temperature * 1.8) + 32;
if(tempF!=0)
{
roomT= tempF;
}
btus = (supplyT-returnT)*500*flowRate;
//Particle publish to thingspeak
unsigned long now = millis();
if ((now - lastTime) >= publish_delay)
{
// field 1 = roomtemp
// field 2 = supplytemp
// field 3 = returntemp
// field 4 = paneltemp
// field 5 = btus
// field 6 = osatemp
// field 7 = pump
char json[256];
snprintf(json, sizeof(json), "{\"roomtemp\":\"%i\",\"supplytemp\":\"%i\",\"returntemp\":\"%i\",\"paneltemp\":\"%i\",\"btus\":\"%i\",\"osatemp\":\"%i\",\"pump\":\"%d\"}", roomT, supplyT ,returnT, panelT, btus, osaTemp,solarFlag);
Particle.publish("solarTemps", json, PRIVATE);
lastTime = now;
}
// Display values
// clear the display first
lcd.clear();
if(buttonPressFlag == 1)
{
Serial1.print("Override:");
lcd.selectLine(2);
Serial1.print(" Pump OFF");
digitalWrite(Relay, LOW);
}
if(buttonPressFlag == 2)
{
Serial1.print("Override: ");
lcd.selectLine(2);
Serial1.print(" Pump ON");
digitalWrite(Relay, HIGH);
}
if(buttonPressFlag == 4)
{
Serial1.print("Failure: ");
lcd.selectLine(2);
Serial1.print(" No Flow");
}
if(buttonPressFlag == 0)
{ //display data and run control program only when button is in normal mode (0)
lcd.clear();
if(solarFlag==false)
{
digitalWrite(Relay, LOW);
}
else
{
digitalWrite(Relay, HIGH);
}
// We have to print to Serial1
Serial1.print("Panel Temp: ");
Serial1.print(panelT);
//
lcd.selectLine(2);
Serial1.print("Room Temp: ");
Serial1.print(roomT);
delay(Print_Delay);
lcd.clear();
//SAT and Rat on one screen
Serial1.print("Supply Temp: ");
Serial1.print(supplyT);
//RAT
lcd.selectLine(2);
Serial1.print("Return Temp: ");
Serial1.print(returnT);
delay(Print_Delay);
lcd.clear();
//btus
Serial1.print("BTUs: ");
Serial1.print(btus);
//
lcd.selectLine(2);
Serial1.print("flag: ");
Serial1.print(solarFlag);
delay(Print_Delay);
lcd.clear();
//Control sequence - does not run when in override mode from button press
if(solarFlag == false && (panelT > PanelOnTemp) && (roomT < (RoomTempSP)) && (panelT < panelHot)) // if panel is hot but not too hot, rm temp is below SP,then turn on pump
{
solarFlag = true;
}
if ((now - lastTime) >= publish_delay) //wait for system "on" to stabilize before checking for "off" conditions, check every ten minutes
{
if (solarFlag == true) //if pump is already running
{
if (supplyT < (returnT + PanelOffDifferential) || roomT > (RoomTempSP+2)) //if supplyT drops below (returnT plus diff) or roomT < (sp+2)
{
solarFlag = false;
lastTime=now;
}
}
}
/*if (flowRate < 0.2)
{ // if no flow then shut down pump
digitalWrite(Relay, LOW);
buttonPressFlag = 4;
}*/
}
}
void buttonPressInterrupt()
{ //button interrupt routine
static unsigned long last_interrupt_time = 0;
unsigned long interrupt_time = millis();
if (interrupt_time - last_interrupt_time > 50) // debounce time = 50milliseconds
{
buttonPressFlag = buttonPressFlag +1;
}
last_interrupt_time = interrupt_time;
if (buttonPressFlag >2)
buttonPressFlag = 0;
}