Virtual Wire message problem

Hi all,

I'm working with an rf transmitter and receiver kit to send a message between two Arduino's.
In my transmitter code I am trying to catch the whole message into a variable so I can use this outside
the for loop scope.

At this moment the message is split in an array. So for example if I send the message: "Hello World" it will send it each char at a time. Message[1] will be "H", Message[2]. will be "E" etc. (If I understand it correctly)
How can i paste these characters into one variable so I can use the whole message outside the scope?
I have been trying this for hours, but i cant find any good solution.

The code I used is from: How to use 315Mhz RF transmitter and receiver modules with arduino - BuildCircuit.COM

The code to catch the received message:

if (vw_get_message(message, &messageLength)) // Non-blocking
{
Serial.print("Received: ");
for (int i = 0; i < messageLength; i++)
{
Serial.write(message[i]);
}

Anonymouss:
How can i paste these characters into one variable so I can use the whole message outside the scope?

They are already in one variable, albeit an array, but it depends on what you want to do with it. For example, you can add an extra space in the array for the null-terminator, and use it with the string.h functions.

For example, you can add an extra space in the array for the null-terminator, and use it with the string.h functions.

Make sure, of course, that you actually put the NULL terminator in that space.

I want the message to be a String so I can use it outside the for loop scope. What is the best way to achieve this?

Anonymouss:
I want the message to be a String so I can use it outside the for loop scope. What is the best way to achieve this?

Not use a String and convert it to a string by adding an extra space in the message array and tacking on '\0' to the end of the message. Viola! You now have a string!

What is the best way to achieve this?

Why? There are very few reasons for using Strings instead of strings.

Arrch:

Anonymouss:
I want the message to be a String so I can use it outside the for loop scope. What is the best way to achieve this?

Not use a String and convert it to a string by adding an extra space in the message array and tacking on '\0' to the end of the message. Viola! You now have a string!

Can you give an example code? I still dont understand how I can convert the message to a string and use it outside the scope. Do you mean adding '\0' at the receiver or transmitter side of the code?

I still dont understand how I can convert the message to a string and use it outside the scope.

Outside of what scope?

char string[36];

void someFunc()
{
  if (vw_get_message(message, &messageLength)) // Non-blocking
  {
      message[messageLength] = '\0'; // Append a NULL;
      if(messageLength < 35) // make sure that the string will fit
      {
         strcpy(string, (char *)message);
      }
  }
}

Now, the received message will be available in the global variable string AS A string.

Thank you! it works!