Conversion doesn't work from String to Char array

Hi all. I have an arduino Uno R3 and I usually send COMMANDS via serial monitor, and my function receive the args and parses it in another function. So now I'm trying to call the same function (so I don't need 2 functions that do the same) by manually providing "fake serial command line arguments" as parameters to the function. But I can't get it to work, here is what it looks like:

void cmdAlarm(const char *args) // takes commands entered in serial monitor i. e. "ALARM 1 20:33" { ... }

Now, I do have a string that I'd like to send as argument to the function, but can't figure out how to get the string I have fitting as char array. Here are my tries: 1. String asmString = "ALARM 1 20:33" cmdAlarm(asmString); -> Results in error message error: cannot convert 'String' to 'const char*' for argument '1' to 'void cmdAlarm(const char*)'

2. String asmString = "ALARM 1 20:33" char TempTweet[asmString.length() + 1];

asmString.toCharArray(TempTweet, asmString.length() + 1); cmdAlarm(TempTweet);

-> Doesn't throw an error and compiles, but seems like my function doesn't know what to to with what I supply as arguments.

Any ideas what I'm doing wrong?

Ditch the Strings.

char* asmString = "ALARM 1 20:33";


Well the reason why I use string is because I "assemble" that string as follows:

asmString = "ALARM 1 "; asmString += tempHour; asmString += ':'; asmString += tempMinute;

Can I do the same with char* ????? I do that because I take the actual time, when my Alarm triggered and I add 5 minutes to it and set it again, so that my alarm triggered every 5min. I know, I could use timer/counter and all that, but I need to use the INT that comes from my RTC clock module in form of my alarm.

Any ideas?

Look at sprintf. You can assemble that string in one line with a char buffer.

char myString[20];

sprintf(myString, "ALARM 1 %02d:%02d", tempHour, tempMinute);

Yeah, it doesn’t work I just tried it as follows:

char* = asmString;
asmString = "ALARM 1 ";
asmString += tempHour;
asmString += ‘:’;
asmString += tempMinute;


// ====================

Here is the serial terminal out put I get:
Alarm number must be 1 or 2

Here the whole function:
void cmdAlarm(const char *args)
tmElements_t time;
alarmMode_t mode;

memset(&time, 0, sizeof(tmElements_t));

int posn = 0;
byte alarmNum = readField(args, posn, 2);
if (!alarmNum || alarmNum == 99) {
Serial.println(“Alarm number must be 1 or 2”);
while (args[posn] == ’ ’ || args[posn] == ‘\t’)
if (args[posn] != ‘\0’) {
// Set the alarm to a new value.
if (matchString(s_OFF, args + posn, strlen(args + posn))) {
mode = alarmModeOff;
} else {
time.Hour = readField(args, posn, 23);
time.Minute = readField(args, posn, 59);
if (time.Hour == 99 || time.Minute == 99) {
Serial.println(“Invalid alarm time format; use HH:MM”);
mode = alarmModeHoursMatch;
bool a1 = RTC.isAlarmInterupt(1);
bool a2 = RTC.isAlarmInterupt(2);
if (alarmNum == 1) {
a1 = true;
} else {
a2 = true;
if (a1 && a2) {
} else if (a1) {
} else if (a2) {
RTC.writeAlarm(alarmNum, mode, time);

// Print the current state of the alarm.
RTC.readAlarm(alarmNum, mode, time);
printAlarm(alarmNum, mode, time);

As you can see, the error message comes because of the if statement in the function shown above if(!alarmNum || alarmNum == 99), so that tells me my function “readField” returns 99 for some reason.
Here the readField Function:

byte readField(const char *args, int &posn, int maxValue)
int value = -1;
if (args[posn] == ‘:’ && posn != 0)
while (args[posn] >= ‘0’ && args[posn] <= ‘9’) {
if (value == -1)
value = 0;
value = (value * 10) + (args[posn++] - ‘0’);
if (value > 99)
return 99;
if (value == -1 || value > maxValue)
return 99;
return value;

Notice the debug println ??? Serial.println(value);
that one throws me the whole String into the serial terminal, for example: “ALARM 1 20:33” which looks totally fine to me.
I think the problem here lies within the string termination when I type it in the serial monitor and when I just assemble my string or char array respectively.

Now how do I print HEX into the serial terminal? I know println(asmString, HEX); but that throws a ton of errors.

I think I need to manually add a linefeed or 0 terminator to the end of my char array, but how would I do that?

Thanks, and sorry for the code-avalance here.

Thanks Delta_G, I really appreciate you taking the time and trying to help me. Truly generous of you.

Anyhow I just tried your last suggestion with the sprintf, but also I receive the serial monitor output "Alarm number must be 1 or 2"

I really believe I need to somehow terminate my char array

First, you should read the two posts at the top of this Forum by Nick Gammon. It contains guidelines for the proper way to post here, especially using code tags when you post code. Second, you don't understand how to define a character array. What is the name of the variable in this line from your code:

char* = asmString;

With a grand total of 4 posts, you say:

Yeah, it doesn't work I just tried it...

yet you ignore the number of posts Delta_G has and it doesn't even occur to you that you might be wrong? Did you even try his code? I did:

void setup() {
  int tempHour;
  int tempMinute;


  tempHour = 12;
  tempMinute = 30;
  char myString[20];

  sprintf(myString, "ALARM 1 %02d:%02d", tempHour, tempMinute);

void loop() {


and it worked perfectly for me. So, take whatever time it takes to understand this program, and once you do understand it, you should be able to work Delta-G's solution into your problem.

Studiologe: I really believe I need to somehow terminate my char array

Sorry, you're right. I missed a character.

char myString[20];

sprintf(myString, "ALARM 1 %02d:%02d\n", tempHour, tempMinute);

You might try something similar to the below.

char buffer[20];
asmString.toCharArray(buffer, 20);