this is my first post here. I'm quite new to electronics and I'm afraid this will be a rather trivial question to ask. I've browsed through the forum since I believe this will be a standard question but I probably don't know what key-phases to search for.
I have a number of latching switches which I want to route to Arduino inputs. Problem is overall I have quite a few more input signals than the Arduino has pins. To my understanding the standard solution would be an Encoder but I believe I can't use this since an encoder asserts that only one input is HI at any given time, which is not the case in my situation. Any combination of switches can be HI at one time.
If I can still use an Encoder (or a Multiplexer) in this case you can stop reading here and let me know the trick
However to test an idea how to do this without ICs I've built a rather simple five-input voltage divider:
The combination of resistor values guarantees a unique voltage on the MEAS pin in the diagram for all combinations of input HIs. So that's fine. The readings I get when I connect this to one of the analogue pins of the Arduino match what I pre-calculated manually so that's good as well.
The brute-force approach to figure out in a sketch which inputs are HI for any voltage on the analogue pin is of course an array of sorts with 32 values, a 32-branch IF-ELSEIF monstrosity or another ugly thing. However I can't shake the feeling that by "reordering" the equations for voltage divider & parallel resistors I should be able to let Arduino figure that out mathematically as well.
And that's where I struggle. I just can't get the math right - school is a long long time ago.
Might try the map function to map the input of 0 - 1023 to 0 - 32. then use a array of 32 results and index the array with the map results. For this to work, your results need to have even distribution of values.
Shift registers might indeed be a viable option, thanks a lot for reminding me of those things @BulldogLowell :). I believe I'd be able to able to scale this up to quite a few morebuttons without the need to increase the number of pins on the controller above four.
You may have calculated unique values for the resistor combinations, but did you calculate them for an equal spread of voltages at each combination? We would generally expect the resistor values to be in a binary progression; each one being approximately double the previous. An "R-2R" sequence as mentioned, is a way of doing this neatly.
The crude tutorials on the Arduino pages refer to connecting switches (push-buttons) to the 5V reference. This is bad engineering practice; any "external" connections should switch to ground where possible to avoid having the 5V supply exposed. Your final R6 can then go to the supply. The values you then read are inverted, but that is a trivial logic concern.
Finally your resistor scale is unnecessarily low - you should increase it by ten times to conserve current. There is a sometime problem with "contact wetting" with switches which carry next to no current, but that will always be a concern with this circuit.
Thanks for the tip about resistor ladders, reading it now.
About those even voltage distances, no they're not even at all with my resistor values but frankly I doubt that's possible with this circuit due to how parallel resistor networks work. Since any combination of inputs can be HI at the same time the voltage difference varies greatly, depending on which inputs are HI.
I started simulating this circuit with resistor values like 100, 200, 400, 800, 1600 Ohm which is the natural thing to do like you said Paul. Thing is it doesn't make a difference in regards to evenly spread the resulting voltages. In the end I picked resistor values that I had flying around here and made that prototype as a proof of concept. But you're right, the values proved to be a mediocre choice wasted current like you said. Its "good enough" for me as proof of concept but you're absolutely right.