I would like to build up a distance measuring system by using radio waves, instead of the commonly used way - Ultrasonic.
Firstly, assuming there's no interference between two objects(object A and object B.), and the distance in between is dozens of kilometers.
So what I use are two 900Mhz radio modules, and connect each of them to a Arduino Uno to be Object A and Object B.
Here is the logic of design:
At object A, radio module are controlled to transmit a beacon(a specific character) every 3seconds,
At object B, when radio module receive this specific character, it will return another character back(assuming as returning beacon).
By using the Arduino at object A to calculate time interval between transmitting beacon and receiving returning beacon, it is possible to invert the distance between A and B, if doing (speed of light)* (time interval)/2.
The code are attaching below,
Object A
unsigned long U;
char C;
char K;
unsigned long P;
void setup() {
Serial.begin(9600);
}
void loop() {
delay(3000); //transmitting beacon every 3 seconds
U = micros(); // T1
Serial.print('L'); //The specific character(transmitting-beacon)
if(Serial.available()>0); // if receive returning-beacon
{
C = Serial.read();
switch(C)
{
case 'J':
P = micros()-U; () // should be the time interval? T2 - T1
Serial.print(P); //print the time interval
break;
}
}
}
Object B
char N;
char L;
char K;
void setup() {
Serial.begin(9600);
}
void loop() {
if(Serial.available()>0); // if receive the transmitting-beacon from object A
{
N = Serial.read();
switch(N)
{
case 'L':
Serial.print('J'); // returning-beacon
break;
}
}
}
Since electronic wave travel at time speed, 300000 km / sec = 300 meter / microsecond
The expected time-interval should be around 200 microsecond if the distance in between is 30km; however, the value I actually got was way more different from "200", which were 12 , 16 or 20.
I think the problem is definitely about the programming issue... which I couldn't figure out yet.
So I am here looking for any suggestion.
Thanks in advance !