Coding for Detect Distance

Hi, I'm Syahmeer. I have a problem here. I did the coding on arduino uno for tracking device by using bluetooth module cc41. The coding that I've been done as shown below is just to make a connection between bluetooth module and android smartphone.

#include <SoftwareSerial.h>

//RX AND TX PIN ASSIGNMENTS FOR THE ARDUINO uno ADK
int bluetoothTx = 2;
int bluetoothRx = 3;

SoftwareSerial bluetooth(bluetoothTx, bluetoothRx);

void setup()
{
//Setup usb serial connection to computer
Serial.begin(9600);

//Setup Bluetooth serial connection to android
bluetooth.begin(115200);
bluetooth.print("$$$");
delay(100);
bluetooth.println("U,9600,N");
bluetooth.begin(9600);
}

void loop()
{
//Read from bluetooth and write to usb serial
if(bluetooth.available())
{
Serial.print((char)bluetooth.read());
}

//Read from usb serial to bluetooth
if(Serial.available())
{
bluetooth.print((char)Serial.read());
}
}

The bluetooth module are paired with the phone.
what is the coding to detect bluetooth module out of range (7 metre) from the phone? then what is the coding to send signal (alert) to the phone when the bluetooth module out of range (7 metre) from the phone ?

Thank you in advance for your help. This is urgent matter so I hope one of you can help me to settle down this problem. :slight_smile:

Can you post a link to the datasheet for your Bluetooth device. The answer is probably within it.

...R

then what is the coding to send signal (alert) to the phone when the bluetooth module out of range (7 metre) from the phone ?

Think about this question. Think about why it is impossible to send anything to the phone when the phone is out of range.

Unless the phone sends a continuous signal, and the Arduino reads the signal, and panics when there is nothing to read for a period of time, it is NOT possible to detect the phone going out of range.

I agree with @PaulS's first paragraph in Reply #2

However I believe the HC05 Bluetooth modules can be queried to see if a device is in range and I'm guessing the module the OP is using has a similar feature. Of course that only tells the Arduino there is a problem. The Arduino won't be able to pass it on to the phone.

Perhaps it is possible to write an Android app that can do the equivalent on the phone.

...R