Help with thermostatically controlled relays

I’m having a lot of trouble controlling the relays based on time and temperature. from 8AM to 8PM I want the temp around 90F, turn light off at 92F and back on at 88F. From 8PM to 8AM I want the temp around 80F, turn the light off at 82F and back on at 78F.

I’m using a Arduino Mega 2560, Ethernet shield, 4X20 LCD screen DC3231 RTC, (4X) 10K Thermistors and (2X) relays. LCD connections RS-D12,E-11, D4-D5,D5-D4,D6-D3,D7-D2. thermistors connections A4,A5,A6,A7. RCT connections SDA-SDA D20, SCL-SCL D21. relay connections D8,D9.

code first half…

#include <math.h>
#include <Wire.h>
#include <DS3232RTC.h>
#include <Time.h>  
#include <LiquidCrystal.h>
#include <SPI.h>
#include <Ethernet.h>
#include <Arduino.h>
#define HR1224 6
#define DS3232RTC.h
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
byte mac[] = 
{
  0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED
};
IPAddress ip(192, 168, 1, 177);
EthernetServer server(80);
int RelayL = 8;//Relay Large Tank
int RelayS = 9;//Relay Small Tank
const int HTempSensorPin0 = 4;// A4 Hot Side Large Tank
const int CTempSensorPin2 = 6;// A6 Cold Side Large Tank
const int HTempSensorPin1 = 5;// A5 Hot Side Small Tank
const int CTempSensorPin3 = 7;// A7 Cold Side Small Tank
int     hourFormat12();   
int day();
int month();
int year();
double mytempC4;
double mytempF4;
double mytempC6;
double mytempF6;
double mytempC5;
double mytempF5;
double mytempC7;
double mytempF7;
void setup()
{
  Serial.begin(9600);
  while (!Serial) 
  {
    ;
  }
  SPI.begin();
  Ethernet.begin(mac, ip);
  server.begin();
  Serial.print("server is at ");
  Serial.println(Ethernet.localIP());
  pinMode(RelayL,OUTPUT);  
  pinMode(RelayS,OUTPUT);
  digitalWrite(RelayL,LOW);
  digitalWrite(RelayS,LOW);
  lcd.begin(20, 4);
  lcd.setCursor(4, 1);
  lcd.print("Loading...");
  lcd.setCursor(4, 2);
  lcd.print("Please Wait");
  {
    delay(1000);
  }
  {
    setSyncProvider(RTC.get);   
    if (timeStatus() != timeSet)
    Serial.println("Unable to Sync with RTC");
    else
    Serial.println("RTC has set the system time");
  }
  {
    lcd.clear();
  }
  lcd.begin(20, 4);
  lcd.setCursor(4,0);
  lcd.print("Temps loaded");
  lcd.setCursor(0,1);
  if (timeStatus() != timeSet)
  lcd.print("Unable to Sync with RTC");
  else
  lcd.print("RTC set system time");
  lcd.setCursor(7, 2);
  lcd.print("Done");
  {
    delay(700);
  }
  {
    lcd.clear();
  }
}
double Thermister(int RawADC) 
{
  double Temp;
  Temp = log(((10240000/RawADC) - 10000));
  Temp = 1 / (0.001129148 + (0.000234125 * Temp) + (0.0000000876741 * Temp * Temp * Temp));
  Temp = Temp - 273.15;
  return Temp;
}

