# [Time][SOLVED] How to compare hours ?

Hoi friends,
i want to make a timer, that drives a rgb-led.changing colors works, so no problem with that.

In the time frame (all MET) between 22 (10 p.m.) and 10 (10 a.m.) the led should not be active, but in the other time frame between 10 and 22 o’clock
.
I read out the variable “hour” from a RTC1307 Module - that works also fine.

Here is my code, but that has no effect:

``````   unsigned int rgbColour[3];
if ((hour() > 22 & hour() <10)) {
rgbColour[0] = 32;
rgbColour[1] = 0;
rgbColour[2] = 0;
}
else if (hour() > 10 & hour() < 22) {
rgbColour[0] = 0;
rgbColour[1] = 0;
rgbColour[2] = 0;
}
``````

i also tried it with the double ampersands, but the same result:

``````[code]   unsigned int rgbColour[3];
if ((hour() > 22 && hour() <10)) {
rgbColour[0] = 32;
rgbColour[1] = 0;
rgbColour[2] = 0;
}
else if (hour() > 10 && hour() < 22) {
rgbColour[0] = 0;
rgbColour[1] = 0;
rgbColour[2] = 0;
}
``````

[/code]

Anyone a suggestion or hint ?

``````    if ((hour() > 22 && hour() <10)) {
``````

The hour is not going to be greater than 22 AND less than 10 at the same time. Do you mean “or”?

hmm, until now i suggested the code should be correct, because i.e. 23 IS > 22 AND < 10

but now you gave a new brain input

i tried the following with the same result:

``````    if ((hour() > 22 || hour() <10)) {
rgbColour[0] = 0;
rgbColour[1] = 32;
rgbColour[2] = 0;
}
else if (hour() > 10 || hour() < 22) {
rgbColour[0] = 0;
rgbColour[1] = 0;
rgbColour[2] = 0;
}
``````

i printed the hour to the serial port and -what’s that ?

``````3385
``````

was the putput…when i print it with the same command earlier, hour, minute and second was displayd correctly.

have i to convert the time ?

Here are the functions for the time and date:

``````void digitalClockDisplay(){
// digital clock display of the time
Serial.print(day());
Serial.print(".");
Serial.print(month());
// if(month < 10)
//  Serial.print('0');
//Serial.print(month());
Serial.print(".");
Serial.print(year());
Serial.print(" ");
Serial.print(hour());
printDigits(minute());
printDigits(second());
Serial.println();
delay(1000);
}

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

hmm, until now i suggested the code should be correct, because i.e. 23 IS > 22 AND < 10

Please explain how 23 is less than 10.

Trixi:
hmm, until now i suggested the code should be correct, because i.e. 23 IS > 22 AND < 10

PaulS:
Please explain how 23 is less than 10.

I was wondering that too.

Trixi: i printed the hour to the serial port and -what's that ?

``````3385
``````

I don't see how your code, which prints dots and spaces, will output the above.

` if ((hour() > 22) || (hour() <10)) {` Watch your parentheses. This will give the hours when you want the lights OFF. Reverse both the ‘<’ and ‘>’ for the hours you want the lights ON.
There’s no need to check the time again in the else statement, as it will only operate when the if statement is false.

@ Nick and Paul:
that’s why i love a forum - if you are stuck in case of “burn out” 8) (it was after midnight here in europe) guys like you helps to keep the focus focused again

Just as i lay in bed the solution with the “&&”, the “&” and the “||” appears in front of my half-closed eyes as clearly as a summer morning sky XD

@Henry
thx a lot, too - this way works (of course, but only after the brain made “click”)

``````    if (hour() > 22 || hour() < 10) {
rgbColour[0] = 0;
rgbColour[1] = 0;
rgbColour[2] = 0;
}
if (hour() < 22 || hour() > 10) {
rgbColour[0] = 255;
rgbColour[1] = 0;
rgbColour[2] = 0;
}
``````

The part for reading the RTC DS1307 and displaying it is the following:

``````void digitalClockDisplay(){
// digital clock display of the time
Serial.print(day());
Serial.print(".");
Serial.print(month());
// if(month < 10)
//  Serial.print('0');
//Serial.print(month());
Serial.print(".");
Serial.print(year());
Serial.print(" ");
Serial.print(hour());
printDigits(minute());
printDigits(second());
Serial.println();
delay(1000);
}

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

p.s.: modified and modified your karmas, too

Let me introduce you to the ‘else’ symbol:

``````   if (hour() > 10 && hour() < 22)
{
rgbColour[0] = 0;
rgbColour[1] = 0;
rgbColour[2] = 0;
}
else
{
rgbColour[0] = 255;
rgbColour[1] = 0;
rgbColour[2] = 0;
}
``````

I advise to write such ‘between’ statement in a logical order. All < in the same direction and increasing numbers like so:

if(10 < hour() && hour() < 22)

You immediately see the edge cases.

You immediately see the edge cases.

Maybe, but that is not the order people typically think in. If you ask someone to look at that code, and figure out what it does, most people will invert the first test, and say that if it is after 10 and before 22, do something. Writing the code they way you think makes it easier to follow, in my opinion. RobvdVeer's code is exactly how I'd write it.

So, Rob’s code was not different to my very first one:

``````  if ((hour() > 22 && hour() <10)) {
rgbColour[0] = 32;
rgbColour[1] = 0;
rgbColour[2] = 0;
}
else if (hour() > 10 && hour() < 22) {
rgbColour[0] = 0;
rgbColour[1] = 0;
rgbColour[2] = 0;
}
``````

instead of the seconde if sequence…

This one works also as the other one with “||” statement.

And i agreed with Paul - you think “real” but this has nothing to do with the code segments

Trixi: instead of the seconde if sequence...

Which I pointed out to you in my post...

There's no need to check the time again in the else statement, as it will [u]only[/u] operate when the if statement is false.

Trixi:
So, Rob’s code was not different to my very first one:

``````  if ((hour() > 22 && hour() <10)) {
``````

rgbColour[0] = 32;
rgbColour[1] = 0;
rgbColour[2] = 0;
}
else if (hour() > 10 && hour() < 22) {
rgbColour[0] = 0;
rgbColour[1] = 0;
rgbColour[2] = 0;
}

One wonders what happens if the time is 10 or 22 in this case.

An “else” seems appropriate here.

And you still need || rather than && (on the first “if”).

This one works:

``````if (hour() > 10 && hour() < 22)
{
rgbColour[0] = 0;
rgbColour[1] = 0;
rgbColour[2] = 255;
}
else
{
rgbColour[0] = 0;
rgbColour[1] = 0;
rgbColour[2] = 0;
}
``````

23:xx is greater then 10 AND not smaller then 22 = so LED does nothing (as wished)

This one CAN not:

``````if (hour() > 10 || hour() < 22)
{
rgbColour[0] = 0;
rgbColour[1] = 0;
rgbColour[2] = 255;
}
else
{
rgbColour[0] = 0;
rgbColour[1] = 0;
rgbColour[2] = 0;
}
``````

23:xx is greater then 10 OR smaller then 22(NO) = so LED makes BLINK

Quite right. 9 is also less than 22.