Varible Conversion & Serial question

im trying to send a incomming serial message over RF. but when i try to compile the code i get an error like: invailed conversion from int to const char*
and than highlights this line:

const char* msg = Serial.read();

i have tried to use char(Serial.read());
but than the compiler tells me that i cannot convert from char to const char*
so i removed const since it isn't contante (i think) but still get error because of the "*" (star)

i just want to put the incomming serial message into the msg
anyway please note that i am using VirtualWire and therfore have trouble when chaning the var type of msg

my total code:

#include <VirtualWire.h>

void setup()
{
  //Setup VirtualWire to transmit on 4
  vw_setup(100);
  vw_set_tx_pin(4);
  
  pinMode(13, OUTPUT);    //Power LED
  digitalWrite(13, HIGH); //Always ON
  
  //setup a serial connection
  Serial.begin(9600);
}

void loop()
{
  if(Serial.available() > 0)
  {
    const char* msg = Serial.read();
    if(*msg != 'for' ||
       *msg != 'bac' ||
       *msg != 'lef' ||
       *msg != 'rig' ||
       *msg != 'xxx')
       {Serial.println("ERROR SENDING:");Serial.println(msg);}
    else
       {
         vw_send((uint8_t *)msg, strlen(msg));
       }
  }
}

thanks alot already :wink:
nick

Serial.read returns a single int, not a pointer to a char.

Serial.read returns a single int, not a pointer to a char.

If you ALWAYS call Serial.available() first, and only call Serial.read() IF there is data available, the return value can be stored, safely, in a char. Still not the same as a char *, though. You can, of course, store the char that you get in an array, along with several others, to make a string.

yes, i've read all that, but now i come to the question how do i do that,
how do i store the Serial data to a Array and than convert it to a string..?
btw, dont you get an error when compiling a string to a char?

thnx
nick

yes, i've read all that, but now i come to the question how do i do that,
how do i store the Serial data to a Array and than convert it to a string..?
btw, dont you get an error when compiling a string to a char?

First thing you need to do is to learn when to use a capital letter, and when not to. You can create an array, not an Array.

Second thing you need to do is to define exactly what constitutes a packet. What do you want to put in the NULL terminated array of chars, also known as a string? Specifically, when do you start and stop reading?

Yes, you get a compiler error when comparing a string to a char. They are different things, and will never be equal. You'd get the same error comparing a string and a float or an int.

If you add/use start and end of packet markers, like you should, you can read serial data and store it in an array using code like this:

#define SOP '<'
#define EOP '>'

bool started = false;
bool ended = false;

char inData[80];
byte index;

void setup()
{
   Serial.begin(57600);
   // Other stuff...
}

void loop()
{
  // Read all serial data available, as fast as possible
  while(Serial.available() > 0)
  {
    char inChar = Serial.read();
    if(inChar == SOP)
    {
       index = 0;
       inData[index] = '\0';
       started = true;
       ended = false;
    }
    else if(inChar == EOP)
    {
       ended = true;
       break;
    }
    else
    {
      if(index < 79)
      {
        inData[index] = inChar;
        index++;
        inData[index] = '\0';
      }
    }
  }

  // We are here either because all pending serial
  // data has been read OR because an end of
  // packet marker arrived. Which is it?
  if(started && ended)
  {
    // The end of packet marker arrived. Process the packet

    // Reset for the next packet
    started = false;
    ended = false;
    index = 0;
    inData[index] = '\0';
  }
}

some questions..

why using buadrate 57600, is 9600 not fast enough?
why is inData having a size of 80 chars?
does Serial.read() only return 1 char at the time? if so.. did you solve that by making the var inData an Array instead of a regular char?
whats the difference between an array and Array?
what excactly does "if(inChar == SOP) " compare inChar too?
if "if(inChar == SOP)" returns true, notthing happens only some variables are set to a state, but later on the variables are used nowhere, why would u even compare than?
furthere more, this is a difficuld way to me for compiling the serial data to a const char*, isn't there a simpler way?

why is inData having a size of 80 chars?

The number of characters on a punched card, of course!

Us oldies are suckers for tradition.

if "if(inChar == SOP)" returns true, notthing happens only some variables are set to a state,

SOP stands for "start of packet", so we've received the start of our data, so reset the array index, and indicate that we've started, but not finished. For the character received itself, there's noting very much else to do, we're no longer interested in it.

why using buadrate 57600, is 9600 not fast enough?

Not any more. 20 years ago, it was. Barely.

does Serial.read() only return 1 char at the time? if so.. did you solve that by making the var inData an Array instead of a regular char?

