Hi Everyone,
I have been trying to get a variable to show 24h hour time like this: (1325) from one library found here.
All I seem to get on my variable is a totally diffrent number to what the time is set.
The time will be use to trigger if...else statements.
Here's some demo code with my issue:
/*
DS3231: Real-Time Clock. Date Format
Read more: www.jarzebski.pl/arduino/komponenty/zegar-czasu-rzeczywistego-rtc-ds3231.html
GIT: https://github.com/jarzebski/Arduino-DS3231
Web: http://www.jarzebski.pl
(c) 2014 by Korneliusz Jarzebski
*/
#include <Wire.h>
#include <DS3231.h>
DS3231 clock;
RTCDateTime dt;
int Time24;
void setup()
{
Serial.begin(9600);
// Initialize DS3231
Serial.println("Initialize DS3231");;
clock.begin();
//Set time to:
clock.setDateTime(2014, 4, 13, 21, 07, 00);
}
void loop()
{
dt = clock.getDateTime();
Serial.print("I Want This Output: ");
Serial.println(clock.dateFormat("Hi", dt));
Serial.print("But I get this & the time was set to 2107: ");
Time24 = clock.dateFormat("Hi", dt);
Serial.println(Time24);
Serial.println();
delay(1000);
}
The linked library's dateFormat function returns char*, not integer. Do you get a clean compile? What if you turn compiler warnings to all in preferences?
so this looks odd:
Time24 = clock.dateFormat("Hi", dt);
According to the library's front page (here) capital 'H' in the format string prints out hours in the 24hr clock format and lower case 'h' gives hours in the 12hr clock format so you should change this line:-
erial.println(clock.dateFormat("Hi", dt)); // outputs 1325
to this:-
erial.println(clock.dateFormat("hi", dt)); // outputs 0125
You might want to add the am/pm indicator to make the line:-
erial.println(clock.dateFormat("hia", dt)); // outputs 0125pm
If you want to lose the leading zero use 'g' instead of 'h'.
As in most things in programming case is important.
Ian
wildbill:
The linked library's dateFormat function returns char*, not integer. Do you get a clean compile? What if you turn compiler warnings to all in preferences?
so this looks odd:
Time24 = clock.dateFormat("Hi", dt);
Hi wildbill,
I get a clean compile even with that setting applied.
Is there a way to get this to work in a variable? If change the datatype to char it outputs a back to front question mark '?'.
Can anyone give an example on how to output this to a int variable in 24h time (eg. 1806 = 6:06pm)
IanCrowe:
According to the library's front page (here) capital 'H' in the format string prints out hours in the 24hr clock format and lower case 'h' gives hours in the 12hr clock format so you should change this line:-
erial.println(clock.dateFormat("Hi", dt)); // outputs 1325
to this:-
erial.println(clock.dateFormat("hi", dt)); // outputs 0125
You might want to add the am/pm indicator to make the line:-
erial.println(clock.dateFormat("hia", dt)); // outputs 0125pm
If you want to lose the leading zero use 'g' instead of 'h'.
As in most things in programming case is important.
Ian
Hi IanCrowe,
I'm all good with printing of formats etc. my issue is to get this format line into a variable so I can program timer/triggers against it.
Anyone got any ideas how I can get this char function output to a variable that I can use in If statements.
I can only seem to use it in Serial.print
Thanks in advance
Try getDateTime - it returns a RTCDateTime struct which has members for hour & minute. No need to parse it out of the string you're using.
Got it!
#include <Wire.h>
#include <DS3231.h>
DS3231 clock;
RTCDateTime dt;
char Time24 [16];
uint8_t Min, Hour;
void setup()
{
Serial.begin(9600);
// Initialize DS3231
Serial.println("Initialize DS3231");;
clock.begin();
clock.setDateTime(2014, 4, 13, 6, 4, 00);
}
void loop()
{
dt = clock.getDateTime();
Min = dt.minute;
Hour = dt.hour;
sprintf_P(Time24, PSTR("Time: %02u%02u\n"), Hour, Min);
Serial.print (Time24);
delay(1000);
}
[/code