I want to measure the time elapsed between my two arduino using rx/tx module in microseconds. I made my codes and I notice that something wrong in my output. I hope you can help me.
Both A
(device 1) and B
(device 2) are responsible for measuring a time delay accurately using a local clock.
- If the time
A
sends the signal isTSA
, - the time
B
receives the signal isTRB
, - the time
B
replies to isTSB
, - the time
A
receives the signal back isTRA
- such that
TSA < TRB <TSB < TRA
, - then
A
measuresTA = TRA -TSA
and B
measuresTB = TSB - TRB
.
The TOF can be estimated by combining these two measurements:
Total time elapsed = (TA-TB)/2
TRANSMITTER code
#include <VirtualWire.h>
#include <LiquidCrystal.h>
LiquidCrystal lcd(7, 6, 5, 4, 3, 2);
const int transmit_pin = 12;
const int receive_pin = 11;
char *c;
unsigned long received, sends, elapsed;
void setup() {
Serial.println();
Serial.begin(9600); // Debugging only
//transmitter settings
pinMode(13, OUTPUT)
vw_set_ptt_inverted(true); // Required for DR3100
vw_set_tx_pin(12);
vw_setup(1000); // speed of data transfer Kbps
//receiver settings
Serial.println();
Serial.begin(9600); // Debugging only
vw_set_rx_pin(11);
vw_rx_start();
}
void loop() {
//Transmitter
digitalWrite(13, 1);
c = "1";
vw_send((uint8_t *)c, strlen(c));
vw_wait_tx(); //Wait until the whole message is go
delay(1000); // for debounce
sends=micros();
//Receiver
uint8_t buf[VW_MAX_MESSAGE_LEN];
uint8_t buflen = VW_MAX_MESSAGE_LEN;
if (vw_get_message(buf, &buflen)) { // Non-blocking
for(int i = 0;i < buflen;i++) {
if(buf[i] == '2') {
digitalWrite(13, 0);
delay(1000); // for debounce
received=micros();
elapsed=(received-sends);
Serial.print(sends);
Serial.println(" TRANSMITTED TIME");
Serial.print(received);
Serial.println(" RECEIVED TIME");
Serial.print(elapsed);
Serial.println(" microseconds elapsed");
}
}
}
}
RECEIVER code
#include <VirtualWire.h>
const int receive_pin = 11;
const int transmit_pin = 12;
char *chars;
unsigned long received, sends;
void setup() {
Serial.println();
Serial.begin(9600); // Debugging only
//transmitter settings
vw_set_ptt_inverted(true); // Required for DR3100
vw_set_tx_pin(12);
vw_setup(1000); // speed of data transfer Kbps
//receiver settings
vw_set_ptt_inverted(true); // Required for DR3100
vw_set_rx_pin(11);
vw_setup(1000); // Bits per sec
pinMode(13, OUTPUT);
vw_rx_start(); // Start the receiver PLL running
}
void loop() {
//Receiver
uint8_t buf[VW_MAX_MESSAGE_LEN];
uint8_t buflen = VW_MAX_MESSAGE_LEN;
digitalWrite(13, 1);
if (vw_get_message(buf, &buflen)) { // Non-blocking
for(int i = 0;i < buflen;i++) {
if(buf[i] == '1') {
received=micros();
//Transmitter
chars = "2";
vw_send((uint8_t *)chars, strlen(chars));
vw_wait_tx(); // Wait until the whole message is gone
digitalWrite(13, 0);
delay(1000);
sends=micros();
Serial.print(received);
Serial.println(" RECEIVED TIME");
Serial.print(sends);
Serial.println(" TRANSMTTED TIME");
}
}
}
}//End for Loop
Transmitter output
Received time should be more larger as you can see in my formula.
Receiver output
I hope you can help me what is the problem on my program.