Hello
Sorry about that, here is the whole code, i have also add 2 extra ouputs that interlock together
The problem seems to occur when the currentMillis = millis() is at the start of the code underneath the sensor reading function.
Thanks
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include "RTClib.h"
RTC_DS3231 rtc;
char daysOfTheWeek[7][12] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
LiquidCrystal_I2C lcd(0x3F, 20, 4);
//----Defines----
#define TempMIN 20
#define TempMAX 80
#define TempX1 0
#define TempY1 80
#define TempX2 20
#define TempY2 20
//----Inputs----
int Temp1_AI = A2;
int Temp2_AI = A3;
//----Outputs----
int Output1 = 2;
int Output2 = 3;
int Output3 = 4;
//----Varibles----
//---Varible to store Sensor Reading
int Sens1 = 0;
int Sens2 = 0;
//---Varible to store Calculations/misc
int Req_Flow = 20; //Required Flow Temperature
int Req_flow_lower = 3; //Required Flow Temperature lower hysteresis
int Req_flow_Upper = 3; //Required Flow Temperature uppper hysteresis
int TempSlope; // Vt Slop Gradient
int Heat_Demand = 0; // Heat demand Status
unsigned long currentMillis = 0;
//----------Output Variable----------
int Output1_Ready = 0;
int Output1_Status = 0;
int check1 = 0;
unsigned long Output1Millis = 0;
long Output1ON = 10000;
long Output1OFF = 10000;
int Output2_Ready = 0;
int Output2_Status = 0;
int check2 = 0;
unsigned long Output2Millis = 0;
long Output2ON = 1000;
long Output2OFF = 10000;
int Output3_Ready = 0;
int Output3_Status = 0;
int check3 = 0;
unsigned long Output3Millis = 0;
long Output3ON = 1000;
long Output3OFF = 1000;
void setup() {
lcd.init();
lcd.backlight();
#ifndef ESP8266
while (!Serial); // for Leonardo/Micro/Zero
#endif
//----Pin Setup----
Serial.begin(9600);
pinMode(2, OUTPUT); // Pin setup
pinMode(3, OUTPUT);
pinMode(4, OUTPUT);
digitalWrite(2, HIGH); // Set All outputs off
digitalWrite(3, HIGH); //
digitalWrite(4, HIGH); //
if (! rtc.begin()) {
Serial.println("Couldn't find RTC");
while (1);
}
if (rtc.lostPower()) {
Serial.println("RTC lost power, lets set the time!");
rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
}
}
void loop()
{
//----Input Readings----
Sens1= Sensors(Temp1_AI);
Sens2 = Sensors(Temp2_AI);
//currentMillis = millis(); // moved from here corrected the problem
DateTime now = rtc.now();
Req_Flow = Heating_Temp(Sens1,TempX1,TempY1,TempX2,TempY2,TempMIN,TempMAX);
Heat_Demand = HeatDemand(Sens2, Req_Flow, Req_flow_lower, Req_flow_Upper, Heat_Demand);
//----Output1
if (Heat_Demand == 0 && check1 == 0) // no demand, and the time hasnt been recorded
{
Output1_Ready = 0; // Ouput can be turned off
check1 = 1; // Says the time has been recorded
Output1Millis = millis(); // record the time
}
else if (Heat_Demand == 1 && check1 == 0) // Same as above but for when there is a demand
{
Output1_Ready = 1;
check1 = 1;
Output1Millis = millis();
}
//----Output2
if (Heat_Demand == 0 && check2 == 0 && Output1_Status == 0) // no demand, and the time hasnt been recorded
{
Output2_Ready = 0; // Ouput can be turned off
check2 = 1; // Says the time has been recorded
Output2Millis = millis(); // record the time
}
else if (Heat_Demand == 1 && check2 == 0 && Output3_Status == 1) // Same as above but for when there is a demand
{
Output2_Ready = 1;
check2 = 1;
Output2Millis = millis();
}
//----Output 3------
if (Heat_Demand == 0 && check3 == 0 && Output2_Status == 0) // no demand, and the time hasnt been recorded
{
Output3_Ready = 0; // Ouput can be turned off
check3 = 1; // Says the time has been recorded
Output3Millis = millis(); // record the time
}
else if (Heat_Demand == 1 && check3 == 0) // Same as above but for when there is a demand
{
Output3_Ready = 1;
check3 = 1;
Output3Millis = millis();
}
//------Output Table
currentMillis = millis();
if ((currentMillis - Output1Millis >= Output1ON) && Output1_Ready == 1)
{
Output1_Status = 1; // state that the output is on
digitalWrite(Output1, LOW); // switch the relay on
check1 = 0; // reset the value so the time can be recorded again
}
else if ((currentMillis - Output1Millis >= Output1OFF) && Output1_Ready == 0)
{
Output1_Status = 0; // state that the output is off
digitalWrite(Output1, HIGH); // switch the relay off
check1 = 0; // reset the value so the time can be recorded again
}
if ((currentMillis - Output2Millis >= Output2ON) && Output2_Ready == 1)
{
Output2_Status = 1; // state that the output is on
digitalWrite(Output2, LOW); // switch the relay on
check2 = 0; // reset the value so the time can be recorded again
}
else if ((currentMillis - Output2Millis >= Output2OFF) && Output2_Ready == 0)
{
Output2_Status = 0; // state that the output is off
digitalWrite(Output2, HIGH); // switch the relay off
check2 = 0; // reset the value so the time can be recorded again
}
if ((currentMillis - Output3Millis >= Output3ON) && Output3_Ready == 1)
{
Output3_Status = 1; // state that the output is on
digitalWrite(Output3, LOW); // switch the relay on
check3 = 0; // reset the value so the time can be recorded again
}
else if ((currentMillis - Output3Millis >= Output3OFF) && Output3_Ready == 0)
{
Output3_Status = 0; // state that the output is off
digitalWrite(Output3, HIGH); // switch the relay off
check3 = 0; // reset the value so the time can be recorded again
}
lcd.setCursor(0,0);
lcd.print("S1=");
lcd.print(Sens1);
lcd.print(" ");
lcd.setCursor(7,0);
lcd.print("Req.=");
lcd.print(Req_Flow);
lcd.print(" ");
lcd.setCursor(15,0);
lcd.print("F=");
lcd.print(Sens2);
lcd.print(" ");
if (Output1_Status == 1)
{
lcd.setCursor(0,1);
lcd.print("1 ON ");
}
else if (Output1_Status == 0)
{
lcd.setCursor(0,1);
lcd.print("1 OFF");
}
if (Output2_Status == 1)
{
lcd.setCursor(6,1);
lcd.print("2 ON ");
}
else if (Output2_Status == 0)
{
lcd.setCursor(6,1);
lcd.print("2 OFF");
}
if (Output3_Status == 1)
{
lcd.setCursor(11,1);
lcd.print("3 ON ");
}
else if (Output3_Status == 0)
{
lcd.setCursor(11,1);
lcd.print("3 OFF");
}
if (Heat_Demand == 0)
{
lcd.setCursor(5,2);
lcd.print(" ");
}
else if (Heat_Demand == 1)
{
lcd.setCursor(5,2);
lcd.print("Heat");
}
}
int Sensors(int pin)
{
int reading = 0;
reading = analogRead(pin);
reading = map(reading, 0, 1023, -10, 80);
return reading;
}
int Heating_Temp(int Outside, int x1,int y1,int x2,int y2, int MIN, int MAX)
{
int temp = 0;
temp = ((y2 - y1)/(x2 - x1)) * Outside + y1;
if (temp <= MIN)
{
temp = MIN;
}
else if(temp >= MAX)
{
temp = MAX;
}
return temp;
}
int HeatDemand (int Flow, int ReqFlow,int Lower,int Upper, int Demand)
{
if (Flow < (ReqFlow - Lower) )
{
Demand = 1;
}
else if (Flow >= (ReqFlow + Upper))
{
Demand = 0;
}
return Demand;
}