send array of integers via xBee s1

Strange, when using this code the compiler gives me an error on the if and else if statement (expected primary expression before '=' token) but i double checked the whole code and I didn't found anything

Post your code, then.

The code was the one you send me, but i fixed the problem, it was simply an error with #define (I've written #Define x=2 as I added you part to my code, then I've seen my huge mistake).
Now, you said me I do not understand Serial communication, it's right, as I wrote Serial.flush() I was trying to clear the buffer, so that the data will be the newest possible. How does I clear the buffer?
Thank you for your help!

Clearing the buffer is generally not a good idea. The old flush() method did that by throwing away random amounts of unread data. I can't see how that is a good idea, unless you are resting code that re-syncs with a sender.

If that is not the goal, then every character sent was sent with the expectation that it would be important, and, therefore, read.

Why would you not want to read that data, if you sent it?

If you don't want the Arduino to have to read the data, don't send it.

I need to have always the more recent data sent, so that the car has the latest command i gave her, If the buffer fills quicker than arduino reading it's data I would have always an encreasing delay from my commands and what the robot does, isn't it?

Now as I'm writing about this I've thought:I could add a part of code that sends an "ok" when it's ready to recive, so the ground station sends it's data and the car recive only those one, leaving the buffer clear when I don't need data and ready to recive as I have to use them. would that work?

My first idea was: ground station continues sending data, car reads the latest sent. I clear the buffer so that the data aren't too old
Now: car needs data, send a byte to the G.station, which sends back the array of data needed. No useless communication, no buffer filling up with data i don't need ,as you said, REALLY the latest data availale.
Right?

If the buffer fills quicker than arduino reading it's data I would have always an encreasing delay from my commands and what the robot does, isn't it?

Yes. So, don't let that happen. Get rid of any delay()s, so that you spent most of your time checking to see if there is anything to do, rather than doing nothing.

I have a little question: as I use the serial.read() function, does it need some time to delete the readen data or is it immediate? 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 and I'm wondering if it's a problem with some delay() I should use. I simply read the data and put them in one array, should I use some delay to give my board the time do read and then clear the data?

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

It happens before the read() method returns.

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.

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.

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?

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.

the sending 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.5 KB)

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.

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?

  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.

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
Serial.print(serialcom[i]); i didn't though tha i would have read4 time the same number. This part could work if I used 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 :slight_smile: I'm new to arduino and i have to get used to use it and touse the support of the forum!

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

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.

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

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

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?

What should I do to send bytes instead of integer?

Define the type as byte, not int, and

all using Serial.write(), not Serial.print()

I use byte() to convert it right?

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

And from byte to integer?

There is nothing you need to do.

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?