Compare times

Hello friends!

I made a small program to compare schedules, that is, if the time is between 10 AM and 5 PM PM the window is open and shows on the LCD “Open window”.
If the time is outside 10 AM and after 5 PM the window is closed and shows on the LCD “Closed window”
The program I made does not work in the comparison part of the schedules, ie it does not show in the Lcd the messages of “window open or closed”

Can someone edit my code and explain why it does not work?
Thank you very much in advance

#include <Wire.h>
#include "RTClib.h"
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 20, 4);
RTC_DS3231 RTC;

#define in1 3 // 
#define val_d  4   // 

byte hour1 = 0;
byte min1 = 0;
byte seg1 = 0;


String zero(int n) { 

  if (n < 10) {     
    return "0" + String(n);       
  } else {
    return String(n);
  }
}
void setup () {
  lcd.backlight();
  lcd.init();
  Wire.begin();
  RTC.begin();
  // RTC.adjust(DateTime(2017,11,5, 3, 0, 0));

  //RTC.adjust(DateTime(__DATE__, __TIME__));
  pinMode(in1, INPUT);
  pinMode(val_d, INPUT);
}

void loop () {

  DateTime now = RTC.now();
  hour1 = (now.hour(), DEC);
  min1 = (now.minute(), DEC);
  seg1 = (now.second(), DEC);

  lcd.setCursor(0, 0);
  lcd.print(zero(now.day()));
  lcd.print('/');

  lcd.print(zero(now.month()));
  lcd.print('/');
 
  lcd.print(now.year(), DEC);
  lcd.print(' ');
  
  lcd.setCursor(0, 1);
  lcd.print(zero(now.hour()));
  lcd.print(':');

  lcd.print(zero(now.minute()));
  lcd.print(':');

  lcd.print(zero(now.second()));

 
      if ((now.hour()) >= 10 && (now.hour()) < 17 && (val_d == HIGH)) //
      {
        lcd.setCursor(5, 3);
        lcd.print("Open window");
        delay(30);
        
      }
   
    if ((now.hour() <10 ) && (now.hour() > 17 ) &&  (val_d == LOW)) //    
       {
        
        lcd.setCursor(5, 3);
        lcd.print("Closed window");
        delay(30);
      }
   
    }

don’t use the quote tag to poste code…

Please correct your post above and add code tags around your code:
[code]`` [color=blue]// your code is here[/color] ``[/code].

It should look like this:// your code is here
(Also press ctrl-T (PC) or cmd-T (Mac) in the IDE before copying to indent your code properly)

———

How do you want your hour to be BOTH less than 10 and larger than 17 at the same time?

    if ((now.hour() <10 ) && (now.hour() > 17 ) &&  (val_d == LOW)) //
#define val_d  4   //
  if ((now.hour()) >= 10 && (now.hour()) < 17 && (val_d == HIGH)) //

val_d is always going to be 4. Did you mean to read the state of the pin using digitalRead(val_d); ?

J-M-L:
don’t use the quote tag to poste code…

Please correct your post above and add code tags around your code:

[code]

[color=blue]// your code is here[/color]

[/code]

.

It should look like this:

// your code is here

(Also press ctrl-T (PC) or cmd-T (Mac) in the IDE before copying to indent your code properly)

———

How do you want your hour to be BOTH less than 10 and larger than 17 at the same time?

    if ((now.hour() <10 ) && (now.hour() > 17 ) &&  (val_d == LOW)) //

Hi J-M-L

I thought I could do these two comparisons as I did in the code, now I have no idea how to do it, if I can help thanks
I did several searches on google and I did not find anything that could help me

Have a look at the difference between the logical AND (&&) that you used and a logical OR (||). That might help. And are you sure you mean > 17? If you use that 17:59 is in your time range (the hour is not greater than 17).

Steve

slipstick: Have a look at the difference between the logical AND (&&) that you used and a logical OR (||). That might help. And are you sure you mean > 17? If you use that 17:59 is in your time range (the hour is not greater than 17).

Steve

Hi slipstick Thank you for your support

I already did the tests with the logic (&&) and ( ||) it still does not work! Unfortunately I do not know how to compare like you said 17:59 PM, as I did the code the comparison does not admit to doing 17; 59! Anyway, I appreciate any help

read again #2

J-M-L:
read again #2

Hi J-M-L

Thanks for your support.
I have modified the code to only compare schedules, because that is what is not working, so it does not show open or closed window messages!
I’ve done several tests with all my little knowledge but I’m not successful
Once again I thank you for your help.

