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