Go Down

Topic: send array of integers via xBee s1 (Read 3 times) previous topic - next topic

propilot01

I thought it was the best way to create an unique string to send. I don't have any idea, which way is the best way to send them. How should I do it? Which is the best way to send those numbers?

PaulS

Code: [Select]
  if((inizio==true) && (fine==true))//la funzione di ricezione è completa
  {
    for(int i=0;i<5;i++)
    {
      for(int j=0;j<4;j++)
      {
        buffer[j]=serialcom[posiz]; //si immettono i dati dell'array di ricezione in quello di conversione
        Serial.print(serialcom[i]);
        posiz++; //si sceglie la posizione successiva per il porssimo dato
      }
      data[i]=atoi(buffer); //vengono convertiti i valori dell array buffer in un unico numero intero
      posiz++; //poichè ogni tre caratteri c'è un divisore bisogna trascurarlo
      Serial.print(data[i]);
    }
  }

What are these nested loops doing? If you send "<1,2,3,200>", serialcom is going to contain "1,2,3,200". You are then printing the value in serialcom[ i ] many times, as well as printing the value in data[ i ] may times, with no indication what is being printed.

propilot01

Now I understand, I've written a Sh*tty code, I have to write everything again. I have to use the commas to understand if there is a new number instead of using pre-fixed position (as here with the loop). I used the loops to take out what i needed, but i used them in the wrong way and as i checked for the incoming data with
Code: [Select]
Serial.print(serialcom[i]); i didn't though tha i would have read4 time the same number. This part could work if I used
Code: [Select]
Serial.print(serialcom[j+i]);  Now I have to:
-rewrite this bad code
-cut out all the loops and use some more efficient way to separate the data

Thank you really much, I was too focussed on the wrong way to realize it was a quite obvious stupid error. Sorry again for the bad english, bad code and everything else :) I'm new to arduino and i have to get used to use it and touse the support of the forum!

PaulS

The strtok() function will do the parsing for you. You only need one while loop to parse serialcom.

Code: [Select]
byte tokenCount = 0;
char +token = strtok(serialcom, ",");
while(token)
{
   data[tokenCount] = atoi(token);
   tokenCount++;
   token = strtok(NULL, ",");
}


Notice how I use tokenCount, rather than i and j. The name tokenCount clearly defines the purpose of the variable. i and j do not.

propilot01

Everything is working!!! no problems transmitting nor receiving, it works just perfectly! thank you really much :)
But sadly I have another question, I don't know if I should post it on another topic but I will ask here before. The code work fine, it doesn't seem to have problems but as it is running every 5-6 ultra fast cicles it does one really slow. Since my robot is a car it's a big problem the fact that I can have long delays between orders and execution of them. Can you suggest me something or dou you have any advice about how to keep the running stable? :)

Go Up