Processing -> Arduino, sending an array

Hi,

I have in Processing an array with 11 elements. I want to send it via Serial connection to a connected Arduino. But I haven't figured out yet the best way to do it.

It is important, that the array is in the same order when it is recieved.

I thank for any help and suggestion.

Greetings
Nicolas

An array with 11 elements of what kind? How are you trying to send the data (string, numeric, ?)?

Hi, thanks for your answer.

its an int array, the number are between 0 and 100.

If you want to write multiple values, and ensure that all the values are received in order, you'll need some start of packet marker and end of packet marker.

You can send the data as numeric data, and receive it the same way, since the values to send are byte-sized ("<",0,14,75,...,99,">"), or you can send it as string data ("<0 14 75 ... 99>").

thanks, can you make me an example? I don't see exactly how to implement it.

greetings
n

If you want to send the data as a string:

  String theStg = "<" + nf(val[0], 3) +
                       " " + nf(val[1], 3) +
                       " " + nf(val[2], 3) +
                       " " + nf(val[3], 3) +
                       " " + nf(val[4], 3) +
                       " " + nf(val[5], 3) +
                       " " + nf(val[6], 3) +
                       " " + nf(val[7], 3) +
                       " " + nf(val[8], 3) +
                       " " + nf(val[9], 3) +
                       " " + nf(val[10], 3) +
                       ">";
  myPort.write(theStg);

If you want to send the data as numbers:

  myPort.write("<");
  myPort.write(val);
  myPort.write(">");

(Assumes that the port is myPort and that the data is in val[], which is a byte sized array).

ok, thank you very much, i think, that could help me alot.

just a little question: to arrange everything again in array in arduino, thats done with substring?

thanks alot!

n

Depends on whether you are sending as numbers or as a string. If you send as numbers, read the values one byte at a time, in the order sent, when the bytes arrive, following the <. Stop when you get a >.

If you are sending as a string, collect the whole string, starting when you get a <, ending when you get a >. I've posted lots of times how to do that.

Then, use strtok with the string as the first argument, to get the first token. Use atoi to convert the token to a number. Then, in a while loop (while(token){}), call strtok with NULL as the first argument (to keep parsing the same string), and use atoi on each token.

Ok, I try it with the String way.

You mentioned, you explained many times how to do that. You have a link of some explanations?

thank you very much!

http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1282127488/25
See reply #20.

Hi,

so I see more of the problem. I recieve the string on the arduino, but now I need it to convert it as you told me.

Here is how i recieve on the reciever:

while(Serial.available() > 0)
{
incomingByte = Serial.read();
if(incomingByte == '<')
{
started = true;
ended = false;
}
else if(incomingByte == '>')
{
ended = true;
break; // Break out of the while loop
}
else
{
rohre[serialIn] = incomingByte;
serialIn++;
rohre[serialIn] = '\0';
}
}

I understand how to convert with asoi, but I don't understand exactly how to handle with strtok.

I found this example:

char str[] ="- This, a sample string.";
char * pch;
printf ("Splitting string "%s" into tokens:\n",str);
pch = strtok (str," ,.-");
while (pch != NULL)
{
printf ("%s\n",pch);
pch = strtok (NULL, " ,.-");
}

This is basically what you tell me to do. But what I don't understand, how to get now the numbers from my String rohre[] into an array newNumbers[]; the working of the while loop is what I don't understand.

Thank you very much for your help.
n

In that example, replace str with your string (rohre).

Where printf is printing a token, you want to use atoi to convert the token to an integer, and store it in the next position in your array, and increment a counter.

int arrIndex = 0;
int arrayOfNums[11];

char *token = strtok (rohre," ");
while (token != NULL)
{
  int aNum = atoi(token);
  if(arrIndex <= 10)
  {
     arrayOfNums[arrIndex] = aNum;
     arrIndex++;
  }
  token = strtok (NULL, " ");
}

Hi,

it looks like it really works.

The only problem remaining is:

I think all values bigger than sender_arr[3] are written to reciever_arr[3].

So the values like sender_arr[11] is in reciever_arr[3].

There is no problem with reciever_arr[0], reciever_arr[1], reciever_arr[2]

You have any idea how this can happen?

Here how I send:

void writeToArduino() {

 // port.write(201); // beginn transmission
 // port.write(fernrohre[1]); 
  
  
    String theStg = "<" + nf(fernrohre[1], 3) +
                       " " + nf(fernrohre[2], 3) +
                       " " + nf(fernrohre[3], 3) +
                       " " + nf(fernrohre[4], 3) +
                       " " + nf(fernrohre[5], 3) +
                       " " + nf(fernrohre[6], 3) +
                       " " + nf(fernrohre[7], 3) +
                       " " + nf(fernrohre[8], 3) +
                       " " + nf(fernrohre[9], 3) +
                       " " + nf(fernrohre[10], 3) +
                       " " + nf(fernrohre[11], 3) +
                       ">";
  port.write(theStg);
}

and this is how I recieve:

void loop() {
  while(Serial.available() > 0)
  {
    incomingByte = Serial.read();
    if(incomingByte == '<')
    {
      started = true;
      ended = false;
    }
    else if(incomingByte == '>')
    {
      ended = true;
      break; // Break out of the while loop
    }
    else
    {
      rohre[serialIn] = incomingByte;
      serialIn++;
      rohre[serialIn] = '\0';
    }
  }

  if(started && ended)
  {

    int arrIndex = 0;


    char *token = strtok (rohre," ");
    while (token != NULL)
    {
      int aNum = atoi(token);
      if(arrIndex <= 10)
      {
        arrayOfNums[arrIndex] = aNum;
        arrIndex++;
      }
      token = strtok (NULL, " ");
    }


    // We got a while packet
    serialIn = 0;
    rohre[serialIn] = '\0';

    started = false;
    ended = false;
  }
  else
  {
    // No data, or only some data, received
  }




}

The code on both ends LOOKS right. We'd need to know what the Arduino actually received, though, to determine whether the failure is in the transmission of the data, the receipt of the data, or the parsing and storing of the data.

Add some Serial.print() and Serial.println() statements to echo rohre, token, aNum, and arrIndex.

Add code in the Processing sketch to respond to, and print, serial data arriving. There is at least one example in the Processing application that shows how to receive and print data.

[edit]I just noticed that your array indices in the Processing application run from 1 to 11. They should run from 0 to 10.[/edit]

[edit]There is nothing in the Arduino code you posted to show that the incoming data is being used. How do you know that the data is being sent/received/parsed/stored incorrectly?[/edit]