user68386: Hi I am doing a project in which I use 2 HC-05 modules and 2 arduino unos driving them. 1 bluetooth module is master and the other is slave. Master module is stationary while slave is moving. If the distance between these 2 modules is less than 1.5 meter, master device turns on a led. If the distance is more than 1.5 meter, the led will not be turned on. This is the basic idea and it works but not as I wanted. I present the code
master device code:
#include <SoftwareSerial.h>
//master device code
SoftwareSerial BTSerial(5, 6); // RX | TX
#define LED 4
unsigned long timer = 0;
unsigned long timeDifference = 0;
char data;

void setup()
{
  Serial.begin(9600);
  BTSerial.begin(9600); 
  pinMode(LED, OUTPUT);
  delay(5000);      
}

void loop()
{
  BTSerial.write('A');
  timer = millis();
  while(!BTSerial.available()){}
  data = BTSerial.read();
  if(data =='B'){
    timeDifference = millis()-timer;   
  }
  Serial.println(timeDifference);

  if(timeDifference < 280){
    digitalWrite(LED,HIGH);
  }else{
    digitalWrite(LED,LOW);  
  }
  delay(500);

}

slave device code:
#include <SoftwareSerial.h>
//slave device code
SoftwareSerial BTSerial(5, 6); // RX | TX

char data;

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

void loop()
{
  while(!BTSerial.available()){}
  data = BTSerial.read();
  if(data =='A'){
    BTSerial.write('B'); 
  }

}

So what happens here is that master device sends a char and starts timer. Slave receives the char and send another char to master device. When master device receives char, it stops timer and looks at how much time passed. The problem is that when I put those bluetooth devices together, the time difference is like 280ms. When I move away in the room with slave device, it is still like same. It is not sensitive. What can I do to make this more sensitive? I am waiting for your opinions

user2967: The approach you're using will not work. You're trying to use speed of light time measurement to gauge distance. There are a whole host of problems with that. First, the clock on the Arduino is not fast enough (high enough resolution). You need accurate, sub-nanosecond time resolution for that (see this video: https://www.youtube.com/watch?v=9eyFDBPk4Yw). Second, BLE is a packet protocol. The overhead and variability of sending packets will totally swamp the tiny difference in round-trip time due to speed of light delay.
The usual way to gauge distance with BLE is to measure signal strength. That is also error-prone, but you can at least get rough "immediate", "near", "middle" and "far" distance measurements.
Alternately you could use and ultrasonic range sensor.

68386: <p>Hi I am doing a project in which I use 2 HC-05 modules and 2 arduino unos driving them. 1 bluetooth module is master and the other is slave. Master module is stationary while slave is moving. If the distance between these 2 modules is less than 1.5 meter, master device turns on a led. If the distance is more than 1.5 meter, the led will not be turned on. This is the basic idea and it works but not as I wanted. I present the code</p>
<p>master device code:</p>
<pre><code>#include &lt;SoftwareSerial.h&gt;
//master device code
SoftwareSerial BTSerial(5, 6); // RX | TX
#define LED 4
unsigned long timer = 0;
unsigned long timeDifference = 0;
char data;

void setup()
{
  Serial.begin(9600);
  BTSerial.begin(9600); 
  pinMode(LED, OUTPUT);
  delay(5000);      
}

void loop()
{
  BTSerial.write('A');
  timer = millis();
  while(!BTSerial.available()){}
  data = BTSerial.read();
  if(data =='B'){
    timeDifference = millis()-timer;   
  }
  Serial.println(timeDifference);

  if(timeDifference &lt; 280){
    digitalWrite(LED,HIGH);
  }else{
    digitalWrite(LED,LOW);  
  }
  delay(500);

}
</code></pre>
<p>slave device code:</p>
<pre><code>#include &lt;SoftwareSerial.h&gt;
//slave device code
SoftwareSerial BTSerial(5, 6); // RX | TX

char data;

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

void loop()
{
  while(!BTSerial.available()){}
  data = BTSerial.read();
  if(data =='A'){
    BTSerial.write('B'); 
  }

}
</code></pre>
<p>So what happens here is that master device sends a char and starts timer. Slave receives the char and send another char to master device. When master device receives char, it stops timer and looks at how much time passed. The problem is that when I put those bluetooth devices together, the time difference is like 280ms. When I move away in the room with slave device, it is still like same. It is not sensitive. What can I do to make this more sensitive? I am waiting for your opinions</p>
 2967: <p>The approach you're using will not work. You're trying to use speed of light time measurement to gauge distance. There are a whole host of problems with that. First, the clock on the Arduino is not fast enough (high enough resolution). You need accurate, sub-nanosecond time resolution for that (see this video: <a href="https://www.youtube.com/watch?v=9eyFDBPk4Yw" rel="nofollow noreferrer">https://www.youtube.com/watch?v=9eyFDBPk4Yw</a>). Second, BLE is a packet protocol. The overhead and variability of sending packets will totally swamp the <strong>tiny</strong> difference in round-trip time due to speed of light delay.</p>
<p>The usual way to gauge distance with BLE is to measure signal strength. That is also error-prone, but you can at least get rough &quot;immediate&quot;, &quot;near&quot;, &quot;middle&quot; and &quot;far&quot; distance measurements.</p>
<p>Alternately you could use and ultrasonic range sensor.</p>
