Getting hour from DS3231

Hello all!

I am trying to get the Hour and minutes only to use for a statment like this( lampState = (hour*60) + min;)

if (lampState >= 360 && lampState < 120){
digitalWrite (lightingPin, LOW);
digitalWrite (bulblightingPin, LOW);
Serial.println(“Lights ON”);

however after reading through the library i have tried t.hour and various other but no luck.
Is this even possible with the libraries i have?

i have looked at the link but is it possible with the library i have attached?

https://tronixlabs.com.au/news/tutorial-using-ds1307-and-ds3231-realtime-clock-modules-with-arduino/

DS3231.cpp (9.71 KB)

DS3231.h (3.04 KB)

They don’t offer a very high level interface but have

void readDS3231time(byte *second,
byte *minute,
byte *hour,
byte *dayOfWeek,
byte *dayOfMonth,
byte *month,
byte *year)
{
  Wire.beginTransmission(DS3231_I2C_ADDRESS);
  Wire.write(0); // set DS3231 register pointer to 00h
  Wire.endTransmission();
  Wire.requestFrom(DS3231_I2C_ADDRESS, 7);
  // request seven bytes of data from DS3231 starting from register 00h
  *second = bcdToDec(Wire.read() & 0x7f);
  *minute = bcdToDec(Wire.read());
  *hour = bcdToDec(Wire.read() & 0x3f);
  *dayOfWeek = bcdToDec(Wire.read());
  *dayOfMonth = bcdToDec(Wire.read());
  *month = bcdToDec(Wire.read());
  *year = bcdToDec(Wire.read());
}

which basically will give you all you need (and more)

I use Adafruit’s RTClib (a fork of JeeLab's real time clock library) which gives you also classes to manipulate time span

Thank you I will check out that Lib and see if I can make it work that way :slight_smile:

Well what won’t work for sure is your if statement if (lampState >= 360 && lampState < 120){...}
As lampState can’t be at the same time greater than 360 AND less than 120… :)) (may be you meant 1200 ?)

Also I would not call that lampState as it does not reflect the state of your lamp, would call that variable timeStamp or currentMinute or minutesSinceMidnight - that would help understand the code better

If you use the other library you have a DateTime class and you Can do comparisons on dates directly

Ha light bulb moment (pardon the pun) thank you so much my maths was a bit frazzled. In my mind I assumed that the code would know when it rolled over between days but you were right it was 1200. I will re write this tomorrow and see how it goes . thank you :slight_smile: