Very Brief Overview:
The AVR's digital inputs are Schmitt-Triggered, allowing for slowly rising/falling and/or noisy "edges" to be "cleaned up" into sharp 0->1 or 1->0 transitions. The threshold-voltages for each transition differ, but are quite stable, thus slowly-changing transitions are relatively immune to noise.First, set the pin as an output to charge the capacitor.
Next, switch the pin to an input and measure the time it takes for the capacitor to discharge.
The code has been developed specifically to improve noise-immunity and to reduce CPU-waiting *dramatically*, via either Blocking or Non-Blocking methods, which can be called repeatedly from the main 'while()' loop.
This, like all my projects, is ongoing... It's currently at version 0.50-9. There's always room for improvement, and it's used as a piece of many of my other projects. (e.g. audioThing)
(In the picture, a Nokia Phone's main PCB has been stripped of all its components, revealing solder-pads for the rows/columns in its matrix-keypad. Resistors were soldered to these pads, as shown in the schematic, atop Kapton tape to insulate them from the PCB.)
This system *definitely* depends on your circuitry-characteristics... When a button is pressed, the system detects which button based on thresholds on time and voltage, both analog values... It works fine in my lab-environment, but may be questionable in other environments.
Some things that affect its measurements:
* AVR clock-frequency
* Quite possibly different AVR Chips with the same part-number will respond differently, due to slightly-different threshold voltages...?
* Large variances in AC electrical-noise...
(6-16-15) #Analog Input Hack: Push Button Encoder looks like it could be a pretty clever usage.
(6-20-15): Non-Blocking Capacitive Sensing (Disco Dance Floor)... dunno why I never thought to use this with capacitive sensing. Seems doable.
As-Implemented (Default) anaButtons doesn't respond until a "button-press" is "released", for the sake of debouncing/noise-immunity. It's been a while since I looked at the code, but I think there's already an option to respond to a "button-press" *while* it's pressed, which is how a "switch" setup like this would appear to the system (as it remains unrotated in a quadrature state).
I dig the Encoder-concept enough that I might try to figure out how to work out that debouncing/noise-immunity for cases like these. (definitely revisit: https://analog10.com/posts/rotary_encoder_analog_input.html and also: http://hackaday.com/2015/06/15/king-of-clever-reads-4-pin-rotary-encoder-with-one-analog-pin)
Basically, this system *is* a capacitive sensor, wherein the capacitor is a fixed-value and the resistors are varied... why not use it where the capacitance is varied? Should be a relatively easy modification.
Jeremy's write-up (linked above) is quite good, though. (huh, can't 'at' him, for some reason).