As the title implies, I am trying to use multiple buttons on 1 analog pin to conserve i/o pins on my arduino. I tried to make an example in Fritzing but confused myself, so I took a detailed picture that I will try to explain.
I bought a 4 button membrane from adafruit, and am using it to control my arduino project. I want to hook all 4 buttons to 1 analog pin, using resistors. I'm under the impression that I can affect the value when I read the analog input to determine which button it is. My current set up seems to work, but not as intended, but I can't figure out exactly what I did wrong. I'm almost confident that it has to do with how I wired it up.
This picture shows my set up (only be concerned with rows 1-5). Row 1 is the common ground for all 4 buttons, The 3 vertical resistors are 2.2k and used to change the analog value. The horizontal resistor is a 2.2k acting as a pull up (at least that's what i want it to be). The yellow jumper, which is plugged in row 5, goes to A5 on the Arduino.
Here is the output of reading the analog pin when I press the buttons. ~671 is the no button reading, ~445 is button one, ~501 is button two, ~3 is button three, and ~333 is button four. While I could probably use these reading to make this work, I feel like its supposed to be between 0 and 1023.
Is what I am doing correct, or do I need to adjust something?
(p.s. if this is the wrong section, sorry ahead of time, was debating between here and 'General Electronics')
I feel like its supposed to be between 0 and 1023.
It is, as far as I can see.
[/quote]More specifically, I thought that the no button value should be closer to 1023. When I get 3, it seems wrong but I am not sure why. It doesnt seem to match a pattern with the rest of the values as I would expect.
[quote author=Nick Gammon link=topic=186157.msg1378512#msg1378512 date=1378263177]
If you press row 5 which grounds it, you would expect around 0V wouldn't you?
[/quote]Ah, I guess that does make sense. I guess my last question would be, do I need a pull up resistor when I am using an analog pin?
Alright Nick, I am confused now. I have the exact same setup, and I am trying to apply the new system to my previous code (where all the buttons were digital pins). Why would the values of analogRead(A5) be different now? If I upload my previous test sketch, they change back to the original values...
Don't know for sure. Get your meter out and read the voltage on A5 with each switch pressed and see if it is what you expect. Also check the 5V pin. Maybe the screen is drawing a lot of current.
I am using this exact same system, you never get 1023, unless you have a resistor directly connected between the 5 volt rail and your analog input, and no other resistor connected to earth.
You want a voltage divider system with your buttons earthing it at different points.
I am using 1k resistors in my system, with 5 buttons.
Shows you the resistor and button system on the freetronics shield, which does return 1023 until you push a button.
I am using 1k resistors in my system, with 5 buttons, i just extended the resistor chain a little bit.
Here is my code, ignore the Ktimer part, as its part of the automatic repeat/delay system.
ButtonWindow is defined to be 10, as the analog values can jump around a little bit, so you need to have a bit of leeway for noise.
OldButton=Button;
Keypress=analogRead(1);
if (Keypress>1024-ButtonWindow){
Button = 0; // nothing pressed, all keys released.
// reset the keypress timer
Ktimer=0;
Kthresh=Krepeat; //reset it
Kaccel=Kthresh/2;
}
if ((Keypress>=768-ButtonWindow)&&(Keypress<=768+ButtonWindow)) {
Button=K_R;
Ktimer=Ktimer+1;
}
if ((Keypress>=683-ButtonWindow)&&(Keypress<=683+ButtonWindow)) {
Button=K_U;
Ktimer=Ktimer+1;
}
if ((Keypress>=819-ButtonWindow)&&(Keypress<=819+ButtonWindow)) {
Button=K_D;
Ktimer=Ktimer+1;
}
if ((Keypress>=511-ButtonWindow)&&(Keypress<=511+ButtonWindow)) {
Button=K_L;
Ktimer=Ktimer+1;
}
if (Keypress<=ButtonWindow) {
Button=K_S;
Ktimer=Ktimer+1;
}
Jeebis, can you draw a circuit diagram?
Your values shouldnt change just because of a change in the sketch, unless you are referencing the ADC to something else.
Sorry for it being a little cluttered, but this is my current breadboard set up. The four buttons represent the 4 button membrane I bought form adafruit, as there was no object for that. The screen is a QC1602A and the trim pot is a small 10k one.
[quote author=Nick Gammon link=topic=186157.msg1378580#msg1378580 date=1378270389]
Don't know for sure. Get your meter out and read the voltage on A5 with each switch pressed and see if it is what you expect. Also check the 5V pin. Maybe the screen is drawing a lot of current.
[/quote]So I just measured the voltages and noticed something weird... When I check the voltage from ground to the 3.3v pin, I get an expected ~3.26v, but when I check on the positive side of my breadboard, I have ~4.1v... When I push the small test sketch I was using to find the analog values, it goes back to ~3.26v. So the values are changing because the voltage reference is changing. Any idea what would cause this to happen?
[quote author=Nick Gammon link=topic=186157.msg1379584#msg1379584 date=1378328648]
I don't see anything connecting that bottom red rail to 5V power.
[/quote]Whoops, looks like I neglected that wire on the diagram. Both sets of rails are connected on my project.