So i have an idea for reading two buttons with only one analog input pin by using different resistors, but I don't know how to put it together. Currently i have it set up like this:
But I cant read different inputs.
see comments, as I can't upload two images at the same time
Would it work better if I set up like this, or would that break intirely?
The code i'm using currently is a simple one, as it's only for testing:
int buttons = A1;
// One button has a resistor of 220 ohm, the other has 10 kOhm
// This should enable the detection of two seperate buttons with only one analog input pin.
int button01State;
void setup() {
Serial.begin(9600);
Serial.println("RESET");
}
void loop() {
button01State = analogRead(buttons[0]);
if(button01State > 0) {
Serial.println(button01State);
}
}
It only prints close to 1023 when any button is pressed, no difference between the two.
Your second diagram is better but still puts 5V on the input when either or both of the switches are pressed and leaves the input floating if neither is pressed.
Add a 10k resistor from A1 to Ground. That will act a part of a voltage divider.
When no button is pressed: 0V
When the top button is pressed: 2.5V (10k/(10K+10K))
When the bottom button is pressed: 4.89V (10K/10.220k)
if both switches are open, it will read +- 503,
If the top switch is closed, it will read +- 240,
If the switch below is closed, it will read +- 340 and
if both are closed, it will read 0.