Yes, and yes (except that it is array, not Array.

whats the difference between an array and Array?

One is correct terminology. The other is not.

what excactly does "if(inChar == SOP) " compare inChar too?

The value #defined as SOP:

#define SOP '<'

but later on the variables are used nowhere, why would u even compare than?

Which variables are not used elsewhere? The variables that are reset are index, inData, started, and ended, all of which ARE used later.

furthere more, this is a difficuld way to me for compiling the serial data to a const char*, isn't there a simpler way?

Certainly. Head over to Gigs and Collaboration, and hire someone to do the programming for you.

oh, i get it now..
but cant i do the same thing in a for-loop?

so the define gives a name to '<' and '>'
but what happens if you compare a variable to '<' ..like:
if(varName == <){}
what does '<' stand for?

edit: btw, can i assign inData to msg now with your exaple? like:

const char* msg = inData[*];

but cant i do the same thing in a for-loop?

I think a clue to the answer to that question comes from the observation that an experienced programmer didn't use a for loop.

please note my edit..

what do you mean by that?

The compiler has already told you it doesn't understand this
const char* msg = inData[*];, and neither do I - what are you trying to do?

I was saying that, if PaulS could have done all that with a for loop, don't you think he would have?

i am trying to get the incomming serial message into msg. because msg is the variable wich will be sended over the RF.
and yes you have a point there. i geus i just dont understand what to do with the example, even if i would copy everything into my code, it woundn't change anything, or am i wrong?

You are clearly struggling with basic programming concepts and really should spend some time focusing on just learning the fundamentals of C/C++.

If you had those fundamentals, you'd know that char* msg and char inData[80] aren't nearly as disimilar as they appear, and that you can in fact use inData in exactly the same way you would use msg (with the one caveat that inData is, effectively a const pointer and cannot be reassigned). So there's really no point in trying to assign inData to msg (or even having a separate msg variable at all).

There's an abundance of C/C++ tutorials on the web. Trying to write code without knowing the basics is like trying to build a house without a foundation.

i am trying to get the incomming serial message into msg. because msg is the variable wich will be sended over the RF.

There is NOTHING magic about the name msg. ANY character array that is properly NULL terminated, as inData would be, is suitable input to any function that expects a const char * variable.

What is sending the serial data to the Arduino? Do you have any control over that process? If so, you need to make that process send "<your data>". The < and the > define the start and end of the packet. The code I posted will store everything between the < and the > in inData, if there is room. You can then send inData using the radio sending code.

oh okey, i understand now. and you are right about my basics of c, they are bad, but im working on it ^^.
anyway my brother does program c++ and tryed to explain me some parts, he sucseed but that doesnt take all problems away..

i've copied and edit my code now, i have this:

#include <VirtualWire.h>
#define SOP '<'
#define EOP '>'

bool started = false;
bool ended = false;

char inData[80];
byte index;

void setup()
{
  //Setup VirtualWire to transmit on 4
  vw_setup(100);
  vw_set_tx_pin(4);
  
  pinMode(13, OUTPUT);    //Power LED
  digitalWrite(13, HIGH); //Always ON
  
  //setup a serial connection
  Serial.begin(57600);
}

void loop()
{
  while(Serial.available() > 0)
  {
    char inChar = Serial.read();
    if(inChar == SOP)
    {
       index = 0;
       inData[index] = '\0';
       started = true;
       ended = false;
    }
    else if(inChar == EOP)
    {
       ended = true;
       break;
    }
    else
    {
      if(index < 79)
      {
        inData[index] = inChar;
        index++;
        inData[index] = '\0';
      }
    }
  }
  
  if(started && ended)
  {
    // The end of packet marker arrived. Process the packet
    vw_send((uint8_t *)inData, strlen(inData));
    // Reset for the next packet
    started = false;
    ended = false;
    index = 0;
    inData[index] = '\0';
  }
}

here is the code of the robot wich im trying to control:

#include <VirtualWire.h>

int m1r = 6;  //motor left red wire
int m1b = 5;  //motor left black wire
int m2r = 9;  //motor right red wire
int m2b = 10; //motor right black wire

void setup()
{
  //virtual wire setup 
  //for recive on pin 4
  vw_setup(100);
  vw_rx_start();
  vw_set_rx_pin(4);
  
  pinMode(13, OUTPUT);    //power LED
  digitalWrite(13, HIGH); //Always ON
}

void loop()
{
  //variables setup
  uint8_t buf[VW_MAX_MESSAGE_LEN];
  uint8_t buflen = VW_MAX_MESSAGE_LEN;
  char recived;
  
  if(vw_get_message((uint8_t*)recived, &buflen))
  {
    move(recived);
  }
  
  delay(50);
}


//[FUNCTION] ~ move into recived driection (for,bac,lef,rig)
void move(char DIR)
{
  //Stop
  if(DIR == 'x')
  {
    analogWrite(m1r, 0);
    analogWrite(m1b, 0);
    analogWrite(m2r, 0);
    analogWrite(m2b, 0);
  }
  //drive forward
  if(DIR == 'f')
  {
    analogWrite(m1r, 255);
    analogWrite(m1b, 0);
    analogWrite(m2r, 255);
    analogWrite(m2b, 0);
  }
  //drive backwards
  if(DIR == 'b')
  {
    analogWrite(m1r, 0);
    analogWrite(m1b, 255);
    analogWrite(m2r, 0);
    analogWrite(m2b, 255);
  }
  //turn left
  if(DIR == 'l')
  {
    analogWrite(m1r, 0);
    analogWrite(m1b, 255);
    analogWrite(m2r, 255);
    analogWrite(m2b, 0);
  }
  //turn right
  if(DIR == 'r')
  {
    analogWrite(m1r, 255);
    analogWrite(m1b, 0);
    analogWrite(m2r, 0);
    analogWrite(m2b, 255);
  }
}

now if i power both and send the letter 'r' trough the serial monitor it doesn't turn right,
is this because of wrong transmition or because something else? (i hope im not going offtoppic)

thanks alot already,
nick

The packet (r) needs to be between start and end markers. Try sending "", instead of "r".

i forgot, and tryed it by sending:
but it didn't change anything still no movement..
any idea's?

This doesn't look good:

  if(vw_get_message((uint8_t*)recived, &buflen))

By the look of the foregoing, it should be

  if(vw_get_message(buf, &buflen))

and then you'll need to pass buf[1] (perhaps) to Move.

any idea's?

As a matter of fact, yes. Add some serial output. Are you receiving what you think you are?

The move() function on the receiver is passed an array of chars. It expects a char. Fail!

Some of the motor control wires control direction, and should be connected to non-PWM pins, with digitalWrite() used to control them. Do the motors do what they are supposed to with a simple sketch on the receiver?

What, exactly, is connecting the two Arduinos?