I'm trying to extract ints from a String on an Arduino.
I'm using This DCF77 Library (.zip) and I get a date and time in this format:
DCF77DATA 2012012531819CET
This translates ito
DCF77DATA YYYYMMDDwhhmmTZ
with Y,M,D being the date, w the day of the week, and h and m being the time. This is always generated at the full minute, so seconds can be expected to be 0.
I've been trying various ways to get the hours and minutes (the date doesn't concern me at the moment) and put them into int variables. The latest method I tried looks like this:
const char *v = dcf77.getDateTime();
if (strcmp (v,"DCF77POLL") != 0) {
Serial.println(v);
String time = String(v);
// DCF77DATA 2012012531819CET
char h_str[3];
time.substring(19,20).toCharArray(h_str, 2);
Serial.print("H: ");
Serial.println(h_str);
char m_str[2];
time.substring(21,23).toCharArray(m_str, 2);
Serial.print("M: ");
Serial.println(m_str);
int hours = atoi(h_str);
int minutes = atoi(m_str);
}
This produces an interesting result ... I get the first number of the time. When it's "23" minutes I get "2", hence the h_str[3]. I thought it was related to the string being 0-terminated, but it didn't help. Same for the hour.
Additionally, when I try to print out the int I don't get any output at all in the serial console, it seems as if the program just halts.