Multiple Threads Programming

I’m Marco and I’m quite new with Photons and Electrons. I’m encountering some problems in setting up a new project: I would like to read a frequency input: the system is the same of a bike computer, simply it counts the times that the magnet in the wheel pass through the sensor. Now I’m not taking about bicycles, but cars (I have 8 magnets in a single wheel. No matter, just faster readings).
I found that the pulseIn() function do exactly what I need, and obviously it simplifies a lot the programming process. My problem is when I try to read 4 wheel rotation speeds and I want that the loop() is always at 10Hz. I have to read one wheel at time, but every response is depending on the wheel speed (the faster you go, the faster is the time between readings).

I would like to separate the readings of the wheel from the loop function, trying to create two or more threads. Is it possible to do that or multithreading is available only for Cloud Backgrounds?
Have you any idea on how can I solve the problem?
Thanks in Advance!

1 Like

For something like this you should consider using interrupts rather than pulseIn()

1 Like

Thanks. I am evaluating to use interrupts that detects the High level on the pins. In the meanwhile I used timers and I think the problem is not completely solved: I can see that functions are called separately and at 100Hz it seems working everything correctly. But there could be some delays anyway (the frequency of the main loop is not precisely constant).