In my project I need to free up one pin so I came up with the idea to connect 2 buttons with voltage dividers to one analog pin.
In the simulation it works as expected and I can read if any of them is pressed or both are pressed, is there a reason why not do it?
Should I expect problems with bouncing?
Hello
No.
Add a Serial.println(AnalogValue); delay(1000); to monitor the real values.
Have a nice day and enjoy coding in C++.
const uint16_t button_delay = 75;
void loop() {
current_time = millis();
// Button timing
if (current_time - button_prev_time >= button_delay) {
// Check Buttons
button_val = analogRead(button_pin);
button_prev_time += button_delay;
}
}
No issues, this is how I do it.
Also, your decision tree does not take into account what it eliminates as it goes. That is ,it makes unnecessary checks:
if (a > 900) {
// do something a > 900
} else if (a > 800) {
// do something 900 >= a > 800
} else if (a > 700) {
// do something 800 >= a > 700
}
You should reply with a copy of your code in a code block </> so others can see it too
This can work and some people wire-up 10 switches to a "resistor ladder voltage divider".
But personally, I hate to see people going digital-to-analog-to-digital when they really just want digital.
I hate it as well but this is my best solution in this specific project.
Just the same as using digital pins. You need to include a debounce code outside the decision tree.
It should be reasonably reliable for just two switches.
My experience is that the little "tact" switches are not always of good quality and many devices using this trick for multiple buttons such as computer monitors and MP3 players - as well as my car odometer which presumably just uses the button as a digital input anyway - fail over time due to moisture ingress in the button causing current leakage.
I think it's a brilliant idea! Good engineering is coming up with a reliable way to accomplish a task with a minimum of materials for lower overall cost. You done good!
In the real world it depends on the type and quality of your switches.
Its better to post your code properly, but I can see its just a "proof of concept" for this idea.
Whether this solution is appropriate for your project will depend on the parts we havent yet seen.
You havent even told us which arduino you plan to use, and maybe you would be better advised to use one that supports more pins?
Just make sure the signal is stable before decoding, ie get 2 or 3 reads in succession that agree well enough - otherwise you might be unlucky and catch the voltage mid-transition.
As per #9.
It is called "debouncing". For an analog signal, you either debounce the analog by taking successive readings and waiting for minimal variation, or feeding successive readings through your decision tree and requiring all to be the same result over a period of say, 5 milliseconds.
I favour the latter because the analog decision tree can be encapsulated in a function which is called in the same way as other digital reads, and because the decision is clearer rather than having a secondary (additional) analog criterion of just how much the reading is allowed to vary over successive reads (as it is likely to do, possibly more than just ±1).
I've never had a bounce issue with this setup, but this method of checking will remove the highest and lowest of 3 reads and leaves the center value in the button_val variable. This is the quickest method I can think of to eliminate 1 outlier that's higher or lower than the group.
uint16_t lowest;
uint16_t highest;
uint16_t button_val;
uint16_t button_val_sum;
const uint16_t button_delay = 75;
void loop() {
current_time = millis();
// Button timing
if (current_time - button_prev_time >= button_delay) {
// Check Buttons
lowest = 1024;
highest = 0;
button_val_sum = 0;
for (byte index = 0; index < 3; index++) {
button_val = analogRead(button_pin);
if (button_val < lowest) lowest = button_val;
if (button_val > highest) highest = button_val;
button_val_sum += button_val;
}
button_val_sum -= lowest + highest;
button_val = button_val_sum;
// button_val has had the lowest and highest of 3 reads removed
// call your decision tree for button_val here
button_prev_time += button_delay;
}
}
Untested
This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.