Serial.print response problem

Hello everybody!

I have a small problem with my application including two Xbees with shield and Arduino unos. First I am sending a message by using following code:

#include <NewSoftSerial.h>
#include "Wire.h"

void setup(){

 Wire.begin(); // I2C connection startup
 Serial.begin(9600); //open serial in baud rate

 Serial.print("Node init.");
}

void loop()
{
sending();
}

int sending() {
 
 int ack;
 char tx_char;
 int k=0;
 int mean=150;
 int mean2=150;
 
  for (int k=0; k<20; k++) { //looping 20 times
   
     if (mean <= 40 && mean2 >= 97 || 
       mean2 <= 37 && mean >= 100 ||  
       mean <= 40 && mean2 <= 37 ||  
       mean >= 100 && mean2 >= 97 ){
    
       Serial.print("F"); //send Xbee 'full'
       tx_char='f'; //expected ack
     }
    
    else{ 
     Serial.print("L");  //send Xbee 'low'
     tx_char='l'; //expected ack
    } //else
  
    //confirmation
    if (Serial.available() > 0) {
    ack = Serial.read();
  
    Serial.print(ack);
    }
  } 
}

So, basically when it sends “F” (“L”) it should receive confirmation of “f” (“l”) send by other node with using following code:

char incomingByte;

void setup() {

  Serial.begin(9600);
}

void loop() {

  if (Serial.available() > 0) {

    incomingByte = Serial.read();
    Serial.print(incomingByte);
    
    if (incomingByte == 'F') {
      Serial.print('f'); //sends confirmation to end device
    } 
    
    else if(incomingByte == 'L'){
     Serial.print('l'); //sends confirmation to end device
   }
  }
}

But the first node never gets the confirmation and loops through the sending 20 times. I am not sure what I am doing wrong.

But the first node never gets the confirmation and loops through the sending 20 times. I am not sure what I am doing wrong.

You never break out of the for loop. If you get a response, you should break out of the for loop.

When you do break out of the for loop, loop() will end, and be called again, where the process will start over. The function of this application escapes me.

My apologies. The code was very confusing, probably because it is part of a bigger entity, which is very long. So, I will strip the function down to the important part, which is not working.

Sender will send 'F' and should receive "f" in response.

This is the code in NODE1(sender):

void setup(){

 Wire.begin(); 
 Serial.begin(9600);
}

void loop()
{
    int ack;
    Serial.print("F"); //send Xbee 'full'
    delay(1000);
    while (Serial.available() > 0) {
    ack = Serial.read();

    Serial.print(ack);
    }
}

Receiver when receiving "F" should send "f" as an response.

Code in NODE2(receiver):

void setup() {
  Serial.begin(9600);
}

void loop() {

  if (Serial.available() > 0) {
    incomingByte = Serial.read();
    Serial.print(incomingByte);
    
    if (incomingByte == 'F') {
      Serial.print('f');
    } 

  }
}

On serial monitor I can see that NODE2 (receiver) prints the "f" as the response, but when checking monitor on NODE1 it never gets the response. I am a bit confused since Serial.print on NODE1 => NODE2 works, but on NODE2 => NODE1 doesn't work.

What kind of XBees are they (series 1 or series 2/2,5)? How are they configured?

Thousand thanks for the help!

Ok, this was solved by using a delay at the NODE1 end when waiting for the confirmation.