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()
{
Serial.begin(9600);
}

void loop()
{
mystring="The ADC value is: " + analogRead(A15);
Serial.println(mystring);
sendlog(mystring);
delay(10000);
}

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?

--Andy

Views: 637

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.

RSS

Latest Activity

GarageLab posted blog posts
1 hour ago
Javier Nogueras is now a member of GarageLab (arduino, electronics, robotics, hacking)
3 hours ago
GarageLab commented on GarageLab's blog post Tutorial: Using GPS Shield as an Anti-Theft Device
"Hello Pierre, Yes, You can do it. Use a GPS module like one…"
21 hours ago
Amy Ciesielka posted a blog post
yesterday
Dam Srasquasz commented on GarageLab's blog post Tutorial: Using GPS Shield as an Anti-Theft Device
"@up you can buy any shield you want for example: click sending text messase is easy and…"
yesterday
Profile IconMarek Florianczyk, Amy Ciesielka and Dam Srasquasz joined GarageLab (arduino, electronics, robotics, hacking)
yesterday
David Braithwaite posted a discussion

Robot, motor, relay kits

Hi,I have been playing around with bots for a few years now, I have found these guys that sell a…See More
Tuesday
David Braithwaite replied to derrrick wiltshire's discussion dc motor (small 5v) reverse direction control circuit
" Hi, Check these guys out for these kits, Rk Online Store Laters "
Tuesday
Chas Stearns updated their profile
Monday
GarageLab posted a blog post

Real-life Spider-Man webshooter uses a coil gun instead of web fluid

In honor of The Amazing Spider-Man 2 opening tomorrow in Germany (among other places), German laser…See More
Apr 17
Shannon Bradley and Marcelo Rodrigues are now friends
Apr 16
Terry Renner is now a member of GarageLab (arduino, electronics, robotics, hacking)
Apr 16
Andy S posted a discussion

Arduino MEGA boolean?

Hi everyone,been banging my head against the wall a few days now.i have a small project, where i…See More
Apr 16
Bobby Marshall posted a video

SELFIE Mirror

Meet the S.E.L.F.I.E., the "The Self Enhancing Live Feed Image Engine". Find out how it works: http://istrategylabs.com/2014/04/take-an-instant-selfie-with-t...
Apr 15
GarageLab posted a blog post
Apr 15
pj jones posted a status
"Designing an Home Automation Project"
Apr 14
Profile Iconpj jones, Robert Kribel and Lars Goldschlager joined GarageLab (arduino, electronics, robotics, hacking)
Apr 14
Fontaine Pierre commented on GarageLab's blog post Tutorial: Using GPS Shield as an Anti-Theft Device
"Hi, Is it possible to make a car tracker with this GPS + GSM shield to send a message if limits…"
Apr 11
GarageLab posted a blog post
Apr 11

© 2014   Created by Marcelo Rodrigues.

Badges  |  Report an Issue  |  Terms of Service