Hello there !
I'm currently having a small issue :
i made a program, wich must be able to know the state of 4 switches ( like if they are on 0010 state, or 1110 state, etc...)
The problem is, i only have 2 ports free, the others being occupied by other things in my project.
Is there a way to have the arduino knowing wich state the switches are ?
I was thinking of using a current divider, but i don't really know how to use them, or how to make it in that specific case.
Thank you in advance for reading and answering
( Apologies for bad english, fellow frenchman's speaking )
Do you have any analog pins available? You can use an analog input with different resistors on each switch to identify the switches with different voltages.
Of course an analog input can always be used as a digital input.
So, if i do that, will the program be able to make the difference betwen all the 16 possibles differents states ? The currents from each switch will just add up ?
Technically a port is a group of 8, 16, or 32 pins sharing an address in the memory-map of the
microcontroller. You are talking about individual signal pins.
Thanks TomGeorge, it seems to be what i'm trying to do !
But i'm habing trouble making it... What resistors should i pick and how should i wire the whole thing ?
Update : I just realized my switches are not what i expected : They have 3 pins, but it's a open/closed only circuit, so it can't switch between being connected to the ground and being connected to Vref...
Can you help me again with this ?
D:
That does make it a bit harder but not impossible.
First option, but the one with a lot of components.... Do all values x10 in the current design (10k and 20k) or even x100 (100k and 200k) but then you need to add a capacitor between the analog in and GND. And instead of connecting the switch to GND parallel the switch with a pull down to GND of 1k. Because it's an order of magnitude different than the normal resistors it will act as a hard connection to GND but will not cause a problem when the switch forces that point HIGH.
Option two, drop the nice R2R. Just use a single resistor to GND and just some different values in series with the switches to Vcc (or the other way around if you want). That way every switch will give you a different voltage but they will not be spaced as nice across 0-Vcc as with the R2R. And every combination of two switches will give you a different voltage as well. So easier on the hardware, harder on the software.
septillion:
Option two, drop the nice R2R. Just use a single resistor to GND and just some different values in series with the switches to Vcc (or the other way around if you want). That way every switch will give you a different voltage but they will not be spaced as nice across 0-Vcc as with the R2R. And every combination of two switches will give you a different voltage as well. So easier on the hardware, harder on the software.
Do you think you can make a schematic of this please ? I don't really understand how to wire what you are explaining, but it sound like a good solution