Fio Xbee communication

I have 2 Fio boards with Xbee series1 on top communicating (9600 baud) back an forth with "H" and "L" using the same code for both boards:

 if(pin1 > pin2){ 
                                    Serial.print('H');
                                    delay(100); 
   digitalWrite(led10, LOW);                       
            }else{
   digitalWrite(led10, HIGH);
                                    Serial.print('L');
                                    delay(100); 
                        if (Serial.available() > 0) {
    incomingByte = Serial.read();
    if (incomingByte == 'H') {
                   moteuravance(255, false, 0, true);
                               
    }else if (incomingByte == 'L') {
                   moteuravance(0, true, 255, false);               
}
}
}

the function moteuravance(); is a command for two motors. It works but not right a way. That is, when pin1 and pin2 values change there is, during 10-20 seconds, no effect and then, after a while, the motors begin to alternatively rotate in one sense and the other depending whether pin1 is larger or not than pin2. Can somebody help me to understand what's going on? Is this something due the way the buffer is filling up?

The indenting in your code is horrible. Use Tools + Auto Format to fix that. Then, post ALL of your code. We can't comment on the action of a function seeing only it's name.

Is this something due the way the buffer is filling up?

What buffers?

                                    delay(100);

Certainly doesn't help.

Here is the complete code I use (with the proper indentation!). I tried to make it simpler…Now the real thing:
Main:

int motorPin1 = 6;
int motorPin2 = 9;
int motorPin3 = 11;
int motorPin4 = 13;
int photoAV = 3;
int photoGA = 4;
int photoDR = 2;
int AV,GA,DR;
int incomingByte,; 
int led10 = 10;
int led12 = 12;

void setup() {
  pinMode(motorPin1, OUTPUT);
  pinMode(motorPin2, OUTPUT);
  pinMode(motorPin3, OUTPUT);
  pinMode(motorPin4, OUTPUT);
  Serial.begin(9600);
  pinMode(led10, OUTPUT); 
  pinMode(led12, OUTPUT); 
}
void moteur(int mot1, boolean mot2, int mot3, boolean mot4, int duree);
void moteuravance(int mot1, boolean mot2, int mot3, boolean mot4);
void lumiere();

void loop() {
  lumiere();
}

and then “lumiere” tab:

void lumiere()
{
  AV = analogRead(photoAV);
  GA = analogRead(photoGA);
  DR = analogRead(photoDR);
  if(AV > GA && AV > DR){ 
    Serial.print('H');
    delay(100); 
    digitalWrite(led10, LOW);
    digitalWrite(led12, LOW);
    moteur(0, false, 0, false, 500);
  }
  else{
    digitalWrite(led10, HIGH);
    digitalWrite(led12, LOW);
    Serial.print('L');
    delay(100); 
    if (Serial.available() > 0) {
      // read the oldest byte in the serial buffer:
      incomingByte = Serial.read();
      // if it's a capital H (ASCII 72), turn motors in one direction:
      if (incomingByte == 'H') {
        moteuravance(255, false, 0, true);
      }
      else if (incomingByte == 'L') {
        moteuravance(0, true, 255, false);
      }
      delay(100);
    }
  }
}

and “moteurs” tab:

void moteuravance(int mot1, boolean mot2, int mot3, boolean mot4)
{
  analogWrite(motorPin1, mot1);
  digitalWrite(motorPin2, mot2);
  analogWrite(motorPin3, mot3);
  digitalWrite(motorPin4, mot4);
}
void moteur(int mot1, boolean mot2, int mot3, boolean mot4, int duree)

{
  analogWrite(motorPin1, mot1);
  digitalWrite(motorPin2, mot2);
  analogWrite(motorPin3, mot3);
  digitalWrite(motorPin4, mot4);
  delay(duree);
}

Can you help me to understand why this code doesn’t respond properly right a way and some delay is required for Fio1 to receive the “H” and “L” from Fio2?

Is this code running on Fio1 or Fio2?

What is running on the other Fio?

What kind of XBees are they?

How are they configured?

Both Fios have been uploaded with the same code. Xbee series 1. DH Xbee1 = SH Xbee2 DL Xbee1 = SL Xbee2 DH Xbee2 = SH Xbee1 DL Xbee2 = SL Xbee1 Few months ago you advised me on some serial communication problem to introduce somewhere

while(Serial.available == 0){
}

do you think that may alleviate my problem?

In your lumiere function, if AV > GA you delay,set the motors and delay some more. Only when the if is false do you check whether there is any serial input. Thus, if AV > GA on both arduinos, they will continually send 'H' which will never be read by the receiver. If the system runs in this state for a while and things change so that that if is false, when you finally do start reading the serial port, the buffer will be filled up with H's. Your delays ensure that it'll take quite a while to get through them all until you finally get the 'L' you're looking for.

I'm not clear what you're really trying to do though.

Forget the delay(100) it doesn't make any difference. It is true that if the "if" is true for both Fio only 'H' are sent and none of the Fio's checks for serial input, so maybe when "if" becomes false then

the buffer will be filled up with H's

. However, I thought

incomingByte = Serial.read();

was supposed to read the oldest byte, that is an 'L' if an 'L' arrives even after many 'H'.

It does indeed read the oldest byte first. i.e. all the 'H' bytes that arrived before the 'L'

Sorry, I thought "newest"! So, how can I get rid of the old bytes and get only the newest. flush()? peek()?

Use a while loop when you read the serial port - while serial is available, read it into a variable. It will then contain the latest byte. Might be better to refactor your code to get rid of the delays and move the serial port reading out of the lumiere function.

I modified the "lumiere" function according to your recommendations:

void lumiere()
{
  AV = analogRead(photoAV);
  GA = analogRead(photoGA);
  DR = analogRead(photoDR);
  if(AV > GA && AV > DR){ 
    Serial.print('H');
    digitalWrite(led10, LOW);
    digitalWrite(led12, LOW);
    moteur(0, false, 0, false, 500);
  }
  else{
    digitalWrite(led10, HIGH);
    digitalWrite(led12, LOW);
    Serial.print('L');
    while (Serial.available()) {
      // read the oldest byte in the serial buffer:
      incomingByte = Serial.read();
      // if it's a capital H (ASCII 72), turn motors in one direction:
      if (incomingByte == 'H') {
        moteuravance(255, false, 0, true);
      }
      else if (incomingByte == 'L') {
        moteuravance(0, true, 255, false);
      }
    }
  }
}

and it works.... Thanks a lot!