RTC problems. related to numeric value for hours

Hello my project uses and RTC to trigger some relays at a specific time. The sketch work but not at certain hours EX: if i set to on at 9 and off at 11 it work but wont work if i set to on at 2 and off at 10, wont turn on. I’m speculating something with the way it sees certain numbers. Do i have to use 02 as the on time and why?

#include <Wire.h>
#include <LiquidCrystal.h>
#include <Time.h>
#include <TimeLib.h>
#include <DS3232RTC.h>

LiquidCrystal lcd(12, 11, 6, 5, 4, 3);

//First we add the libraries.
//Then we create the datatype integer to read the value from the hours.

int val;

//Next in void setup() we set the solid state relay pin to 7
//at line 14 you can set the time for the RTC.
//You only need to do this once!
//The format is:Hours / minutes / second / day of month / month / year
//After setting time upload the code.
//On the next upload you need to comment out line 14 and 15 shown below.
//setTime(17, 59, 45, 20, 10, 2017);//Hours / minutes / second / day of month / month / year
//RTC.set(now());
//The rtc will remember the time, if you do not comment out line 14 and 15 after setting the time, the RTC will reset every time you upload the code.

void setup()
{
  // set up the LCD's number of columns and rows:
  lcd.begin(16, 2);
  Serial.begin(9600);
  pinMode(9, OUTPUT); //relay1
  pinMode(8, OUTPUT); //relay2


  /////////Uncomment both lines below when you want to set the time
  /////////AFTER SETTING TIME COMMENT the lines.
  // setTime(17, 32, 45, 11, 10, 2018);//Hours / minutes / second / day of month / month / year
  // RTC.set(now());

  setSyncProvider(RTC.get);   // the function to get the time from the RTC
  if (timeStatus() != timeSet)
    Serial.println("Unable to sync with the RTC");

  else Serial.println("RTC has set the system time");
  lcd.print("RTC Sync OK ");

}

//At the function void loop() we set the functions digitalClockDisplay(); and AutomaticProgram(); to run forever

void loop()
{
  digitalClockDisplay();
  AutomaticProgram();

}

//At the function void AutomaticProgram() we set the if statements to run the automatic program between 2 values/hours.
//in this example we used the value (hour()); from the function voiddigitalClockDisplay(void){
//The if statements simply read the first value of the output in the serial monitor.
//So in this case if 4 oclock in the midday is the value that hits, the Solid state relay will turn HIGH/ON and turn on the LAMP/Group/Or whatever device you like.
//If the value = 5 the solid state relay will turn LOW/OFF. Default is low.

void AutomaticProgram()
{ //we are using hour as a value the rest is ignored.
  val = (hour()); //Grow/Day - Groei/Dag between 4:00 and 22:00
  if ((2 == val))
  {
    digitalWrite(9, HIGH);
    digitalWrite(8, HIGH);
  }
  else if (val == 10)
  {
    digitalWrite(9, LOW);
    digitalWrite(8, LOW);
  }




  else
  {
    digitalWrite(8, LOW);
    digitalWrite(9, LOW);
  }
}

//At the function void digitalClockDisplay(void) { we send the values to the serial monitor.
//You can uncomment all of these and send them to the serial monitor if you like i just chose to use hours());
//as a value to read, the rest will be ignored as you can see in the serial monitor it prints out 4:00:00 for example
//but the code only reads the first value which is hours()); the rest :00:00 is ignored.

void digitalClockDisplay(void)
{
  // digital clock display of the time
  Serial.print(hour());
  printDigits(minute());
  printDigits(second());
  // Serial.print(' ');
  // Serial.print(day());
  // Serial.print(' ');
  // Serial.print(month());
  // Serial.print(' ');
  //Serial.print(year());
  Serial.println();

  lcd.setCursor(0, 1);

  if (hour() < 10) {
    lcd.print("0");
    lcd.print(hour());
  }
  else {
    lcd.print(hour());
  }


  lcd.setCursor(2, 1);
  lcd.print(":");

  lcd.setCursor(3, 1);
  if (minute() < 10) {
    lcd.print("0");
    lcd.print(minute());
  }
  else {
    lcd.print(minute());
  }





}

//At the function void printDigits(int digits) we print the digits to the serial monitor.

//You can change line 66 from : to . or , or whatever.

void printDigits(int digits)
{
  // utility function for digital clock display: prints preceding colon and leading 0
  Serial.print(':');//line 66
  if (digits < 10)
    Serial.print('0');
  Serial.print(digits);
}

The way you’ve got your function set up,

void AutomaticProgram()
{ //we are using hour as a value the rest is ignored.
    val = (hour()); //Grow/Day - Groei/Dag between 4:00 and 22:00
    if ((2 == val))
    {
        digitalWrite(9, HIGH);
        digitalWrite(8, HIGH);
    }
    else if (val == 10)
    {
        digitalWrite(9, LOW);
        digitalWrite(8, LOW);
    }
    else
    {
        digitalWrite(8, LOW);
        digitalWrite(9, LOW);
    }
}

it will switch off any time val is not equal to 2. Better when you work with ranges is to specify the limits and then test between them,

int on_time = 2;
int off_time = 10;
if (val >= on_time && val < off_time)
{
  turn_on();
}
else
{
  turn_off();
}

If you’re passing midnight, you need to do a bit more math.

Did you want 2 AM to 10 AM? Did you want 2 PM (14:00) to 10 AM? Did you want 2 AM to 10 PM (22:00)? Did you want 2 PM (14:00) to 10 PM (22:00)? If your clock is set for 12-hour time you will need to check the AM/PM flag with each comparison.

