Help needed with strings, please

I have a function loop which reads date and time elements from an RTC and returns three variables: years, months and days. Each of these is an unsigned integer in decimal format.

I want to merge these into the form YYMMDD, and put this into a character array where I will change individual elements (characters) later in the programme.

I have tried several approaches so far, but they all lead to errors or warnings about 'deprecated conversion'. Currently I have:

  date[0] = getDate.years/10 + '0';
  date[1] = getDate.years%10 + '0'; 
  date[2] = getDate.months/10 + '0';
  date[3] = getDate.months%10 + '0';
  date[4] = getDate.days/10 + '0';
  date[5] = getDate.days%10 + '0';

There's obviously something about the language I just haven't grasped. Can anyone advise, please?

Assuming you have year, month and day as ints:

char date[7];
sprintf(date, %02d%02d%02d", year, month, day);
Serial.print("The date is: ");
Serial.println(date);

Wow, that's concise! Many thanks. I have a lot to learn in C++.

As a matter of interest, is it possible at all using the Arduino programming language?

Thanks again.

J.

snusmumriken:
As a matter of interest, is it possible at all using the Arduino programming language?

There is no "Arduino programming language"

Arduinos are programmed using C++.

The "Arduino language" includes some macros and functions to do the I/O and timers on the chip. The syntax that is explained in this Arduino site is a subset of C++, choosen to be a balance between enough to usefull without so many variations that it confuses. The compiler behind the IDE is a full fledged C++ compiler.

The sprintf() function is a normal function available in C/C++. There are lots more - check up a standard C++ reference. Note that using these "powerfull" functions does include lots more code in your sketch.

(NB: The IDE does a bit of "mangling" of your source (ie it edits it slightly) before passing it to the compiler so you are free to declare functions in any order. The downside is multifile and scope rules are slightly altered.)

Meanwhile, back to the original question. I made a similar test program, and I get no warnings. I think your are not using the library call correct. On my DS1307 library the code would be

#include "DS1307.h"
char date[7] ;
  :
  RTC.getTime() ; // get the chip info into the variables year, month, day
  date[0] = RTC.year/10 + '0';  // convert the 1st digit of the year
  :

The "RTC" is the class instantiation of the RTC chip (the name is hardwired in the library). I call update (a method) of the RTC object and then use the variables (property) of the RTC object.

... oh yes, I forgot to add

  :
  date[6] = 0 ;  - to make the array null terminated.

Which makes it a string (with lowercase 's'. Using String (with uppercase S) is a whole different subject - dont go there)

Many thanks, M2. I have not used a DS1307.h library, mainly because I didn't know about it, but also because I have been able to access the chip data OK so I never thought to look for a library. I have put the RTC access routine into a function called getDate.

Your way of handling the data into a char array seems to be the same as mine (assuming RTC.year is an integer?), so so it looks as though my problem is more likely to do with the way I am using the function. I confess I am still rather foggy about that after reading the reference material on the Arduino site. I'll have another go at it.

Thanks too for the tip about the final array element. I had dimensioned it correctly with 6 elements, and populated elements 0 to 5 with "YYMMDD" to help with debugging, but hadn't thought to set that last one to 0.

I looked into Strings and read a few forum posts about them. Felt very insecure, so I backed out again before my original post!

For the record, it turns out I was indeed mis-using the function. I should have been passing arguments to the function by reference rather than by value, as explained at http://www.cplusplus.com/doc/tutorial/functions2/. My edited code now compiles and works, although I still get warnings about 'deprecated conversion' - so I shall substitute code using sprintf as suggested by Arrch.

It seems that explicitly filling the trailing element with a zero was unnecessary in my case, because this was done automatically when I initialised the (adequately dimensioned) char array with dummy characters from a string literal.

Thanks again to all.

There is no "Arduino programming language"

While discouraged__*__ often, an Arduino way to do this is with substrings and the setCharAt() function:

Example: http://arduino.cc/en/Tutorial/StringCharacters

* Because there are more uC resource friendly functions to handle this kind of thing, such as sprintf(). However, many programmers will like the syntax of the String functions as they are similar to other languages they may know.
The above being said, exercise caution and check you SRAM to ensure you do not get digital constipation.

A memory 'free' utility:

int freeRam () {
  extern int __heap_start, *__brkval; 
  int v; 
  return (int) &v - (__brkval == 0 ? (int) &__heap_start : (int) __brkval); 
}

Usage:

  Serial.print(F("  Free RAM = ")); Serial.println(freeRam()); Serial.println();

Ray