#include <Wire.h>
#include "RTClib.h"
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 20, 4);
RTC_DS3231 RTC;

//#define in1 3 // 
//#define val_d  4   // 

byte hour1 = 0;
byte min1 = 0;
byte seg1 = 0;


String zero(int n) { 

  if (n < 10) {     
    return "0" + String(n);       
  } else {
    return String(n);
  }
}
void setup () {
  lcd.backlight();
  lcd.init();
  Wire.begin();
  RTC.begin();
  // RTC.adjust(DateTime(2017,11,5, 3, 0, 0));

  //RTC.adjust(DateTime(__DATE__, __TIME__));
  pinMode(in1, INPUT);
  pinMode(val_d, INPUT);
}

void loop () {

  DateTime now = RTC.now();
  hour1 = (now.hour(), DEC);
  min1 = (now.minute(), DEC);
  seg1 = (now.second(), DEC);

  lcd.setCursor(0, 0);
  lcd.print(zero(now.day()));
  lcd.print('/');

  lcd.print(zero(now.month()));
  lcd.print('/');
 
  lcd.print(now.year(), DEC);
  lcd.print(' ');
  
  lcd.setCursor(0, 1);
  lcd.print(zero(now.hour()));
  lcd.print(':');

  lcd.print(zero(now.minute()));
  lcd.print(':');

  lcd.print(zero(now.second()));

 
      if ((now.hour()) > 10 && (now.hour()) < 12 ) // 
      {
        lcd.setCursor(5, 3);
        lcd.print("Open window");
        delay(30);
        
      }
   
    if ((now.hour() <= 10 ) && (now.hour() > 13 )) //
    {   
        lcd.setCursor(5, 3);
        lcd.print("Closed window");
        delay(30);
      }
   }
  if ((now.hour() <= 10 ) && (now.hour() > 13 ))

You have done it again.

now.hour() cannot be equal or less than 10 and greater than 13

It could be equal or less than 10 or greater than 13 though

See reply #4

UKHeliBob:

  if ((now.hour() <= 10 ) && (now.hour() > 13 ))

You have done it again.

now.hour() cannot be equal or less than 10 and greater than 13

It could be equal or less than 10 or greater than 13 though

See reply #4

Hi UKHeliBob

Thank you for your Support

I understand that at the same (if) I can not make two comparisons with (now.hour ()) right?
I already did several searches but I did not find anything that helps me.
No (reply # 4) already said that I do not know how to implement the code for the 17:59 time
I changed the code again in two (if) but without success!

Hope it helps

#include <Wire.h>
#include "RTClib.h"
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 20, 4);
RTC_DS3231 RTC;

//#define in1 3 // 
//#define val_d  4   // 

byte hour1 = 0;
byte min1 = 0;
byte seg1 = 0;


String zero(int n) { 

  if (n < 10) {     
    return "0" + String(n);       
  } else {
    return String(n);
  }
}
void setup () {
  lcd.backlight();
  lcd.init();
  Wire.begin();
  RTC.begin();
  // RTC.adjust(DateTime(2017,11,5, 3, 0, 0));

  //RTC.adjust(DateTime(__DATE__, __TIME__));
  //pinMode(in1, INPUT);
  //pinMode(val_d, INPUT);
}

void loop () {

  DateTime now = RTC.now();
  hour1 = (now.hour(), DEC);
  min1 = (now.minute(), DEC);
  seg1 = (now.second(), DEC);

  lcd.setCursor(0, 0);
  lcd.print(zero(now.day()));
  lcd.print('/');

  lcd.print(zero(now.month()));
  lcd.print('/');
 
  lcd.print(now.year(), DEC);
  lcd.print(' ');
  
  lcd.setCursor(0, 1);
  lcd.print(zero(now.hour()));
  lcd.print(':');

  lcd.print(zero(now.minute()));
  lcd.print(':');

  lcd.print(zero(now.second()));

 
      if (now.hour() > 10 ) // 
      {
        if(now.hour() < 12 )
        {
        lcd.setCursor(5, 3);
        lcd.print("Open window");
        delay(30);
        }
      }
         if (now.hour() < 10 ) // 
         {
        if(now.hour() > 12 )
        {
        lcd.setCursor(5, 3);
        lcd.print("Closed window");
        delay(30);
        }
      }
}

I understand that at the same (if) I can not make two comparisons with (now.hour ()) right?

Wrong

You can make as many comparisons as you like (within reason) but you must do it the right way

