Store time in variable using RTC module DS3231

Status
Not open for further replies.

Kaas

New member
Hello,

I want to obtain the current hours that my RTC DS3231 outputs at a certain time.
I am using a script that comes with the library that I am using.
The module is connected to a Teensy 3.6.
The only addition that I've made in the code is visible at the end of the void loop().

Code:
int Uur;

// Date and time functions using a DS3231 RTC connected via I2C and Wire lib
#include <Wire.h>
#include "RTClib.h"

RTC_DS3231 rtc;

char daysOfTheWeek[7][12] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};

void setup () {

#ifndef ESP8266
  while (!Serial); // for Leonardo/Micro/Zero
#endif

  Serial.begin(9600);

  delay(3000); // wait for console opening

  if (! rtc.begin()) {
    Serial.println("Couldn't find RTC");
    while (1);
  }

  if (rtc.lostPower()) {
    Serial.println("RTC lost power, lets set the time!");
    // following line sets the RTC to the date & time this sketch was compiled
    rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
    // This line sets the RTC with an explicit date & time, for example to set
    // January 21, 2014 at 3am you would call:
    // rtc.adjust(DateTime(2014, 1, 21, 3, 0, 0));
  }
}

void loop () {
    DateTime now = rtc.now();
    Serial.print(daysOfTheWeek[now.dayOfTheWeek()]);
    Serial.print(now.hour(), DEC);
    Serial.print(':');
    Serial.print(now.minute(), DEC);
    Serial.print(':');
    Serial.print(now.second(), DEC);
    Serial.println();

// My own addition which should store the hours in variable "Uur".
    Uur = (now.hour(), DEC);
    Serial.println(Uur);
    delay(1000);
}

The problem is that my console doesn't show the actual hours. In the console is visible how the hours are 16. Whenever the hours change from 16 to for example 17, my code still outputs 10 for the variable "Uur".

Code:
Sunday16:20:52
10

It has been a while that I also used this particular code on an Arduino uno. As far as I can remember this code worked on the Arduino.
Could someone please help me with my problem?

Thank you so much!
 
Serial println converts automatically hex to dec, the latter being the default format. Thus, your code Uur = (now.hour(), DEC) makes no sense at all.

Uur = now.hour();
Serial.println(Uur);

should work.
 
Status
Not open for further replies.
Back
Top