I am trying to measure RF signal HIGH duration. My sender and receiver code are at the end of this message.
I have two Arduino's. Just to be sure I've tried VirtualWire send/receive on them and it works fine. So the equipment and connections are OK. I've also tried to connect pin 11 to 12 directly (plain wire connected) and then I get my code running fine ok that way too. So my code should be OK.
But when I use my send/receive using RF I get a wrong duration. For example I've set it to send 500 milliseconds HIGH but my receiver gets 105-150 milliseconds. Furthermore if I change the send HIGH duration to 250 millisecond I still get same 105-150 milliseconds. I don't think noise is a factor: it is much shorter than 100 milliseconds.
So what is going on? What am I missing? Are OFF noise signals somehow blanking a HIGH from my sender?
Here is my sender code:
#define SENDERPIN 12
void setup()
{
Serial.begin(9600);
Serial.println("setup rfout");
pinMode(SENDERPIN, OUTPUT);
digitalWrite(SENDERPIN,LOW);
}
void loop()
{
int i,i2;
i2=100;
for (i=0;i<i2;i++)
{
Serial.print("Round: ");Serial.print(i+1);Serial.print("/");Serial.println(i2);
digitalWrite(SENDERPIN,1);
delay(500);
digitalWrite(SENDERPIN,0);
delay(1);
delay(5000); // odota 5 sek
}
}
Here is my receiver code
#define RECEIVEPIN 11
void setup(){
Serial.begin(9600);
Serial.println("setup receive");
pinMode(RECEIVEPIN,INPUT);
}
void loop()
{
long time1,time2;
while (1) // wait for a HIGH
{
if (digitalRead(RECEIVEPIN)==1)
{ // got HIGH, exit and start timeing it's duration
break;
}
}
time1=millis(); //start time for start of HIGH
while(1)
{
if (digitalRead(RECEIVEPIN)==0)
{ // HIGH ends, now we have LOW, measure how much time was on HIGH
time2=millis()-time1; // how much time was on HIGH
if (time2>100 && time2<800)
{ // cut some noise and try to focus on sender's 500 millisec
Serial.println(time2);
break;
}
else
{ // got a short HIGH
while (1) // wait until the short HIGH is back to LOW
{
if (digitalRead(RECEIVEPIN)==0)
break; // got OFF break and start at inition while -loop
}
break;
}
}
}
}