Hi, I have a simple program that measures the width of a pulse every second or so, and displays the value on the Adafruit 32x32 RGB Matrix LED panel. The measurement code and the display code work separately but when they are used in the same program, the measurement values become grossly underestimated and unstable. It occurred to me that both the RGB panel library and the pulseIn() function use timer, and it's likely that the RGB panel lib code is changing or resetting the timer used by the pulseIn() function.
So here's my question. Is there an easy work around so I can either briefly hold the execution of the RGB panel code while using pulseIn(), or make one of the two use a different timer? Since the pulse width I am measuring is less than 10 ms, the effect on the display should be minimum if the display code is stopped momentarily.
Lejaune:
the pulse width I am measuring is less than 10 ms
Maybe don't use pulseIn()? Just capture the value of micros() when you see the pulse start, and when you see it end, and subtract the figures. The code running the panel may cause some jitter in the measurements, but maybe you could take average or minimum over several readings?
PaulRB:
Simplified, but still too large to post in code tags?
No, not large. I just can't find a way to upload in "code tag". I can copy and paste as plain text but I am not sure how the formatting would do to it.
PaulRB:
Maybe don't use pulseIn()? Just capture the value of micros() when you see the pulse start, and when you see it end, and subtract the figures. The code running the panel may cause some jitter in the measurements, but maybe you could take average or minimum over several readings?
Yes, micros() works much better. Thanks!
I took a look at the source file (wiring_pulse.c) for pulseIn() function, and it seem that time measurement is done by counting the number of loops of execution, which could explain why it's not accurate because of the interrupts generated by the RGB matrix. Furthermore, in the same file, there is a pulseInLong() function that actually uses micros(), and it is accurate. Somehow, pulseInLong() is not listed in the Arduino Reference.
Somehow, pulseInLong() is not listed in the Arduino Reference.
Good find. +1 Karma. However, the fact that it is not shown on the Arduino site may mean it is not officially supported, and could be removed without notice.
Well, the pulseInLong() is now on the Arduino site but not in the reference html docs included in my 1.8.8. Looks like they recently renovated the web site.
Lejaune:
Well, the pulseInLong() is now on the Arduino site but not in the reference html docs included in my 1.8.8. Looks like they recently renovated the web site.
Cool. Switch to using that and make your code more "arduino-style".