I assume that this

if ((now.hour() <= 10 ) && (now.hour() > 13 ))

is intended to test whether the hour is less than or equal to 10 or that the hour is greater than 13.

Is that what you want to test for ?
If not, then what do you want it to test for ?

Let’s try again. The test you’ve written (with the &&) is testing if hour is less than or equal to 10 AND AT THE SAME TIME hour is greater than or equal to 13. It can’t possibly be true.

What we all think you mean if hour <= 10 OR ELSE hour >=13.

OR is written as ||. AND is written &&.

Steve

UKHeliBob:
Wrong

You can make as many comparisons as you like (within reason) but you must do it the right way

I assume that this

if ((now.hour() <= 10 ) && (now.hour() > 13 ))

is intended to test whether the hour is less than or equal to 10 or that the hour is greater than 13.

Is that what you want to test for ?
If not, then what do you want it to test for ?

Hi UKHeliBob

I’ll try to explain what I’m about to do.

Yes, what I really need is a program that, from 10:00 a.m. to 5:00 p.m., show me on the LCD “open window”, and that from 5:00 p.m. to 10:00 p.m. Show me the morning on the Lcd “closed window”

The clock is working fine, it shows the date and time on the LCD without any problem, now these comparisons of the schedules is that it does not work.

I’m using DS3231 in my tests.

Thank you very much for your attention.

slipstick:
Let’s try again. The test you’ve written (with the &&) is testing if hour is less than or equal to 10 AND AT THE SAME TIME hour is greater than or equal to 13. It can’t possibly be true.

What we all think you mean if hour <= 10 OR ELSE hour >=13.

OR is written as ||. AND is written &&.

Steve

Hi Steve.

Thank you for your help, but unfortunately I do not quite understand what you mean by;

What we all think you mean if hour <= 10 OR ELSE hour >=13.

OR is written as ||. AND is written &&.

from 5:00 p.m. to 10:00 p.m.

Looks wrong

Do you mean if the hour is less than 05:00 or the hour is greater than 22:00 display "Close window"

UKHeliBob:
Looks wrong

Do you mean if the hour is less than 05:00 or the hour is greater than 22:00 display “Close window”

Hi

I hope you understand what I mean ???

From 10:00 am until 17:00 pm window open, and after 17:00 p.m. until 10:00 p.m. closed window

From 10:00 am until 17:00 pm window open, and after 17:00 p.m. until 10:00 p.m. closed window

What about midnight to 10:00 am and 10:00 pm to midnight ?

It doesn’t help that you keep changing the times you’re talking about.

I think what you mean is what you originally said: If the current time is from 10am up to but not including 5pm (17:00) show “Window open” or if it is not between those times show “Window closed”. Is that right?

If so then surely all you need is something as simple as:

if ((hour >9) && (hour <17){
do the show "Window Open" stuff
}
else{
do the show "Window Closed" stuff
}

What am I missing?

Steve

You’re getting very close. I think you could do it like this:

      if ((now.hour() >= 10) && (now.hour() < 17)) 
      {
        lcd.setCursor(5, 3);
        lcd.print("Open window");
        delay(30);
      }
      else
      {
        lcd.setCursor(5, 3);
        lcd.print("Closed window");
        delay(30);
      }

since anything not inside the case is outside the case. But I think what you are looking for is this:

      if ((now.hour() >= 10) && (now.hour() < 17))
      {
        lcd.setCursor(5, 3);
        lcd.print("Open window");
        delay(30);
      }
      if ((now.hour() < 10) || (now.hour() >= 17)) 
      {
        lcd.setCursor(5, 3);
        lcd.print("Closed window");
        delay(30);
      }

Jimmus:
You’re getting very close. I think you could do it like this:

      if ((now.hour() >= 10) && (now.hour() < 17)) 

{
        lcd.setCursor(5, 3);
        lcd.print(“Open window”);
        delay(30);
      }
      else
      {
        lcd.setCursor(5, 3);
        lcd.print(“Closed window”);
        delay(30);
      }


since anything not inside the case is outside the case. But I think what you are looking for is this:

if ((now.hour() >= 10) && (now.hour() < 17))
      {
        lcd.setCursor(5, 3);
        lcd.print(“Open window”);
        delay(30);
      }
      if ((now.hour() < 10) || (now.hour() >= 17))
      {
        lcd.setCursor(5, 3);
        lcd.print(“Closed window”);
        delay(30);
      }

Hi Jimmus

Thanks for your help
This code I already tested and it did not work!