pimatic / RFControl

433mhz rf sniffer and receiver
GNU General Public License v3.0
117 stars 34 forks source link

Noise filtering #16

Open kirill9617 opened 7 years ago

kirill9617 commented 7 years ago

I had noise with pulse duration greater than signed int and this patch fixes my problem.

mwittig commented 7 years ago

Can you please squash your commits into a single commit? Thanks

mwittig commented 7 years ago

I think the root problem is an overflow with the calculation of "duration" at https://github.com/pimatic/RFControl/blob/master/RFControl.cpp#L290

duration is an unsigned int (16bit) value while currentTime (and lastTime) hold 32 bit values. This will overflow in particular in the first cycle when lastTime is 0, but it will also overflow at longer intervals ... 65536 microsecs * 4 = if the interval is longer than 200 ms roughly.

Another thing to consider is timer rollover, which happens roughly every 50 days if I calculated it correctly. http://playground.arduino.cc/Code/TimingRollover

sweetpi commented 7 years ago

@kirill9617 Nice catch. As @mwittig mentioned: I think the pull request does not fix the problem itself.

We should check that the result of (currentTime - lastTime) / PULSE_LENGTH_DIVIDER does fit into a unsigned int (16bit). If it does not fit then we could skip the pulse.