Can someone who knows electronics take a look at the attached diagram, and validate it as a working idea?
I want to be able to count the number of closed switched by reading the analog in.
("Pins" are an arduino.)
The way you have it, the analog pin will always read 5V no matter how many switches are closed.
You need to add a resistor from the analog pin to gnd.
I recommend putting higher value resistors to avoid loading the power supply too much...
@jerrytres Thank you, probably saved me a good few hours of confusion.
I can kind of reason why it needs to go to ground, but does it need to go to ground after the switches and resistors, not before?
@george_ if you're also interested in which of these buttons are closed and not just the number of closed buttons, you could use (slightly) different values for the R1-R5 resistors.
If you use e.g. 1k as R1, 1k + 22 Ohm as R2 etc. you should still be able to do a quite simple evaluation of the number of pressed buttons, by comparing to the voltage/ADC ranges for X buttons pressed.
@george_ Determine the max and min for a range by "pressing"=substituting-with-0 the X number of Resistors starting from the lowest values (min) and the highest values (max) via the formula given by @jerrytres .
E.g. if R1 has the lowest value and R4 has the highest one, you can get the min voltage for 3 buttons pressed by substituting R1,R2,R3 with 0 in that formula and the max value by substituting R4,R3,R2 with 0.
If the variations in the values are small, the ranges won't overlap.
@george_ if later on you decide to also care about wich exact button-combination is pressed, you can determine all the values for each combination with the same approach (I would use a spreadsheet) and compare to these (+tolerance) instead of the ranges determined in the previous approach.
The Arduino has a 10bit ADC? I believe (unverified) that should be accurate enough when using 1k, 1022, 1033, 1056 Ohms as R1 to R4 and 2k Ohms as R5 (again using @jerrytres post as base).
@dwagenk @jerrytres Interesting idea. Kinda like file permissions. I'm planning to coarsely read remaining water in a barrel, so they should only switch in a single order, so I can keep it simple. I'm already starting to think a single switch warning me that it's nearly empty is probably enough. But I like to graph things, so would like having more data points.
Fosstodon is an English speaking Mastodon instance that is open to anyone who is interested in technology; particularly free & open source software.