Sending to diff variables from serial port

Hi.

Just beginig with Arduino and Programing

How can i alternate the numbers I recive via Serial port (Max MSP) to, say, 2 different variables?

Please Help.

Do you mean that you receive one value in each message and the first value should go into variableA, the second int variableB, the third into variableA etc?

You could do this with a counter variable to keep track of where you are - something like

if (thisVar == 0) {
  variableA = receivedValue;
  thisVar = 1;
}
else {
  variableB = receivedValue;
  thisVar = 0;
}

but it is likely to get mixed up very easily

It would be much better to send the two values at the same time always in the same order - for example "<valueA, valueB>" and then you always know which value is which because of its place in the message.

Have a look at the examples in Serial Input Basics - simple reliable non-blocking ways to receive data. There is also a parse example to illustrate how to extract numbers from the received text.

...R

Robin2:
Do you mean that you receive one value in each message and the first value should go into variableA, the second int variableB, the third into variableA etc?

You could do this with a counter variable to keep track of where you are - something like

if (thisVar == 0) {

variableA = receivedValue;
  thisVar = 1;
}
else {
  variableB = receivedValue;
  thisVar = 0;
}



but it is likely to get mixed up very easily

It would be much better to send the two values at the same time always in the same order - for example "<valueA, valueB>" and then you always know which value is which because of its place in the message.

