Pages: 1 [2] 3   Go Down
Author Topic: send array of integers via xBee s1  (Read 2955 times)
0 Members and 1 Guest are viewing this topic.
Seattle, WA USA
Online Online
Brattain Member
*****
Karma: 551
Posts: 46209
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 33
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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?
Logged

Seattle, WA USA
Online Online
Brattain Member
*****
Karma: 551
Posts: 46209
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 33
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

the sending code:
Code:
#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.

* ctrl_robot_HC_SR04_test_serialmov_v1_7_4.ino (11.45 KB - downloaded 6 times.)
Logged

Seattle, WA USA
Online Online
Brattain Member
*****
Karma: 551
Posts: 46209
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
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.

Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 33
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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?
Logged

Seattle, WA USA
Online Online
Brattain Member
*****
Karma: 551
Posts: 46209
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
  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.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 33
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
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:
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 smiley I'm new to arduino and i have to get used to use it and touse the support of the forum!
Logged

Seattle, WA USA
Online Online
Brattain Member
*****
Karma: 551
Posts: 46209
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Code:
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.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 33
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Everything is working!!! no problems transmitting nor receiving, it works just perfectly! thank you really much smiley
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? smiley
Logged

Seattle, WA USA
Online Online
Brattain Member
*****
Karma: 551
Posts: 46209
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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).
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 33
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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?
Logged

Seattle, WA USA
Online Online
Brattain Member
*****
Karma: 551
Posts: 46209
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 33
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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?
Logged

Seattle, WA USA
Online Online
Brattain Member
*****
Karma: 551
Posts: 46209
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
how do I change the value from integer from 0 to 1023 to byte from 0 to 254?
Maybe like this:
Code:
  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.
Logged

Pages: 1 [2] 3   Go Up
Jump to: