Can RTClib use decimal hour entry

It would make my little program much simpler if I could input decimal hours (1.5) as opposed to having to fiddle with minutes. Can Float be used in the library to allow this ?

You could write your own library to do this.
Or
You could use what is currently available to you.

Your decision.

.

What kind of comment is that ? I am asking what is available. Thanks for the help.

1 Like

(deleted)

As mentioned above do your own thing.
1.5*60=90
90%60=30
If you want, put it in a function.
What's the the problem?

.

Maybe it would help if I showed how I am trying to use it.

Lets suppose I want a light to turn on at 2 a.m. and off at 4:30 a.m.

If it was 2 & 4 I would just say
If now.hour >=2 && now.hour <=4
turn on the light and this works

But the 4:30 is more awkward. I have tried now.hour 4.5, no good
I have tried using minutes more than 60 (ie now.hour > 2 && now minute < 150), no good

When I try mixing hours and minutes les than 60, it turns off in between 2 & 4:30

() left out due to laziness

I won't say I'm stumped, but being able to use decimal hours would solve this issue.

Convert to minutes.

myMinutes > 120 && myMinutes < 270 // 2am to 4:30am

.

Well i just set it up for minutes >= 747 (that's 12:27 p.m. my time) & <= 750

nope. As I mentioned before, it doesn't want to respond to minutes > 60. Course I could be doing something wrong.

minutes >= 747 && minutes<= 750

Make sure you use unsigned int (an int works too)

Show us your sketch.

.

  // This is supposed to turn on a light at 2 a.m. & off at 4:30 a.m. Pin 2 is an LED.

#include <Wire.h>
#include "RTClib.h"

RTC_DS1307 rtc;

void setup() 
{
  Serial.begin(9600);
  Wire.begin();
  rtc.begin();

  if (! rtc.isrunning())
  {
    Serial.println("RTC is NOT running!");
  }
  //Any pin. I have used On-Board LED Pin 13 & 2 to LED
  pinMode(13, OUTPUT);
  digitalWrite(13,LOW);
  pinMode(2, OUTPUT);
  digitalWrite(2,LOW);

}

void loop() 
{
  DateTime now = rtc.now();
  Serial.print(now.year(), DEC);
  Serial.print("/");
  Serial.print(now.month(), DEC);
  Serial.print("/");
  Serial.print(now.day(), DEC);
  Serial.print(" (");
  Serial.print(now.hour(), DEC);
  Serial.print(":");
  Serial.print(now.minute(), DEC);
  Serial.print(":");
  Serial.print(now.second(), DEC);
  Serial.print(")");
  Serial.println();

  
  Serial.println();
  delay(1000);

    //The time is set as 24 hours
    
  if (now.minute() >= 770 && now.minute() <= 775)  // Set Pin 13 HIGH until Time interval exceeded, the LOW
  {
    digitalWrite(13,HIGH);
    digitalWrite(2,HIGH);
  }
  
   else
   {
    digitalWrite(13,LOW);
    digitalWrite(2,LOW);
   }
   
}

I am using a 3231 rtc.

if (now.minute() >= 770 && now.minute() <= 775)

But you have not added the hour (12 in your example) to this.

Try:
if (((now.hour() * 60) + now.minute()) >= 770 && ((now.hour() * 60) + now.minute()) <= 775)

now.hour()

BTW
You can let the compiler do work for you.
ex.
12 * 60 + 50 rather than saying 770

.

hey hey, it worked.
I had to comment out the trailing now.hour() - I'm not sure what that is for.

Until your last comment, I thought my mistake was not introducing now.hour() so that the minutes would have a reference point.

what is the difference if the machine multiplies 12*60 + 50, or I just put 770 in ? Obviously it does make a difference, but I'm wondering why. You have solved my problem, so if you don't feel like going into explanations, I am still grateful.

"what is the difference if the machine multiplies 12*60 + 50, or I just put 770 in ? "

Helps prevent mistakes if you are old and you cannot multiple any more :wink:

. . . >= 770 versus . . . >= 12*60+50

for example

int lowerTime = 12 * 60 + 50; //12:50
int upperTime = 12 * 60 + 55; //12:55
.
.
.
if (((now.hour() * 60) + now.minute()) >= lowerTime && ((now.hour() * 60) + now.minute()) <= upperTime)

.