VirtualWire and PulseIn() issue

Hello,

I am having trouble combining VirtualWire library with PulseIn() function. In the code below everything works fine if I remove the lines between "check input start" and "check input end" and uncomment "//char *msg = "1";" line. I am able to send and receive information between two Arduinos. But as soon as PulseIn() call is added the communication breaks. Do you have any suggestions how to solve this issue?

Thanks,
Nick

#include <VirtualWire.h>
int trxPin = 3; // transmitter pin
int channelInPin = 7; // pin on which Arduino is listening for the command channel
int pulseDuration;
boolean printDebugInfo = true;

void setup()
{
  Serial.begin(9600);	  // Debugging only
  Serial.println("setup");
  
  // Initialise the IO and ISR
  vw_set_ptt_inverted(true); // Required for DR3100
  vw_setup(2000);	 // Bits per sec
  vw_set_tx_pin(trxPin);
  pinMode(channelInPin, INPUT);
  digitalWrite(channelInPin, HIGH);
}

void loop()
{
  /* check input start */
  char *msg;
  pulseDuration = pulseIn(channelInPin, HIGH, 3000); //get the channel level 

  p("Pulse Duration", pulseDuration);
  if (pulseDuration > 1900 && pulseDuration < 2200)
  {
    const char *msg = "1";
    p("Sending",1);
  }
  else
  {
    const char *msg = "2";
    p("Sending",0);
  }
  /* check input end */
  
  //char *msg = "1";
  digitalWrite(13, true); // Flash a light to show transmitting
  vw_send((uint8_t *)msg, strlen(msg));
  vw_wait_tx(); // Wait until the whole message is sent
  digitalWrite(13, false);
  delay(200);
}

void p(String label, int value)
{
  if (printDebugInfo)
  {
    Serial.print(label);
    Serial.print(": ");
    Serial.println(value, DEC);
  }
}

First, I suggest you change your p function to read like this:

void p(const char * label, int value)

That saves almost 1000 bytes of sketch size, and is much less likely to fragment memory.

OK, sure. I've added this only for debugging. But, do you have an answer to my question?

  char *msg;
...

  if (pulseDuration > 1900 && pulseDuration < 2200)
  {
    const char *msg = "1";
    p("Sending",1);
  }

You have two different msg variables here you know. One with undefined contents (the first one) and a second one, which you don't use. This is likely to cause it to crash.

This would be better, but untested:

  const char *msg;
  pulseDuration = pulseIn(channelInPin, HIGH, 3000); //get the channel level 

  p("Pulse Duration", pulseDuration);
  if (pulseDuration > 1900 && pulseDuration < 2200)
  {
    msg = "1";
    p("Sending",1);
  }
  else
  {
    msg = "2";
    p("Sending",0);
  }

You are right, Sir. Thank you.

It is sending data now, but PulseIn() still does not seem to work. I am getting zeros and occasionally random numbers. Pin 7 is connected to one of the channels of Spektrum RC receiver. I can control the interval of the output signal there and set it to any value between 1000 and 2000 uS. When we have interval around 2mS, that should be picked by the Arduino, but it is not happening.

I'm not a big expert on pulseIn.

You can count pulse widths with a timer, I don't know if this will help or not:

That's a good reading, but way too advanced for me. I just want to be able to measure time intervals between 1 and 2 mS when VirtualWire library is enabled.

With a lot of this stuff, the devil is in the detail, as they say.

You have a 3 mS timeout on your pulseIn. Are you sure the pulse starts in that time?

Yeah, that's a good point. I will hook an oscilloscope and see what's going on. The interesting thing is that when VirtualWire library is used, PulseIn() function returns incorrect values. For example, if pulse duration is 1900 uS the reported value is around 1500 uS. And when the pulse is 1000 uS, the reported value is around 850 uS. As soon as I remove the library, the durations are reported correctly.