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: 187

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

Christian Rheinnecker posted a status
"New Stuff @arduinoprx.de http://www.arduinoprx.de/"
4 hours ago
Profile IconWill Fleming, Elijah Patric, Paul R Avery and 4 more joined GarageLab (arduino, electronics, robotics, hacking)
5 hours ago
GarageLab posted blog posts
15 hours ago
Christian Rheinnecker posted a video

Arduino PrX : Sequencer AM808 VX3 - Recording a Beat to Atari Mega ST 2 in Cubase 3

In this video you see my AM808 VX3 Midi Sequencer Play a Beat and my Atari Mega ST 2 with Satan Disk NVDI 5 and Cubase 3 is recording a Beat in one Part. It ...
yesterday
Doug Hyde updated their profile
yesterday
Profile IconDoug Hyde, Stephen Gray, Mark Evans and 5 more joined GarageLab (arduino, electronics, robotics, hacking)
yesterday
GarageLab posted a blog post

Airburr Flying Robot Attaches to Walls

Airburr was developed at the Laboratory of Intelligent Systems, a branch of the European École…See More
Tuesday
GarageLab posted a blog post

Arduino robotic hand under $200

Robotic hand is one of most important parts in robot equipment. This is main manipulator that…See More
Monday
Profile IconDavid M Patterson, Jordan Hanger , Jason and 3 more joined GarageLab (arduino, electronics, robotics, hacking)
Monday
abhishek yadav posted a discussion

how to record time between two events using arduion

i have a disk rotating at variable speed(max rpm 600) . i want to know how much time it takes to…See More
Sunday
Danny Vigo updated their profile
Sunday
Danny Vigo commented on Fernando Gil's blog post Tutorial: Servomotor + Arduino
"Why would you want to connect them in parallel? Are you trying to increase the current? Perhaps to…"
Sunday
Danny Vigo replied to SATYA NARAYANA REDDY NIMMAKAYALA's discussion 5 stepper motor control code
"Well if you look in the examples that come included with the Arduino IDE you can find some code…"
Sunday
Profile IconEvaristo Ramos, Jr. and Dayan Martinez joined GarageLab (arduino, electronics, robotics, hacking)
Saturday
Christian Rheinnecker posted a status
"New Video Cypress Hill - Rap Superstar https://www.youtube.com/watch?v=VZSFgdrhzQQ"
Saturday
Christian Rheinnecker posted a video

Arduino PrX : Sequencer AM808 VX3 - Demo Cypress Hill - Rap Superstar Beat Play

Demonstration eines Beats auf dem AM808 VX3 Arduino Midi Sequencer Cypress Hill - Rap Superstar Weiter Infos auf http://arduinoprx.de
Saturday
Christian Rheinnecker posted a status
Saturday
Christian Rheinnecker posted a status
Saturday
Shannon Bradley replied to Dennis Melamed's discussion Little Gem Amp Circuit problems
"Im not sure why people turn to IC's for sound or power. I had this little amp kicking around…"
May 17
GarageLab posted blog posts
May 17

© 2013   Created by Marcelo Rodrigues.

Badges  |  Report an Issue  |  Terms of Service