code problem

Hello guys
i'm trying to send data form 2 sensors using xbee s2 :

transmit code 

// I2Cdev and MPU6050 must be installed as libraries, or else the .cpp/.h files
// for both classes must be in the include path of your project
#include "I2Cdev.h"
#include "MPU6050.h"

// class default I2C address is 0x68
// specific I2C addresses may be passed as a parameter here
// AD0 low = 0x68 (default for InvenSense evaluation board)
// AD0 high = 0x69
MPU6050 accelgyro;

int16_t ax, ay, az;
int16_t gx, gy, gz;

#define LED_PIN 13
bool blinkState = false;


int f2 = A1; //analog pin 0
int f1=A0;
char s_str1[9];
char s_str2[9];
char s_str3[9];
char s_str4[9];
String str1,str2,str3,str4;
void setup(){
    // join I2C bus (I2Cdev library doesn't do this automatically)
    Wire.begin();

    // initialize serial communication
    // (38400 chosen because it works as well at 8MHz as it does at 16MHz, but
    // it's really up to you depending on your project)
    Serial.begin(9600);

    // initialize device
    Serial.println("Initializing I2C devices...");
    accelgyro.initialize();

    // verify connection
    Serial.println("Testing device connections...");
    Serial.println(accelgyro.testConnection() ? "MPU6050 connection successful" : "MPU6050 connection failed");

    // configure Arduino LED for

}

void loop(){
  accelgyro.getMotion6(&ax, &ay, &az, &gx, &gy, &gz);
  int flex1 = analogRead(f1); 
 int flex2 = analogRead(f2); 
   int flex11= map(flex1, 225, 279, 180, 0);
  int flex12 = map(flex2, 225, 279, 180, 0);
  int vro = map(ay, -16000, 16000, 0, 179); 
int vba=map(ax, -16000, 16000, 0, 179);
 int flex112=constrain(flex11,0,180);
 int flex122=constrain(flex12,0,180);
 int vro2=constrain(vro,0,180);
 int vba2=constrain(vba,0,180);
 // read raw accel/gyro measurements from device
 // Indicates the start of the message (e=motors, s=robot arm)
 
 Serial.print("z");Serial.print(vro2);  Serial.print("\n");// Pinky
 
  delay(30);
   Serial.print("s");Serial.print(vba);  Serial.print("\n");// Pinky
   delay(30);
    Serial.print("y");Serial.print(flex112);  Serial.print("\n");// Pinky
 
  delay(30);
   Serial.print("w");Serial.print(flex122);  Serial.print("\n");// Pinky
   delay(30);
  Serial.flush();
    Serial.flush();
 

   

}
Receiver code 
void setup() {
  // put your setup code here, to run once:
Serial.begin(9600);
}

void loop() {
  // put your main code here, to run repeatedly:
if (Serial.available()>=21)
{
if (Serial.find("z"))
{
  int y=Serial.parseInt();
  Serial.print(y);Serial.print("\n");
}
if (Serial.find("s"))
{
  int r=Serial.parseInt();
Serial.print(r);Serial.print("\n");
}
  delay(20);
}}

My problem is sometimes that data reach successfully but after 2 min or less there delay time shown on the serial monitor .
sometimes the same code won't receive code as i want it .

I'm about to graduate and no one on my area ever use xbee s2 .
any suggest will help .

Your serial receive code is really weird. It's probably the problem. You should be reading data into a buffer and then processing it, not using find and counting waiting characters like you do. That is not very robust, to say the least.

aarg:
Your serial receive code is really weird. It's probably the problem. You should be reading data into a buffer and then processing it, not using find and counting waiting characters like you do. That is not very robust, to say the least.

I receive 4 value i need each one of them go to different way and i'm trying to separate them
what can i do ?!

Robin2's serial input basics thread may help you. It will show how to build a data packet that is easy to send, read and parse using non blocking methods and character arrays (instead of, problematic, Strings).

I look at that code and considering the 30ms delays between sends, it is the receive code that fails the most.

At 9600 baud, I could drive a truck through the char to char gap!
I have a char to char parser that keeps up with 250000 baud, only 40 micros between chars. 9600 baud is > 1000!

You have blocking code in your receive code, no wonder "you need to buffer"!

GoForSmoke:
I look at that code and considering the 30ms delays between sends, it is the receive code that fails the most.

At 9600 baud, I could drive a truck through the char to char gap!
I have a char to char parser that keeps up with 250000 baud, only 40 micros between chars. 9600 baud is > 1000!

You have blocking code in your receive code, no wonder "you need to buffer"!

how can i do that ?

happyboss:
how can i do that ?

Did you follow the link in reply #3?

Don't use delays.

Use the method in Serial Input Basics.

The delays on the sender are also unnecessary. If you only want it to send data once per second, put a delay(1000); after sending all the data.

happyboss:
how can i do that ?

You would need to do enough study and practice to learn well about not-blocking and serial input without blocking before starting on your particular app.

I would just write the code. My didn't learn this overnight happened in the 80's.

But some of the things you do that block are

if (Serial.available()>=21)
{

Right here you wait until all of the chars are received, 2 mistakes in one BTW if all sent don't get received cleanly enough to get buffered.

Oh, did I write buffered? That's because incoming serial gets buffered in a buffer that can get filled and miss whatever else gets sent -- which might account for symptoms you see.

if (Serial.find("z"))   // your sketch could have identified the 'z' when it arrived but you waited to look
{
  int y=Serial.parseInt();    // at least you have the chars all buffered up for this but it could be done sooner
  Serial.print(y);Serial.print("\n");
}
if (Serial.find("s"))
{
  int r=Serial.parseInt();
Serial.print(r);Serial.print("\n");
}
  delay(20);                          // why is this needed? 
}}

Reading text decimal digits and turning them to numeric values as they come in, the hardest part is handling errors which your code doesn't do anyway.

If my sketch is reading text digits "123" and I want to get the value, I start with an int

accumulate = 0;

I read the '1' of the "123".

textRead = Serial.read();

I multiply whatever is in accumulate by ten and add 1

accumulate = accumulate * 10 + textRead - '0';

The text code for '1' is 49 and for '0' is 48. Subtraction gives me the value 1. All text decimal digits are 48 to 57 (=9).

As I read in digits I repeat the process of x 10 + new value.
When I get a non-digit, I'm done even if it's because of error.
If no error, accumulate holds the value gotten before the non-digit delimiter was read, not evaluated afterwards.

If you see a parallel between this and how you deal with numbers on paper, it's because there is.

This kind of text parse and evaluate is usually easiest done in a finite state machine, Nick Gammon presents a simple one for teaching in his "How to process incoming serial data without blocking" tutorial about 1/3rd the way down.

http://www.gammon.com.au/serial

and his state machines tutorial is

http://www.gammon.com.au/statemachine

Robin2 has sticky threads (they stay up top of the list) on the forum covering the same basic things in thread form.

If you're sharp, you can get this learned before September working part time hours but warning, you HAVE to work with simpler practice sketches to get all of it. If you get zoomy and think you can wing it, it will take longer to get right.