byte countr = 1;
void loop()
{
  {
  EthernetClient client = server.available();
  if (client) 
  {
    Serial.println("new client");
    boolean currentLineIsBlank = true;
    while (client.connected()) 
    {
      if (client.available()) 
      {
        char c = client.read();
        Serial.write(c);
        if (c == '\n' && currentLineIsBlank) 
        {
          client.println("HTTP/1.1 200 OK");
          client.println("Content-Type: text/html");
          client.println("Connection: close"); 
          client.println("Refresh: 5");
          client.println();
          client.println("<!DOCTYPE HTML>\n");
          client.println("<html>\n");
          mytempC4 = Thermister(analogRead(4));
          mytempF4 = (mytempC4 * 1.8) + 32.0;
          mytempC6 = Thermister(analogRead(6));
          mytempF6 = (mytempC6 * 1.8) + 32.0;
          mytempC5 = Thermister(analogRead(5));
          mytempF5 = (mytempC5 * 1.8) + 32.0;
          mytempC7 = Thermister(analogRead(7));
          mytempF7 = (mytempC7 * 1.8) + 32.0;
          client.println("<head>");
          client.println("Snake Temps");
          client.println("</head>");
          client.println("<title>");
          client.println("Snake Temps");
          client.println("</title>");
          client.println("<body bgcolor=black>");
          client.println("<font color=white>");
          client.println("<meta http-equiv=\"refresh\" content=\"5\">"); 
          client.println("<center>");                                      
          client.println("<b>");          
          client.println("<td align=center>");
          client.println("<font color = blue size = 15>");
          client.println("Snake Temps");
          client.println("
");
          client.println("<table>");
          client.println("<td align=center>");
          client.println("<font color = blue size = 5>");
          client.println(hourFormat12());
          client.print(':');
          client.print(minute());
          client.print(' ');
          if (isAM())
          {
            client.print("AM");
          }
          if (isPM())
          {
            client.print("PM");
          }
          client.print(' ');
          client.print(month());
          client.print('/');
          client.print(day());
          client.print('/');
          client.print(year());
          client.print(' ');
          client.println("<table>");
          client.println("<td align=left>");
          client.println("<font color = white size = 4>");
          client.println("
");
          client.println(" Large Tank,");
          client.println("
");
          client.println("Hot Side:");
          client.print(mytempC4);
          client.print("C ");
          client.print(' ');
          client.print("- ");
          client.print(' ');
          client.print(mytempF4);
          client.print("F ");
          client.println("
");        
          client.print("Cold Side:");
          client.print(mytempC6);
          client.print("C ");
          client.print(' ');
          client.print("- ");
          client.print(' ');
          client.print(mytempF6);
          client.print("F ");      
          client.println("<table>");
          client.println("<td align=left>");
          client.println("<font color = white size = 4>");
          client.println("
");
          client.println(" Small Tank,");
          client.println("
");
          client.println("Hot Side:");
          client.print(mytempC5);
          client.print("C ");
          client.print(' ');
          client.print("- ");
          client.print(' ');
          client.print(mytempF5);
          client.print("F ");
          client.println("
");        
          client.print("Cold Side:");
          client.print(mytempC7);
          client.print("C ");
          client.print(' ');
          client.print("- ");
          client.print(' ');
          client.print(mytempF7);
          client.print("F ");       
          client.println("");
          client.println("</td>");
          client.println("</tr>");
          client.println("</table>");
          client.println("<p>");
          client.println("<FORM>");
          client.println("<INPUT type=button value=Refresh onClick=window.location='/?reconnect\'>");
          client.println("</FORM>");   
          client.println("<FORM>");    
          client.println("</html>");
          break;
        }
        if (c == '\n') 
        {
          currentLineIsBlank = true;
        } 
        else if (c != '\r') 
        {
          currentLineIsBlank = false;
        }
      }

second half…

 }
    delay(1);
    client.stop();
    Serial.println("client disconnected");
    Ethernet.maintain();
  }
}
  {
  char buffer[10];
  time_t t = now();     
  sprintf(buffer, "%02d:%02d:%02d", hourFormat12(t), minute(t), second(t));
  mytempC4 = Thermister(analogRead(4));
  mytempF4 = (mytempC4 * 1.8) + 32.0;
  mytempC6 = Thermister(analogRead(6));
  mytempF6 = (mytempC6 * 1.8) + 32.0;
  mytempC5 = Thermister(analogRead(5));
  mytempF5 = (mytempC5 * 1.8) + 32.0;
  mytempC7 = Thermister(analogRead(7));
  mytempF7 = (mytempC7 * 1.8) + 32.0;         
  Serial.println(buffer);
  Serial.print(month());
  Serial.print("/");
  Serial.print(day());
  Serial.print("/");
  Serial.print(year());
  Serial.println(" ");
  Serial.println(" ");
  Serial.println("Hot Side Large Tank");
  Serial.print(mytempC4);
  Serial.print("C");
  Serial.print(' ');
  Serial.print(mytempF4);
  Serial.print("F");
  Serial.println(" ");
  Serial.println(" ");
  Serial.println("Cold Side Large Tank");
  Serial.print(mytempC6);
  Serial.print("C");
  Serial.print(' ');
  Serial.print(mytempF6);
  Serial.print("F");
  Serial.println(" ");
  Serial.println(" ");
  Serial.println("Hot Side Small Tank");
  Serial.print(mytempC5);
  Serial.print("C");;
  Serial.print(' ');
  Serial.print(mytempF5);
  Serial.print("F");
  Serial.println(" ");
  Serial.println(" ");
  Serial.println("Cold Side Small Tank");
  Serial.print(mytempC7);
  Serial.print("C");
  Serial.print(' ');
  Serial.print(mytempF7);
  Serial.print("F");
  Serial.println(" ");
  Serial.println(" ");
  } 
   {
    if (isAM())
    {
      lcd.setCursor(5,3);
      lcd.print(" AM ");
    }
    if (isPM())
    {
      lcd.setCursor(5,3);
      lcd.print(" PM ");
    }   
    if (minute()<10)
    {
      lcd.setCursor(2,3);
      lcd.print(":");
      lcd.setCursor(3,3);
      lcd.print("0");
      lcd.print(minute());
    }
    if (minute()>=10)
    {
      lcd.setCursor(2,3);
      lcd.print(":");
      lcd.setCursor(3,3);
      lcd.print(minute());
    }   
    lcd.setCursor(0,3);
    if (hourFormat12() < 10)
    {
       lcd.print("0"); 
       }
       if (hourFormat12() > 12)
       {
          lcd.print("0"); 
        } 
        else 
        {
          lcd.print(hourFormat12()); 
          }
    lcd.setCursor(10,3);
    lcd.print(month());
    lcd.print("/");
    lcd.print(day());
    lcd.print("/");
    lcd.print(year());
    lcd.setCursor(0, 0);
    lcd.print("LG Tank");
    lcd.setCursor(0, 1);
    lcd.print("H");
    lcd.setCursor(11,0);
    lcd.print("SM Tank");
    lcd.setCursor(11, 1);
    lcd.print("H");
    lcd.setCursor(0,2);
    lcd.print("C");
    lcd.setCursor(11, 2);
    lcd.print("C");
    lcd.setCursor(2, 1);
    lcd.print(mytempF4);
    lcd.print("F");
    lcd.setCursor(2, 2);  
    lcd.print(mytempF6);
    lcd.print("F");
    lcd.setCursor(13, 1);  
    lcd.print(mytempF5);
    lcd.print("F");
    lcd.setCursor(13, 2);    
    lcd.print(mytempF7);
    lcd.print("F");
    delay(1000);
  }
}

I don't see anything anywhere in that code that ever looks at the temperature or tries to control a relay.

Make a variable for the setpoint. You need one if statement to check if it is time for it to be 90 or time to be 80. Just use hour() from the time library and see if it is between 8 and 20 or not. If it is put the setpoint at 90 else put it at 80. Then inside that if you need an if that checks the temp and sees if it should turn the relay on else turn it of. If the temp is less than the setpoint minus 2 then turn the relay on, else if it is greater than the setpoint plus 2 turn it off.

I don’t know how to have if see if it is between two times. I have tried to find examples for hints but couldn’t find anything.

if (hour() == 8 && mytempF4 <= 92)
      {
        digitalWrite(8, LOW);
      }
      if (hour() == 8 && mytempF4 <= 88)
      {
        digitalWrite(8, HIGH);

that works but if I try and add more times it doesn’t work.

Don’t try to combine the temp checking and time checking in the same if statement. That just gets confusing. Always separate things when you can.

int tempSetPoint;  // at global scope


if(hour >=8 && hour < 20){
     tempSetPoint = 90;
}
else {
     tempSetPoint = 80;
}


if (temperature <= tempSetPoint - 2) {
    turn the relay on
}
else if (temperature >= tempSetPoint +2) {
    turn the relay off
}

Okay I see what I was doing wrong now, Thank you. I’ll go ahead and retry it this way. To add two relays I would do say timeSetPoint1 and timeSetPoint2? also I would have to write this for ==0 && <8 and again for =>20 && <23:59 or 24?

if(hour >=8 && hour < 20){
     tempSetPoint = 90;
}
else {
     tempSetPoint = 80;
}

If there are only two setpoints it’s easy. This one covers everything between 8am and 8pm. The else catches everything else. If it ain’t between 8am and 8pm, then it has to be between 8pm and 8am so there’s no reason to even check the time is outside that range. Any time that’s not in this range has to be in the other.

So thats it, all I have to do is write that twice replacing temperature with my mytempF4 and mytempF5 and telling it to turn relays LOW and HIGH for both pin 8 and 9. Thank you so much I have been messing around with this for about 3 nights. its been driving me crazy.

Go read post 2 again and look at the code I wrote. See how the description of what you want to happen in English lines right up with how the code reads. Sometimes it really helps to put the code aside for a minute and just write out what you want to happen. Then the code sort of writes itself.