Go Down

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

PaulS

Quote
does it need some time to delete the readen data or is it immediate?

It happens before the read() method returns.

Quote
I've had some trouble because as I send something like 1,2,3 with the code you gave me I receive 1111,,,,2222,,,,3333

What are you sending from? It looks to me like the problem is on the sending end.

Quote
and I'm wondering if it's a problem with some delay() I should use.

Let me see if I can express this in a way that you can understand.
N
delay(3)
o
delay(3)
i
delay(3)
s
delay(3)
n
delay(3)
e
delay(3)
e
delay(3)
d
delay(3)
e
delay(3)
d

Period. Anywhere.

propilot01

I send a string from the other arduino. It's like c=<1,123,123,200> and in the code I wrote Serial.print(c). So in the end do I have to give some delay somewhere? Or should I send the data I want not as a string?

PaulS

Quote
I send a string from the other arduino. It's like c=<1,123,123,200> and in the code I wrote Serial.print(c). So in the end do I have to give some delay somewhere? Or should I send the data I want not as a string?

How about C) post your code.

propilot01

the sending code:
Code: [Select]
#include <SoftwareSerial.h>

const int x=2;
const int y=1;
const int z=0;
int sx=0;
int sy=0;
int sz=0;
String datapac="";
SoftwareSerial mySerial(10, 11);

void setup()
{
 Serial.begin(9600);
 mySerial.begin(9600);
 pinMode(x,INPUT);
 pinMode(y,INPUT);
 pinMode(z,INPUT);
 pinMode(10, INPUT);
 pinMode(11, OUTPUT);
}

void loop()
{
 sx=analogRead(x);
 sy=analogRead(y);
 sz=analogRead(z);
 sx=map(sx,0,1023,0,255);
 sy=map(sy,0,1023,0,255);
 sz=map(sz,0,1023,0,1);
 delay(10);
 datapac += "<";
 datapac += String(sz);
 datapac += ",";
 datapac += String(sx);
 datapac += ",";
 datapac += String(sy);
 datapac += ",";
 datapac += String(200);
 datapac += ">";
 /*if(Serial.available()>0)
 {*/
   Serial.print(datapac);
   mySerial.print(datapac);
 /*}*/
 delay(20);
 datapac="";
}

the reading/control code:
attached to the post.

PaulS

Code: [Select]
String datapac="";
  datapac += "<";
  datapac += String(sz);
  datapac += ",";
  datapac += String(sx);
  datapac += ",";
  datapac += String(sy);
  datapac += ",";
  datapac += String(200);
  datapac += ">";

Why? All those Strings are unnecessary.


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? :)

PaulS

I don't know if your problem is one of data not getting through quick enough, or the fact that int to text conversion has to take place, then the text is transmitted, then needs to be parsed and converted back to ints.

None of the values you are sending are larger than byte sized, so sending 4 bytes would make more sense.

You could map from 0 to 1023 to 0 to 254 (instead of 255), then send 255 as the sync bit (instead of 200), all using Serial.write(), not Serial.print() (and no start and end markers).

propilot01

What should I do to send bytes instead of integer? Which code would work better? I use byte() to convert it right? And from byte to integer?

PaulS

Quote
What should I do to send bytes instead of integer?

Define the type as byte, not int, and
Quote
all using Serial.write(), not Serial.print()


Quote
I use byte() to convert it right?

No. You define it as the right type to start with.

Quote
And from byte to integer?

There is nothing you need to do.

propilot01

Perfect! But I have to read 3 variable resistors, how do I change the value from integer from 0 to 1023 to byte from 0 to 254?

PaulS

Quote
how do I change the value from integer from 0 to 1023 to byte from 0 to 254?

Maybe like this:
Code: [Select]
  sx=analogRead(x);
  sy=analogRead(y);
  sz=analogRead(z);
  sx=map(sx,0,1023,0,255);
  sy=map(sy,0,1023,0,255);
  sz=map(sz,0,1023,0,1);

except that the output should not overwrite the input variable. Store the output in a different (byte) variable. Note that sz will be 0 for input values of 0 to 1022 and 1 for input of 1023. I'm not sure why you want that.

Go Up