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);
}
}