Create a string out of char array VirtualWire

I'm working on my first Arduino project (Sketch). I am using a wireless transmitter and receiver to send a message between two Arduino's. I have found plenty of examples of how to setup and send the message using VirtualWire. This all has worked out well.

Link to example: How to use 315Mhz RF transmitter and receiver modules with arduino - BuildCircuit.COM

I need to parse the incoming message on the receiving end so I can send a signal to the appropriate pin to go to high or low (string compare and yes I would like for it to be a string). I have figured out how to send the messages I want and I can receive the messages. Now I need to find out how to assign this char array into a sting (I think I'm using the correct terminology here). Here is what the code looks now:

void loop()
{
if (vw_get_message(message, &messageLength)) // Non-blocking
{
Serial.print("Received: ");
for (int i = 0; i < messageLength; i++)
{
Serial.write(message*);*
}
Serial.println();
}
}
Here is some really bad pseudocode for the concept I'm going for.
{
Serial.print("Received: ");
for (int i = 0; i < messageLength; i++)
{
Serial.write(message*);*
{
completeMessage = message;
Once I have the message in string format I think I can figure out the rest.
Thanks.

Lonestar76:
Once I have the message in string format I think I can figure out the rest.

A c-style string (as opposed to a String) is an array of characters terminated by a NULL.

I would expect that "message" is already in a string format but just in case if you do:

message[messageLength] = '\0';

after you receive it then It will be for certain.

In case it helps take a look at http://www.learncpp.com/cpp-tutorial/66-c-style-strings/.

Hope this helps,

Brad
KF7FER

EDIT: Note that to print a string (or even a String) you don't need to loop, just do:

Serial.print(message);    // Or println

EDIT #2: Well you did ask for a "string" not a "String". There is a huge difference even if you may think I'm being picky.

If you'd like to create a String from a character array, try something like:

if (vw_get_message(message, &messageLength)) // Non-blocking
{
  String stringMessage = message;
  Serial.print("Received: ");
  Serial.println(stringMessage);

Just in case that's really what you wanted :slight_smile:

If you'd like to create a String from a character array, try something like:

First, you still need to NULL terminate the array.
Second, don't. There is NOTHING that you can do with a String that you can't do, just as easily, with a string.

PaulS:

If you'd like to create a String from a character array, try something like:

First, you still need to NULL terminate the array.
Second, don't. There is NOTHING that you can do with a String that you can't do, just as easily, with a string.

BIG PLUS ONE ON BOTH COUNTS!

Lonestar, depending on what Arduino you have C++ capital-S Strings are bad or real bad news in terms of memory and excess clock cycle waste. They use extra bytes. They copy themselves even to add just 1 char then erase the old copy leaving a hole in your heap, all at cost in cycles and all just to package text in a BASIC kind of way.

Learn C char array lowercase-s strings from a C site if you can, or get a C book at a used book store or Amazon.

Here is the <string.h> library reference to the GCC underlying Arduino:
http://www.nongnu.org/avr-libc/user-manual/group__avr__string.html
And the page with all the standard libraries:
http://www.nongnu.org/avr-libc/user-manual/modules.html
A strong learner might not need more or much more except for tests and practice, practice, practice.

In time you may learn to parse and lex without buffering incoming text but leave that for later.