Wireless (separate) ultrasonic receiver and transmitter

Hi all

I have been trying to get one ultrasonic sensor (hc-sr04) to send and another to receive using rf to snyc the two arduinos

Now I have gotten them to work but the readings are never the same they vary about 2-4cm the whole time which is not very good for what I need, It is most likely timing issues that are not the same the whole time.

Is there perhaps a way to receive the rf signal with and interrupt?

Another questions - why is radiohead library so much slower than virtual library?

Thanks
M…

#include <VirtualWire.h>

int ultraTRIG = 5;
char *msg2 = "C";
int timer = 250;
void setup()
{
  pinMode(ultraTRIG,OUTPUT);
  vw_set_ptt_inverted(true);
  vw_setup(2000);
  vw_set_tx_pin(3);
}

void loop()
{
  digitalWrite(13, true);
  vw_send((uint8_t *)msg2, strlen(msg2));
  vw_wait_tx();
  digitalWrite(13, false);
  digitalWrite(ultraTRIG,LOW);
  delayMicroseconds(timer);
  digitalWrite(ultraTRIG,HIGH);
  delayMicroseconds(10);
  digitalWrite(ultraTRIG,LOW);
  delay(1000);//3000 is 3 second delay before next loop
}
#include <VirtualWire.h>

int usTRIG1 = 4;

int usECHO1 = 6;

char data;
unsigned long d1,d2;//distances
void setup()
{
  Serial.begin(9600);
  pinMode(usTRIG1,OUTPUT);
  digitalWrite(usTRIG1,LOW);
  pinMode(usECHO1,INPUT);
  vw_set_ptt_inverted(true);
  vw_setup(2000);
  vw_set_rx_pin(2);
  vw_rx_start();
  pinMode(13,OUTPUT);
}

void loop()
{    
  uint8_t buf[VW_MAX_MESSAGE_LEN];
  uint8_t buflen = VW_MAX_MESSAGE_LEN;
  if (vw_get_message(buf, &buflen)) // Non-blocking
  {
    int i;
    digitalWrite(13, true); // Flash a light to show received good message
    for (i = 0; i < buflen; i++)
    {
      if(buf[i] == 'C'){
        digitalWrite(usTRIG1,LOW);
        delayMicroseconds(10);
        digitalWrite(usTRIG1,HIGH);
        delayMicroseconds(10);
        d1=pulseIn(usECHO1,HIGH);//return pulse length in uS
        Serial.println(d1/29);//distance in cm, 29 not 58 because ultrasound is 1 way only, no reflection
      }
    }
    digitalWrite(13, false);
  }
}

I don't understand your code. You seem to send a ping from each sketch?

The first code is for the transmitter code and the second code is for the receiver.

These Hc-sro4 ultrasonic sensors you need to send out a ping before they start to receive, that is why I ping it (the receiving sketch) so that it starts receiving.

Is there another way?

How do you prevent the second ping from also reaching the receiver?

IR could be used instead of RF, or a simpler RF transmission without packets.

I have removed the ping transducer.

I want to use it outdoors so maybe IR wont work properly?

I am new to RF so not sure how to do that, but I think I need to implement an interrupt as soon as a message is received it starts the ping on the receiver because my sketch is going to get bigger which will make things worse

But how constant is the travel speed of RF that might also be why I am not getting constant readings.

A message may be transmitted multiple times, until it's received without errors. This can happen in every network. If you use a simpler protocol, that e.g. only sends a pulse, no such delays can occur. Travel speed of RF is almost constant, else GPS couldn't work.

You could add a wired connection between sender and receiver, where the sender signals the start of a transmission, so that you can find out whether there is a real and varying delay between the message transmission and reception.

Ok but when you use a simpler protocol and only send a pulse will a person not pickup noise from other RF signals ?

I am going to try to add that wire to see what the actual delay is.

thanks