If First Char

Hi all,

Sorry I'm new to Arduino and struggling with what feels like a few basics.

I have the following code on my Mega;

  // read from port 0, send to port 1, 2 & 3:
  if (Serial.available()) {
    int stringone = Serial.read();
    Serial1.write(stringone); 
    Serial2.write(stringone); 
    Serial3.write(stringone); 
  }

But I want to change it so that the 1st char decides which port to send the string to.

IE if the string read from Serial "1ON" it would send the string "ON" out on Serial1.

I guess the bit I'm stuck on is how to seperate out in to two strings, even I can manage once I have a string containing "1" and another "ON".

Thanks in advance :slight_smile:

    int stringone = Serial.read();

Rubbish. Serial.read returns an int, but the data of interest is in the low order byte, if the data is valid (and it will be, because you only call Serial.read() when there is data to read).

The output is NOT a string or a String. Your variable name leaves a LOT to be desired.

And, is the root of your problem.

You need to read the whole string from the serial port. At the moment all you are reading is one int.

This would be a good read for you http://www.gammon.com.au/forum/bbshowpost.php?bbsubject_id=11425&page=1

Thanks Paul. - If I knew what I was talking about I wouldn't be asking a question. As mentioned I'm very new to Arudino.

Can you explain why my variable name is a problem? Being as there is only one variable in my sketch surely the name has no impact on anything? (Apart from readability in the future)

UKHeliBob - Thank you, that explains alot! It makes things much easier to understand and fix when you have an idea about whats wrong. Will have a read of that page :slight_smile:

Can you explain why my variable name is a problem?

It's conceptual. If you see a variable name with the word "string" in it, you might be forgiven to think it is a string or a String.
In this case, it is clearly neither, but imagine your sketch had grown to a few hundred lines.

Can you explain why my variable name is a problem?

Because the name, later in the code, separated from the type, implies something that is completely false. stringone is NOT a string.

If I wrote code like:

int floatVal = 14;
float charArray;
char floatArray[5];

and expected you to make sense of those variables later, would you be able to?

if(strcmp(floatArray, "Jane") == 0)
{
   digitalWrite(floatVal, charArray);
}

Complete nonsense, of course, because no one would name variables like that, right?

You need to decide how you're going to detect the start and end of these command strings. If you're sending them from the serial monitor or some sort of serial client, perhaps you're sending newline/carriage return characters at the end of each command. In that case you can use those to indicate the start and end of each command.

Once you have decided that, you need to write some code that reads characters from the serial port as they become available, and buffers them in a char array. Once the end-of-message indicator arrives, you know you have a complete message and can parse it to see which operation it's requesting on which pin. That's easily done by converting the first char to a number, and doing a string comparison on the remaining chars.