In regards to the second reply, I am looking for 2 am to 10 am. This operates a heater of sorts used in the Canadian winters so it has to work. I do believe it uses 24 hr format as I print it to an lcd and it prints in 24 hr time. For the first response I could try to change my code as you mentioned but I am new might take me some time. Why does it work if i set it to 9 am to 10 am, as i tested the other day?

The hour is 9 until it becomes 10. It would remain on as long as the hour was 9 and turn off otherwise. Coincidence that your off time was set to 10. Set the off time to any other value and it would still turn off as soon as the hour was not 9.

Ive tried this now…

#include <Wire.h>
#include <LiquidCrystal.h>
#include <Time.h>
#include <TimeLib.h>
#include <DS3232RTC.h>

LiquidCrystal lcd(12, 11, 6, 5, 4, 3);

//First we add the libraries.
//Then we create the datatype integer to read the value from the hours.

int val;
int on_time = 2;
int off_time = 10;

//Next in void setup() we set the solid state relay pin to 7
//at line 14 you can set the time for the RTC.
//You only need to do this once!
//The format is:Hours / minutes / second / day of month / month / year
//After setting time upload the code.
//On the next upload you need to comment out line 14 and 15 shown below.
//setTime(17, 59, 45, 20, 10, 2017);//Hours / minutes / second / day of month / month / year
//RTC.set(now());
//The rtc will remember the time, if you do not comment out line 14 and 15 after setting the time, the RTC will reset every time you upload the code.

void setup()
{
  // set up the LCD's number of columns and rows:
  lcd.begin(16, 2);
  Serial.begin(9600);
  pinMode(9, OUTPUT); //relay1
  pinMode(8, OUTPUT); //relay2


  /////////Uncomment both lines below when you want to set the time
  /////////AFTER SETTING TIME COMMENT the lines.
  // setTime(17, 32, 45, 11, 10, 2018);//Hours / minutes / second / day of month / month / year
  // RTC.set(now());

  setSyncProvider(RTC.get);   // the function to get the time from the RTC
  if (timeStatus() != timeSet)
    Serial.println("Unable to sync with the RTC");

  else Serial.println("RTC has set the system time");
  lcd.print("RTC Sync OK ");

}

//At the function void loop() we set the functions digitalClockDisplay(); and AutomaticProgram(); to run forever

void loop()
{
  digitalClockDisplay();
  AutomaticProgram();

}

//At the function void AutomaticProgram() we set the if statements to run the automatic program between 2 values/hours.
//in this example we used the value (hour()); from the function voiddigitalClockDisplay(void){
//The if statements simply read the first value of the output in the serial monitor.
//So in this case if 4 oclock in the midday is the value that hits, the Solid state relay will turn HIGH/ON and turn on the LAMP/Group/Or whatever device you like.
//If the value = 5 the solid state relay will turn LOW/OFF. Default is low.

void AutomaticProgram()
{ //we are using hour as a value the rest is ignored.
  val = (hour()); //Grow/Day - Groei/Dag between 4:00 and 22:00


  if (val >= on_time && val < off_time)
  {
    digitalWrite(9, HIGH);
    digitalWrite(8, HIGH);
  }
  else
  {
    digitalWrite(9, LOW);
    digitalWrite(8, LOW);
  }
}
//At the function void digitalClockDisplay(void) { we send the values to the serial monitor.
//You can uncomment all of these and send them to the serial monitor if you like i just chose to use hours());
//as a value to read, the rest will be ignored as you can see in the serial monitor it prints out 4:00:00 for example
//but the code only reads the first value which is hours()); the rest :00:00 is ignored.

void digitalClockDisplay(void)
{
  // digital clock display of the time
  Serial.print(hour());
  printDigits(minute());
  printDigits(second());
  // Serial.print(' ');
  // Serial.print(day());
  // Serial.print(' ');
  // Serial.print(month());
  // Serial.print(' ');
  //Serial.print(year());
  Serial.println();

  lcd.setCursor(0, 1);

  if (hour() < 10) {
    lcd.print("0");
    lcd.print(hour());
  }
  else {
    lcd.print(hour());
  }


  lcd.setCursor(2, 1);
  lcd.print(":");

  lcd.setCursor(3, 1);
  if (minute() < 10) {
    lcd.print("0");
    lcd.print(minute());
  }
  else {
    lcd.print(minute());
  }

}
//At the function void printDigits(int digits) we print the digits to the serial monitor.

//You can change line 66 from : to . or , or whatever.

void printDigits(int digits)
{
  // utility function for digital clock display: prints preceding colon and leading 0
  Serial.print(':');//line 66
  if (digits < 10)
    Serial.print('0');
  Serial.print(digits);
}

Was this what you were thinking?

“Minutes since midnight” (hour*60+minute on a 24h clock) makes for easy on/off comparisons.

For on at 9 AM and off at 2 PM, something like this works:

if ( (x >= 360) && (x <= 840) ) turn_on();
else turn_off();

In cases where a timed interval spans midnight, use logical OR. Example: for on at 6PM and off at 8AM

if ( (x >= 1080) || (x <= 480)) turn_on();
else turn_off();

The modulus operator can be useful in those cases, too.

That kinda looks like exactly what I suggested. If you want finer resolution on the time, think about the current being the number of minutes since midnight so 0900 becomes 540, 0915 becomes 555 etc. Then the test becomes for example, on at 0915, off at 1430 if(val_minutes >= 555 && val_minutes < 870).

Extending jremington's suggestion, a simple test,

 if(off_time > on_time) use &&
else use ||

Thanks DK, I am fine just using the hours so I will go ahead and upload this and give it a go. Quick question on logical OR, is that just two lower case L's ? Thanks everyone for your help.

No, it’s the vertical bar, typically found <>.