2 Bluetooth communications fail

Hello everybody,
I have a problem and understand why? the problem is;

Arduino -A (send data)
I am measuring distance that is ok read up to 400 cm.I want to send to other arduino with bluettoth module.The problem starting here.

Arduino-B (recieve data)
When i sent bluetooth data from A Arduino I can read distance between 4 cm and 200 cm but i can not read distance over 200 cm.you can see codes below.Could you please help me?

thanks.

#include <SoftwareSerial.h>
SoftwareSerial BtSerial(10, 11);
const int trigger_pin = 6;
const int echo_pin = 7;
int Time;
int distance;
void setup()
{
  pinMode(trigger_pin, OUTPUT);
  pinMode(echo_pin, INPUT);
  Serial.begin(9600);
  BtSerial.begin(9600);
}

void loop()
{
  digitalWrite(trigger_pin, LOW); /* sensör pasif hale getirildi */
  delayMicroseconds(5);
  digitalWrite(trigger_pin, HIGH); /* Sensore ses dalgasının üretmesi için emir verildi */
  delayMicroseconds(10);
  digitalWrite(trigger_pin, LOW);
  Time = pulseIn(echo_pin, HIGH);
  distance = (Time / 2) / 28.5;
  delay(500);
  //BtSerial.write(gonderilenKarakter);
  //BtSerial.write(gonderilenKarakter);
  if (distance > 0)
  {
    BtSerial.write(distance);
    Serial.print("Distance cm : ");
    Serial.println(distance);
  }


}

recieve arduino

#include <SoftwareSerial.h>
SoftwareSerial BtSerial(10, 11);
int data;

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

void loop()
{
  if (BtSerial.available() > 0)
  {
    data = BtSerial.read();
    Serial.println(data);

  }
}
    BtSerial.write(distance);

What type is the argument to the write() method? It is NOT the same as the type of the variable you use.

What are the limits to the range of value that the proper type can hold?

What do you suppose happens when you pass it a value that is not of the correct type?

Have a look at the examples in Serial Input Basics - simple reliable ways to receive data. The technique in the 3rd example will be the most reliable.

...R

Hello PaulS

What you mean? write(int)

Code: [Select]
BtSerial.write(distance);

What type is the argument to the write() method? It is NOT the same as the type of the variable you use.

The Serial.write() command sends a byte. Your variable is defined as an int so only half of it will be sent.

...R

Hello again

Solved problem, you can see the codes below

 if (distance > 0)
  {
    
      while (BtSerial.available())
  {
    Serial.write(BtSerial.read());
    delay(1);
  }
  if (distance > 0)
  {
    
    BtSerial.println(distance);
    Serial.println(distance);
    delay(200);
  }
  }

After fixing the piss-poor indenting, you have:

  if (distance > 0)
  {
      while (BtSerial.available())
      {
         Serial.write(BtSerial.read());
         delay(1);
      }
      if (distance > 0)

What are the odds that the second if statement will be evaluate to true EVERY time the first one evaluates to true?

yea the first one statement evaluates to true, every time.

Now is everything be ok