I used chatGPT. It was some work and some time to get this result, which I have tested but not to the extent it shoukd be tested.
Work of a kind different to programming. Way less real time from start to finish, with the sketch incorporating a number of enhancements I never thought of thinking about. I can say I also thought of a few things to make good better, like waving a magic wand or I guess telling the programmers you supervise to make a few changes. I will never know that kind of power for realz.
There were a few missteps. Amusing. Ordinarily I wouldn't waste time debugging an AI sketch, but this was quite plausible at glance and I felt it was worth trying to salvage.
I'm on the way to no longer in the lab. I will post my first prompt and the code when I get chance. A few of the things the AI didn't get right away are interesting as well.
Play with the bounce analyzer/contact timer here:
TBH if I wrote this it would be much smaller and do only a half-assed job of the matter.
Fun: in the wokwi you can turn off bouncing at the
pushbutton level; it made for a test of the essential logic which after all should not care whether the switch actually bounces or not.
Most button libraries and debounce algorithms tend to wait till the END of bouncing to report a switch state change.
Trying to measure switch state durations that are in the same ballpark as bounce times is always going to be ... annoying and/or ambiguous.
A logic-analyzer or scope would be a good start starting point.
For 0.01s timeframes, you can easily make a single-channel scope or logic analyzer using an AVR Arduino.
It should be sufficient to use "attachInterrupt()" to a pin, and log (to an array, for "later" printing) micros()... attachInterrupt() should have a latency of 10us ot so, and micros() has a resolution of 4us. It won't be perfect, but it should be close enough.
Actually it is simple, connect a resistor so it conducts current then connect a oscilloscope across the contacts and cycle. You can then use the cursors to measure the time once you get a decent signal triggered.
Hi Gil,
What I didn't mention was that the solution needs to be 100% portable and displays the timing on it's own micro display.
My bad... but in a lab situation... I love your solution -- awesome!
Best,
R
They make battery powered scopes for under $100.00. Also the scope will show each subsequent bounce as there are usually several, a counting system will usually only get one.
The code I posted counts the bounces, at some resolution.
A logic analyzer or oscilloscope would do a better job, but would not fit well in a "good enough" portable instrument based on an Arduino board.
And it is the duration of the stable contact that is of interest anyway. The code I posted does do a good enough job of measuring the bounce period if it does miss transitions during the periods of instability.