Sorry for the stupid question, but i'm just looking for another set of eyes, perhaps someone can tell me how can i get this going...

String mystring="";
void setup()

void loop()
mystring="The ADC value is: " + analogRead(A15);

void sendlog(const char* message)
//do whatever to send this to logging SD...

while compiling, i get the following: 

error: cannot convert 'String' to 'const char*' for argument '1' to 'void sendlog(const char*)'

I understand, this is an expected error, but how to i fix this? i want to get away from using String object altogether. The sendlog is a part of other library, which i do not want to modify, but looking to fix my code instead... Anyone has any idea? I tried all different option starting from .toCharArray and ending with strcpy and alike to fix this, but it seems, that nothing is being passed to the sendlog in the end. so, i need to append a string of characters with int, and send it to the sendlog as a char*. This must be very simple, and this is one of those, where i do not seem so see a very obvious solution... Help anyone?


Views: 10853

Reply to This

Replies to This Discussion

First, if you try to append a non-string value to a string it'll get weird.  Instead of

mystring="The ADC value is: " + analogRead(A15);

you should use

mystring="The ADC value is: " + String(analogRead(A15), DEC);

to append a new String (created from a decimal value) to your existing String.  Note that the original method won't cause a compile error, but what you get over the serial port will likely be garbage.  (What you're doing is adding the value read from the analog port to the memory location of mystring, and it'll pass that to println which will read from that incorrect point until it encounters a null byte.)

Second, you have to break the String object back to an array of chars, and to do that you have to have an array of chars to put it in.  If you know what the length of your string is going to be, or at least some sort of maximum range, it's pretty easy.  We know that your string will only be 23 characters long ("The ADC value is: " = 18, "1023" [the max analogRead() value] = 4, and one extra for the terminating null character), so our array only has to be that big.  If you change the wording you'll have to resize the array.

char otherString[23]; //char array to copy the String into

mystring.toCharArray(otherstring, mystring.length() + 1); //Copy the string (+1 is to hold the terminating null char)

sendlog(otherString); //send our char array copy of the String object to sendlog()

Char arrays don't offer all the flexibility that a String object does, and they're sometimes kind of hard to work with, but they're a lot smaller and faster (they only take up as much space as there are characters).

I hope this helps you out.  You could achieve the same outcome using only a char array, but the String object is so much easier to use, especially for converting a number to a string and appending one string to another.

I keep reading in the Arduino reference that you can use "Constant String" + analogRead(), myString += 123, and similar constructions, but whenever I do I get random-length garbage.  Using the String constructor to concatenate a number to a string works though.  Who knows.


Latest Activity

Avtar Singh is now a member of GarageLab (arduino, electronics, robotics, hacking)
Profile IconPete Z, Anatolio Filho and ashok joined GarageLab (arduino, electronics, robotics, hacking)
Profile IconEmrah Ponjevic and Sergio Salazar joined GarageLab (arduino, electronics, robotics, hacking)
autoelectric is now a member of GarageLab (arduino, electronics, robotics, hacking)
Oct 14
Gary Plaza is now a member of GarageLab (arduino, electronics, robotics, hacking)
Oct 13
James Anderson is now a member of GarageLab (arduino, electronics, robotics, hacking)
Oct 10
Adan Salinas commented on Marcelo Rodrigues's blog post SwagBot
"Great use of technology right there."
Oct 5
Ojasvi Gupta left a comment for Olivy Gis
"Something is fishy here....what do you wish to talk about...if you could be a tad more specific?"
Oct 5
Profile IconDave Petersen, Olivy Gis, gian crespo and 1 more joined GarageLab (arduino, electronics, robotics, hacking)
Oct 5
Olivy Gis updated their profile
Oct 5
Alaa Alrashdan commented on GarageLab's blog post Tutorial: How to use the Monster Moto Shield
"Hi I have a firgelli actuator that i run using arduino+monster moto shield. I can run it with a…"
Oct 4
Richard Reubenstone posted a blog post

Some great maker projects you can join

If you are interested in joining some teams consisting of makers working together from all over the…See More
Oct 4
Dmitry Slepov posted a blog post

Tibbo Project System-based Office Aircon Controller Application

The app allows you to manage traditional air conditioners that are controlled through infrared…See More
Oct 4
Marcelo Rodrigues left a comment for Jose Gustavo Abreu Murta
"Spammer member suspended. Thanks!"
Oct 2
Profile IconCharles Howey, Neil Besner and Jim Ory joined GarageLab (arduino, electronics, robotics, hacking)
Oct 2
Roosevelt Sebial left a comment for Roosevelt Sebial
"I have left a message to your given e-mail address Mr. Frank. Thanks!"
Sep 29
Eric Delgado updated their profile
Sep 29
Jose Gustavo Abreu Murta left a comment for Jose Gustavo Abreu Murta
"SPAM !!!!"
Sep 29
Selig Plapp updated their profile
Sep 29
Alexander Perez updated their profile
Sep 29

© 2016   Created by Marcelo Rodrigues.   Powered by

Badges  |  Report an Issue  |  Terms of Service