Sunday 17 March 2024

Avoiding Arduino millis() Rollover Bugs

If you are using the millis() function to measure time on an Arduino, you might be tempted to do something like this:-

unsigned long timer_expires_at = millis() + SOME_DURATION; while (millis() < timer_expires_at) { [do other stuff while waiting for the timer to expire] } [do stuff that should happen after the timer expires]

For the most part, that will work well. But after around 49.7 days (4,294,967,296 a.k.a 232 milliseconds, to be exact), you might run into a problem. If millis() + SOME_DURATION exceeds 4,294,967,295, it will rollover and your comparison will match prematurely. Of course, you would have to be pretty unlucky for this to occur in practice, but - as Mary Chapin Carpenter put it - Sometimes you're the Louisville Slugger, sometimes you're the ball.

I had plans sketched out for a small C++ class to handle this elegantly when I decided to do a quick bit of Googling to see if I was reinventing the wheel entirely here and I came across the most beautiful simple, and elegant solution to the problem in https://arduino.stackexchange.com/questions/12587/how-can-i-handle-the-millis-rollover/12588#12588. It is a long, detailed reply and well-worth reading, but the gist of it is to just change the logic very slightly:-

unsigned long timer_started_at = millis(); while (millis() - SOME_DURATION < timer_started_at) { [do other stuff while waiting for the timer to expire] } [do stuff that should happen after the timer expires]

It should be pretty clear how this avoids the problem after ~49.7 days, but it may be less obvious why it doesn't just shift the problem to the other end.  Imagine that your Arduino has been powered on for 30 seconds, so a call to millis() will return a value of 30,000. You want to wait for 40 seconds, so you set SOME_DURATION to 40,000. Subtracting 40,000 from 30,000 in the condition of the while() loop will indeed cause a rollover (30,000 - 40,000 mod 4,294,967,296 = 4,294,957,296), but the result of that rollover will still satisfy the condition in the way we want.

Antenna Experiments

Let me state from the outset that my knowledge of RF and antenna design is very rudimentary. I basically consider it to be black magic.

With that out of the way, I read something a while back that made me wonder if it was possible to measure antenna resonance with a track-generator equipped spectrum analyser.  In particular, with two identical antennae - one connected to the track-generator output and one connected to the spectrum analyser input - would there be an obvious peak in response at the antenna's resonant frequency.  That is an experiment worth trying.  

Here is the initial setup:-


 It is sweeping from 0 - 1.5GHz and there is already a very obvious peak.  Looking closer:-

The antennae are intended for use with DFRobot APC220 radios which have a frequency-range of 418-455MHz.  So in the ballpark, even if the measured peak is kind of low.  There is a significant difference between performance at the two ends of that frequency range, though. Reducing the frequency span on the spectrum analyser from to 418-455MHz:-


There is nearly 15dB difference between 418MHz and 455MHz.  Still...I can't say for sure what the resonant frequency of the antenna is actually supposed to be: perhaps it is 407.5MHz and the result is completely valid.

For the second test, I replaced the both antennae with a simple monopole consisting of a piece of coax with 16.5cm of the shielding stripped back.  This should resonate at a wavelength of 4 × 16.5cm = 66cm.  Assuming the speed of light is  3 × 108m/s, that should be equivalent to 454.5MHz.  Let's see:-

Far from conclusive. The peak is at 152.5MHz, way over on the left.  There is a second peak at about 415MHz.  Zooming in to the 418-455MHz range:-


There is no sign of a peak of any kind near the resonant frequency.  

My "assumption" above about the speed of light wasn't entirely tongue-in-cheek.  The headline figure of 3 × 108m/s is the speed of light in a vacuum. Signals travel through coax cable significantly slower than this.  According to a datasheet for RG179 coax that I checked (I think that the kind of coax I have), the velocity factor is 0.7.  That would put the speed of the signal propagating through that coax at closer to 2.1 × 108m/s and - if the wavelength-to-frequency computation should take this into account - would put the resonant frequency at 318MHz.  I have no idea if the velocity factor should be applied here or not: if you know, please leave a comment.  There is certainly no bump in the output of the spectrum analyser at or near 318MHz that would give a clue either way.

Finally, I replaced the two coax monopoles with two lengths of wire, balanced precariously in the SMA connectors:-


The pieces of wire are identical and are both 10.5cm long. Looking at them as 1/4 wavelength monopoles and applying the equation, their resonant frequency should be 714MHz.  It doesn't appear to be:


There is indeed a strong peak, but it is nowhere near where it is supposed to be.  Interestingly, if I apply the velocity factor of 0.7, things look a lot better: that would imply a resonant frequency of 500MHz.  But I have no reason to believe that the velocity factor of a scrap of wire is 0.7, even if I knew that it was supposed to be included in the calculation.  I'm just fudging numbers here.

I got a similar result with 16.7cm of the same wire:


A good strong peak at 345MHz where is nowhere near the predicted 449MHz, but in the right ballpark if I apply the magic velocity factor of 0.7.

If you read down this far hoping for a satisfying conclusion and some enlightenment then I can only apologise for the disappointment you must be feeling.  I promise you, I feel your pain.

Nothing in any of this has done anything to shake my conviction that RF and antenna design is a black art.


Rotary Encoders

These are wonderful little things.

I got to thinking a bit about exactly how they worked, so I hooked one up to a power-supply and an oscilloscope.  There is 3V being delivered to the centre pin and one oscilloscope channel connected to each of the the two outer pins.  At rest, the centre pin isn't connected to either of the outer pins.  Turning the encoder clockwise...

...you can see that the blue trace goes to 3V a little before the yellow trace (the blue channel is the left-hand "A" pin on the encoder). 

Turning the encoder counter-clockwise and the opposite happens:-

Here the yellow trace (the "B" pin) goes to 3V before the blue one.  The key thing is that as the encoder passes over every detent, there is a point at which both pins are connected to the centre pin, but the direction of rotation determines whether the "A" pin gets there first or the "B" pin.

What sent me down this particular rabbit-hole was a very interesting pair of YouTube videos about using using just digital logic (i.e. no microcontrollers), here and here.  I had never previously thought about using these without a microcontroller and thinking about them in this way does reveal some interesting insights.