Virtual Wire question

Hello,

I have set up the tranmitter and reciever and they are working fine. my question with the programming is, i want the receiver to always be checking for the pressence of the transmitter and if it gets out of range a pin goes high until its back in range. I edited a code from instructables written by Mohannad Rawashdeh

 #include <VirtualWire.h>
 char *controller;
 void setup() {
   pinMode(13,OUTPUT);
 vw_set_ptt_inverted(true); //
 vw_set_tx_pin(12);
 vw_setup(4000);// speed of data transfer Kbps
 }

 void loop(){
 controller="1"  ;
 vw_send((uint8_t *)controller, strlen(controller));
 vw_wait_tx(); // Wait until the whole message is gone
 digitalWrite(13,1);
 delay(100);


 }
 #include <VirtualWire.h>
 void setup()
 {
     vw_set_ptt_inverted(true); // Required for DR3100
     vw_set_rx_pin(12);
     vw_setup(4000);  // Bits per sec
     pinMode(13, OUTPUT);

     vw_rx_start();       // Start the receiver PLL running
 }
     void loop()
 {
     uint8_t buf[VW_MAX_MESSAGE_LEN];
     uint8_t buflen = VW_MAX_MESSAGE_LEN;

     if (vw_get_message(buf, &buflen)) // Non-blocking
     {
       if(buf[0]=='1'){

   
    digitalWrite(13,0);
       }  
    else
    digitalWrite(13,1);
     }

 }
 }

This code sends a char '1' ten times a second. and I want the pin13 on the reciever to stay low as long as that is true. The problem with this setup is the "vw_get_mesage(buf, &buflen)" seems to check for that char but when it recieves it it just stays that way until a new char is sent and it does not care if there is nothing being sent,

Zalsobrooks:
This code sends a char '1' ten times a second. and I want the pin13 on the reciever to stay low as long as that is true. The problem with this setup is the "vw_get_mesage(buf, &buflen)" seems to check for that char but when it recieves it it just stays that way until a new char is sent and it does not care if there is nothing being sent,

I think you will want your receiver to

  • switch the LED off and reset a timeout counter when a message is received
  • switch the LED on when the timeout counter timed out

Perhaps something like:

unsigned long timeoutStart;
void loop()
{
  uint8_t buf[VW_MAX_MESSAGE_LEN];
  uint8_t buflen = VW_MAX_MESSAGE_LEN;

  if (vw_get_message(buf, &buflen)) // Non-blocking
  {
    if(buf[0]=='1')
    {
      digitalWrite(13,0);
      timeoutStart=millis();
    }
  }  
  if (millis()-timeoutstart>200)
  {
    digitalWrite(13,1);
  }
}