I made an analog assembly with 4 pushbuttons.(see attached file).
I want to reach the specific case below:
When i push S5, Led D1 is on, when i push S6 while S5 Led D1 is still on but not D2, it's only after releasing S5 that the Led D2 is on and the Led D1 is off.
This could happen with any buttons from S5 to S8 in any order.
Do someone think it's possible with an analog set-up like mine?
I am not sure if this is a cross post with the debounce questions, but I suspect it is. Yes, what you show here is possible, assuming only one button will ever be pressed at a time. Given the working theory that pressing any one of the buttons will result in a different value for analogRead(). This is a very dirty way to do what you want to do. You will need to first find out the actual value returned from analogRead(), from each button press, and hard-code that in your program, for each of these circuits you build, as it will differ from unit to unit. Also, if two buttons are pressed at the same time, analogRead(), will return the value based on the parallel resistance, not one or the other. If you have only one GPIO pin available for the push-buttons, it would be better to use more external circuitry to more correctly encode the state of all 4 push-buttons.
You need to read the value in and assign it to a variable ( first state) , then on the next read assign it to another ( next state). Then make a decision based on these values .
Suggest you get it working by just reading the values in to start with , to see if you need to debounce the signals . When this is working a more obvious solution may appear .
As above using digital inputs may give a better result and simpler code
I hate to see people using analog when the input is actually digital...
Every combination of buttons should give a different analog reading so you can make a table and your software can know what button(s) are pushed. Of course you can't count-on getting the exact reading every time so you'll have to look for a number that's "close".
And, your software already knows what LEDs are on.
The circuit shown can not detect simultaneous switches closed. For example if S5 is pressed input A0 is connected to ground regardless of the state of switches S6, S7, and S8.
MrMark:
The circuit shown can not detect simultaneous switches closed. For example if S5 is pressed input A0 is connected to ground regardless of the state of switches S6, S7, and S8.
You can't do an R/2R ladder with buttons, R/2R is constant impedance.
The closest equivalent is a set of R/2R/4R/8R resistors in series driven by a constant current source.
Each push button shorts across one of the resistors...
Could Hammy or someone else go further with the explanation in quotes?
You need to read the value in and assign it to a variable ( first state) , then on the next read assign it to another ( next state). Then make a decision based on these values .
MarkT:
You can't do an R/2R ladder with buttons, R/2R is constant impedance.
The Arduino reads voltage, not impedance. The R/2R ladder is a common D/A circuit, producing a different analog voltage for each combination of digital inputs. A button can act as a digital input. I think you are mistaken about the published circuit not working.
johnwasser:
The Arduino reads voltage, not impedance. The R/2R ladder is a common D/A circuit, producing a different analog voltage for each combination of digital inputs. A button can act as a digital input. I think you are mistaken about the published circuit not working.
While there is the theoretical possibility of it working, at some temperature, given enough ambient cooling to maintain the values within their respective ranges, it remains a very dirty way of doing it. It won't work over a larger range of temperature, and, as the number of switches increases, the precision required increases, to some point of infeasibility. If only one GPIO pin is available to receive the signal for multiple switches, it would be more reliable to use active circuitry to combine the inputs into a single signal.
Perehama:
While there is the theoretical possibility of it working, at some temperature, given enough ambient cooling to maintain the values within their respective ranges, it remains a very dirty way of doing it. It won't work over a larger range of temperature, and, as the number of switches increases, the precision required increases, to some point of infeasibility. If only one GPIO pin is available to receive the signal for multiple switches, it would be more reliable to use active circuitry to combine the inputs into a single signal.
Certainly if you wanted ten buttons (1024 combinations) or even eight buttons (256 combinations) the R-R2 resistor ladder would probably be impractical on a 10-bit A/D (like the Arduino analog inputs). The request is for four buttons, 16 values. That allows each range of 64 analog input values to cover one combination of buttons, allowing for about 6% error.
If the temperature coefficient of the resistors are similar, the ratio of resistances will remain similar over the temperature range. Use of 1% tolerance resistors would probably be a good idea. Since putting two resistors in parallel will give you 1/2R or in series will give you 2R it is easy to get the R and 2R value using a single value of resistor. We can get 1K 1% 1/4W through-hole resistors for ten cents each:
I can see you enjoy math. I agree with you 100%. I might do the resister network just for the fun of it. In some ways the availability of inexpensive ICs have made this kind of puzzling a lost art. That said, there is also merit in doing it in a way that can scale, is more universally applicable (works on any GPIO), and more modular.