Have a look at the examples in [Serial Input Basics](http://forum.arduino.cc/index.php?topic=396450.0) - simple reliable non-blocking ways to receive data. There is also a parse example to illustrate how to extract numbers from the received text.


...R

Thanks for not being an ass with beginers.

Moris526:
Thanks for not being an ass with beginers.

Thank you for your kind words. However I am not aware that any of the regulars here is unkind to beginners. We all want to encourage them.

...R

Robin2:
Thank you for your kind words. However I am not aware that any of the regulars here is unkind to beginners. We all want to encourage them.

...R

He's talking about his other post: 8x8 led display con by max msp. Newbie problem - LEDs and Multiplexing - Arduino Forum

He as with many newbies are getting confused as to Serial.read() actually does.

I suppose he expects us to be mind readers hence not an "ass".

.

ieee488:
He's talking about his other post: 8x8 led display con by max msp. Newbie problem - LEDs and Multiplexing - Arduino Forum

He as with many newbies are getting confused as to Serial.read() actually does.

I suppose he expects us to be mind readers hence not an "ass".

.

I dont expect that. I expect a straight forward answer to someone who doesnt know something, without the added hubris

Moris526:
I dont expect that. I expect a straight forward answer to someone who doesnt know something, without the added hubris

As a relative newcomer to this forum I have observed that on a regular basis people come on here asking for help with vague problems without even attempting code, asking for help (i.e., do it for me) with homework problems, and finally asking for help with issues that could be easily resolved using Google or tutorials.

I can see how it would be maddening for members that have been here for a while. I'm amazed the people can find the forum but not the dozens of samples and tutorials that are on this same site!

Moris526:
I dont expect that. I expect a straight forward answer to someone who doesnt know something, without the added hubris

Okay!
Straightforward answer: we don't know because you have provided zero useful information.

ToddL1962:
As a relative newcomer to this forum I have observed that on a regular basis people come on here asking for help with vague problems without even attempting code, asking for help (i.e., do it for me) with homework problems, and finally asking for help with issues that could be easily resolved using Google or tutorials.

I can see how it would be maddening for members that have been here for a while. I'm amazed the people can find the forum but not the dozens of samples and tutorials that are on this same site!

Just look at his question in this thread.

Robin2's answer is quite right, but I know that is not what the OP really needs.

The OP is struggling with using serial communication.
He abandons his original post which I linked and then writes a second post even more vague than the first one.

garbage in = garbage out

.

Moris526:
I dont expect that. I expect a straight forward answer to someone who doesnt know something, without the added hubris

It is not helpful when you start a second Thread about what is essentially the same problem. Please click Report to Moderator and ask to have the Threads merged.

By the way I have read your other Thread and I could not see anything in any of the Replies that would justify a complaint.

...R

How come I feel guilty for not knowing?

Moris526:
How come I feel guilty for not knowing?

Well, there is a "Read this before posting a programming question ..." at the top of the discussions list that is often missed or just ignored by persons first posting to the forum. It is usually obvious weather it has been read by the poster just by looking at the post. That provides an irritation to some. That being said, if further post have adequate problem descriptions and content that is helpful to the question resolution, things go smoothly. Do be aware that there are some that go into a stalker mode when they don't have an answer, asking things like "why do you want to do that" and generally become somewhat condescending to the poster. Just comes with the territory.

Moris526:
How come I feel guilty for not knowing?

I'm not sure what that is intended to convey. It does not sound like the sort of comment that is likely to endear you to those who might help you.

I made some suggestions in Reply #1 and so far there has been no response from you about any of them, or about any of the technical issues that (I presume) sparked off your enquiry in the first place. Have you come here just for a chat? or to get help with your project?

...R

I just need to change the first item of the string to be parsed from a word to an integer

// Example 5 - Receive with start- and end-markers combined with parsing

const byte numChars = 32;
char receivedChars[numChars];
char tempChars[numChars];        // temporary array for use when parsing

      // variables to hold the parsed data
char messageFromPC[numChars] = {0};
int integerFromPC = 0;
int floatFromPC = 0;

boolean newData = false;

//============

void setup() {
    Serial.begin(9600);
    Serial.println("This demo expects 3 pieces of data - text, an integer and a floating point value");
    Serial.println("Enter data in this style <HelloWorld, 12, 24.7>  ");
    Serial.println();
}

//============

void loop() {
    recvWithStartEndMarkers();
    if (newData == true) {
        strcpy(tempChars, receivedChars);
            // this temporary copy is necessary to protect the original data
            //   because strtok() used in parseData() replaces the commas with \0
        parseData();
        showParsedData();
        newData = false;
    }
}

//============

void recvWithStartEndMarkers() {
    static boolean recvInProgress = false;
    static byte ndx = 0;
    char startMarker = '<';
    char endMarker = '>';
    char rc;

    while (Serial.available() > 0 && newData == false) {
        rc = Serial.read();

        if (recvInProgress == true) {
            if (rc != endMarker) {
                receivedChars[ndx] = rc;
                ndx++;
                if (ndx >= numChars) {
                    ndx = numChars - 1;
                }
            }
            else {
                receivedChars[ndx] = '\0'; // terminate the string
                recvInProgress = false;
                ndx = 0;
                newData = true;
            }
        }

        else if (rc == startMarker) {
            recvInProgress = true;
        }
    }
}

//============

void parseData() {      // split the data into its parts

    char * strtokIndx; // this is used by strtok() as an index

    strtokIndx = strtok(tempChars,",");      // get the first part - the string
    strcpy(messageFromPC, strtokIndx); // copy it to messageFromPC
 
    strtokIndx = strtok(NULL, ","); // this continues where the previous call left off
    integerFromPC = atoi(strtokIndx);     // convert this part to an integer

    strtokIndx = strtok(NULL, ",");
    floatFromPC = atoi(strtokIndx);     // convert this part to a float

}

//============

void showParsedData() {
    Serial.print("Message ");
    Serial.println(messageFromPC);
    Serial.print("Integer ");
    Serial.println(integerFromPC);
    Serial.print("Integer ");
    Serial.println(floatFromPC);
}

Moris526:
I just need to change the first item of the string to be parsed from a word to an integer

What have you tried? and what happened?

It's much easier to give relevant advice if you show what you have tried because then it is clearer what you you are having trouble with.

...R

Im just learning the language, Im watching tutorials, videos and messing around with strtok with no luck.

Have you noticed this line in the program in Reply #13

    integerFromPC = atoi(strtokIndx);     // convert this part to an integer

Have you considered putting a similar line of code into the example in place of the line that deals with the message part

    strcpy(messageFromPC, strtokIndx); // copy it to messageFromPC

...R

Yes. I did that. In fact, the last item was a flouting number which I could change to Int

strtokIndx = strtok(tempChars,",");      // get the first part - the string
    messageFromPC= atoi(strtokIndx); // copy it to messageFromPC
 
    strtokIndx = strtok(NULL, ","); // this continues where the previous call left off
    integerFromPC = atoi(strtokIndx);     // convert this part to an integer

    strtokIndx = strtok(NULL, ",");
    floatFromPC = atoi(strtokIndx);     // convert this part to a float

and a get the error

¨Incompatible types in assignment of ´int´to ´char [32]¨

then I`ve changed

   // variables to hold the parsed data
char messageFromPC[numChars] = {0};
int integerFromPC = 0;
int floatFromPC = 0;

to

 // variables to hold the parsed data
int messageFromPC= 0;
int integerFromPC = 0;
int floatFromPC = 0;

same error

To clarifie:

Im a musician. My aim is not to be a Arduino programmer. Im just learning this to build an instrument controller.

Once i have the Arduino setup, the rest of the programing I´ll do from Max/Msp (which I understand).

Thats why im picking bits from here and there, and not making a clean, ground up learning curve.

You got most of it right.

http://www.cplusplus.com/reference/cstring/strtok/

char tempChars[] = "helpMe,567,23.45";

char * pointerToChars;
char messageFromPC[20];
int integerFromPC = 0;
int floatFromPC = 0;


void setup() 
{

  Serial.begin(115200);

    pointerToChars = strtok(tempChars,",");      // get the first part - the string
    strcpy(messageFromPC, pointerToChars);
    Serial.println(messageFromPC);    

    pointerToChars = strtok(NULL, ",");         // this continues where the previous call left off
    integerFromPC = atoi(pointerToChars);       // convert this part to an integer
    Serial.println(integerFromPC);
    
    pointerToChars = strtok(NULL, ",");
    floatFromPC = atof(pointerToChars);         // convert this part to a float
    Serial.println(floatFromPC);
}

void loop() 
{

}

Thanks for the answer.

I got it.