Delay problem/timing

Hi guys im having trouble getting my timing right here, If i put a delay of more than 200us in my receiving sketch I get wrong readings on my sensors and for my project to work i need some delays in place.

Any feedback is much appreciated.

Receiving sketch:

#include <VirtualWire.h>
#undef int
#undef abs
#undef double
#undef float
#undef round
#define usTRIG1 4
#define usTRIG2 8
#define usECHO1 6
#define usECHO2 11
char data;
unsigned long t1,t2;//timers
const int pin = 12;
void setup()
{
  Serial.begin(9600);
  pinMode(usTRIG1,OUTPUT);pinMode(usTRIG2,OUTPUT);
  digitalWrite(usTRIG1,LOW);digitalWrite(usTRIG2,LOW);
  pinMode(usECHO1,INPUT);pinMode(usECHO2,INPUT);
  vw_set_ptt_inverted(true);
  vw_setup(2000);
  vw_set_rx_pin(2);
  vw_rx_start();
  pinMode(13,OUTPUT);
  pinMode(pin,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] == 'E'){
          digitalWrite(usTRIG1,LOW);delayMicroseconds(10);digitalWrite(usTRIG1,HIGH);delayMicroseconds(10);digitalWrite(usTRIG1,LOW);
          t1=pulseIn(usECHO1,HIGH);//return pulse length in uS
        }
        if(buf[i] == 'C'){
          digitalWrite(usTRIG2,LOW);delayMicroseconds(10);digitalWrite(usTRIG2,HIGH);delayMicroseconds(10);digitalWrite(usTRIG2,LOW);
          t2=pulseIn(usECHO2,HIGH);
          Serial.print(t1/29);Serial.print("\t");Serial.println(t2/29);//distance in cm, 29 not 58 because ultrasound is 1 way only, no reflection
        }
        }
     digitalWrite(13, false);
     }
        
}

Sending sketch:

#include <VirtualWire.h>
#undef int
#undef abs
#undef double
#undef float
#undef round
#define ultraTRIG 5
char *msg1 = "C",*msg2 = "E";
int timer = 200,timer2=150;
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 *)msg1, strlen(msg1));vw_wait_tx();digitalWrite(13, false);
  digitalWrite(ultraTRIG,LOW);
  delayMicroseconds(timer);
  digitalWrite(ultraTRIG,HIGH);delayMicroseconds(10);digitalWrite(ultraTRIG,LOW);
  delay(timer2);
  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(3000);//3000 is 3 second delay before next loop
}

Even if you use only the rx pins, all the other pins are still used (set as output).
The default pins for the VirtualWire are 10 (ptt),11 (rx),12 (tx). You better not use them for something else.

It looks like you are trying to triangulate a position by measuring the distance between one remote ultrasonic sender and two local ultrasonic receivers. I take it the delay is to account for the receive time of the triggering message so the receiver can be triggered at the same time as the sender. In what way are the results "wrong" when you use a delay over 200 and why do you want to use a higher value if it doesn't work?

You might get better results if you used the OOK transmitter and receiver directly rather than layering a messaging library (VirtualWire) over the OOK transmission. Look at Ben Heckendorn's robotic luggage:

Hi John thanks for your reply, yes you are spot on.
My readings are correct with the code as is but because Im still new to all this arduino stuff im stuck.

I want to add some movement now like

if(t1 > 20){
analogWrite(motor,255)
}

But now if i add this code to my receiving sketch first of all It doesnt matter if my sensor reading is 10 or 25cm etc the motor keeps running or the led stays on if i do a digitalWrite high secondly I thought maybe I need a delay before I add the new if statement for the motor so that there is enough time for the arduino to store the serial readings but when I add the delay it puts my sensor reading out.

M....

analogWrite() and digitalWrite() are 'sticky': the value you write will remain in effect until you write a different value. When you want your motor to stop you will need to analogWrite(pin, 0) or digitalWrite(pin, LOW).

Ok finally I got some clarity.

I put digitalWrite Low at the top of my loop

and this at the bottom:

if (t1 > 900)
     {
       digitalWrite(pin, HIGH);
     }

As you can see that 900cm is very high and that is what was messing me around but that 900cm corresponds to +- 30cm on my serial monitor so now the led only goes on if the distance is just over 30 cm.

If you have any idea why the serial monitor and the distance in the sketch are different please share your knowledge.

Thanks
M...

Undermentioned:
As you can see that 900 cm is very high and that is what was messing me around but that 900 cm corresponds to ~30cm on my serial monitor so now the led only goes on if the distance is just over 30 cm.

If you have any idea why the serial monitor and the distance in the sketch are different please share your knowledge.

It's because t1 is a time in microseconds, not a distance in centimeters. When you display it you divide by 29 to convert from microseconds to centimeters. That is why the distance display shows 31 when t1 is 900 microseconds.

Ok thanks John, I got it :grinning: .

Much appreciated.