Combine values into int, array, char, strings, etc.

Ok, I have googled for the past 2 days and still have no answer to what I would think is an easy question.

I have values, predetermined from other parts of code, and I wish to combine them all into a single char, but have yet to find anywhere that will explain how to combine text together.

In the end, I wish to have,

char timestamp[27] = ("2015-07-01 WED 12:31 
");

This above line is used to write to a log file that I am using for another project.

I have values:

year =  2015
month = 7
monthday = 1
weekday = 4
hour = 12
minute = 31

I wish to take all these values and combine them all together to make a char string. But what I have tried does not work at all.

timestamp[27] = (year, "-", month, "-", monthDay, " ", weekDay, " ", hour, ":", minute,  "
");

That gives me bracket errors.

I have tried to build it as a string array:

String timestampbuild;                                                                                      
  timestampbuild = (year, "-", month, "-", monthDay, " ", weekDay, " ", hour, ":", minute,  "
");         
  timestampbuild.toCharArray(timestamp, 27);

But that just ends up with a Screen.println =

How can I combine all the values that are already determined into a char array?

There is a C function called sprintf(), look into it.

"2015-07-01 WED 12:31 "

char data[25];
char * DayOfWeek[7] = {"SAT", "SUN", "MON", "TUES", "WED", "THURS", "FRI"};
// your other variables here

sprintf(data, "%d-%02d-%02d %s %02d:%02d 
", year, month, monthday, DayOfWeek[weekday], hour, minute);
Serial.println(data);

Example: http://cpp.sh/8tpu

It's {} not () !

Mark

holmes4:
It’s {} not () !

Mark

If I change timestampbulid from () to {}, I get

D:\Info\Arduino\arduino-1.6.5-r2\hardware\arduino\avr\cores\arduino/WString.h:86:11: note: String& String::operator=(const String&)
String & operator = (const String &rhs);
^
D:\Info\Arduino\arduino-1.6.5-r2\hardware\arduino\avr\cores\arduino/WString.h:86:11: note: no known conversion for argument 1 from ‘’ to ‘const String&’
D:\Info\Arduino\arduino-1.6.5-r2\hardware\arduino\avr\cores\arduino/WString.h:87:11: note: String& String::operator=(const char*)
String & operator = (const char cstr);
^
D:\Info\Arduino\arduino-1.6.5-r2\hardware\arduino\avr\cores\arduino/WString.h:87:11: note: no known conversion for argument 1 from ‘’ to 'const char

D:\Info\Arduino\arduino-1.6.5-r2\hardware\arduino\avr\cores\arduino/WString.h:88:11: note: String& String::operator=(const __FlashStringHelper*)
String & operator = (const __FlashStringHelper str);
^
D:\Info\Arduino\arduino-1.6.5-r2\hardware\arduino\avr\cores\arduino/WString.h:88:11: note: no known conversion for argument 1 from ‘’ to 'const __FlashStringHelper

Mail_Box_Phase_03:469: error: expected primary-expression before ‘/’ token
Mail_Box_Phase_03:471: error: expected primary-expression before ‘}’ token
Mail_Box_Phase_03:471: error: expected ‘;’ before ‘}’ token
no match for ‘operator=’ (operand types are ‘String’ and ‘’)

HazardsMind: There is a C function called sprintf(), look into it.

Yours is a good idea, and I was able to make it work. But I am very limited on ram room. sprintf is known to take a good bit of ram, ram that I don't have to work with. :(

To concatenate Strings, you would use the + operator.

String Crap = “Garbage” + " " + “Manure”;

eqrunner: Yours is a good idea, and I was able to make it work. But I am very limited on ram room. sprintf is known to take a good bit of ram, ram that I don't have to work with. :(

Post your full sketch and lets see if we can shave it down.

Do you really need to combine the values into a single variable ? Why not just write the individual values to the log file ?

UKHeliBob: Do you really need to combine the values into a single variable ? Why not just write the individual values to the log file ?

It might because of how I think. grew up with mIRC codeing Plus I like to have a single variable that contains all the info. See below for full code of this current project.

The project in the end is for a mail box opening logger. Aka, when the mail box is opened, the arduino sees this as a 'button press' and logs the time and date to it's log file. This log file can be read via a local webpage.

arduino + ethernet sheild w/ SDcard slot + RTC chip

Full code: still a work in progress. http://pastebin.com/2G3nL0if

Current code: Sketch uses 27,804 bytes (86%) of program storage space. Maximum is 32,256 bytes. Global variables use 1,727 bytes (84%) of dynamic memory, leaving 321 bytes for local variables. Maximum is 2,048 bytes. Low memory available, stability problems may occur.

This will save you some RAM.

All your Serial.println("..."); take up RAM. To fix this, use the F() macro.

Serial.println( F("I will now be stored into the Flash Memory, saving you RAM!") );

I should be a little more clearer. Only use the F() macro on strings that are not going to be changing, so nothing like this.

int counter = 345;
Serial.println( counter ); //This is ok
Serial.println( F(counter) ); //This is not ok

HazardsMind: I should be a little more clearer. Only use the F() macro on strings that are not going to be changing, so nothing like this.

int counter = 345;
Serial.println( counter ); //This is ok
Serial.println( F(counter) ); //This is not ok

Well that is both helpful and confusing. That is one thing I have grown to dislike with arduino. Things sometimes works except when they don't in some cases when something else is used instead of what didn't work. /s

Going back to the original question.

Yes your string from above does work:

char data[25];
char * DayOfWeek[7] = {"SAT", "SUN", "MON", "TUES", "WED", "THURS", "FRI"};
// your other variables here

sprintf(data, "%d-%02d-%02d %s %02d:%02d 
", year, month, monthday, DayOfWeek[weekday], hour, minute);
Serial.println(data);

My question to you, is can you futher explain how the %d %02d and %s work?

%(type) just drops your data of that type into the string to be.

%d = decimal, so anything that is a number within a 16 bit range, like byte or int (16 bit on an Arduino) %ld = long decimal, variables that are type long (32bit). Im not sure if 64bit variables work, I never actually tried it before, never needed to.

%02d = formatted decimal and the [u]02[/u] represents two placeholders with the value 00, so %03d = 000.

Addition: %2d does the same thing but does not show you the zeros, instead it just shows a blank space however many times.

%s = strings (I believe it does both char pointers and String objects)

Link: http://www.cplusplus.com/reference/cstdio/printf/

It might because of how I think. grew up with mIRC codeing

I grew up using BASIC but I don't use line numbers and GOTO

I like to have a single variable that contains all the info.

Why, unless you are going to use it for something apart from writing to the log file ?

UKHeliBob:
IWhy, unless you are going to use it for something apart from writing to the log file ?

Probably because I think in lines not individual characters. I know my script has to write the date/time string to two files, and I would see it as redundant and a waste of space if I were to have it open one file, write it character by character, then close, then repeat with the second one. This way I can put it in a single char array, and then reuse that char array to do both writes, along with serial print.

Well that is both helpful and confusing

What's confusing?

This way I can put it in a single char array, and then reuse that char array to do both writes, along with serial print.

Point taken. sprintf() will do what you want but will use a comparatively large amount of memory.

I know my script has to write the date/time string to two files, and I would see it as redundant and a waste of space if I were to have it open one file, write it character by character, then close, then repeat with the second one.

Are you having to do this many hundreds of times a second, or is te processor busy doing other things? If not, I can't see the problem.

AWOL: Are you having to do this many hundreds of times a second, or is te processor busy doing other things? If not, I can't see the problem.

No for neither. For this project, the void loop is just paying attention to a pin (button) . When the button is pushed, it then goes to the RTC, grabs the current time, and writes the time to a log file. Then goes back to being dorment until either the button is pushed, or someone pings it webpage to view the log.

I cant really figure out any other way to write to the log.txt file other than building the line I want to write in the sprintf(), then writing that sprintf() to the log.txt file.

I can't figure out how I can write it character by character and still get it to display the way I want( Ex: Year-Month-Day DayofWeek Hour:minute)

I cant really figure out any other way to write to the log.txt file other than building the line I want to write in the sprintf(), then writing that sprintf() to the log.txt file.

What is wrong with using sprintf() apart from it needing memory to work ?

If you can’t/won’t use sprintf() then I am afraid that you are stuck with writing each item separately to the file(s) or screen. If that sounds messy then write the code once, put it in a function and call it when you need it.