Issue with using multiple instances
Hello,
I'm not sure if this is still being maintained since it looks like it's been a while since it's been touched.
I'm having an issue using when using this library with multiple instances of the class AnalogMultiButton. The way my circuit is set up, I've grouped buttons in groups of 3-5 in order to keep everything modular but still minimize pin usage. If I set this up for one set of buttons and declare one instance similarly to the example, it works perfect.
Now, if I create multiple instances of the class, eg:
AnalogMultiButton xyButton = AnalogMultiButton(XY_AXIS_BUT,XY_But_Total,setptxy,80);
AnalogMultiButton zButton = AnalogMultiButton(Z_AXIS_BUT,Z_But_Total,setpt3,80);
AnalogMultiButton setButton = AnalogMultiButton(SET_AXIS_BUT,Set_But_Total,setpt3,80);
AnalogMultiButton miscButton = AnalogMultiButton(MISC_BUT,Misc_But_Total,setpt3,80);
And then in my loop(), update all four sets:
xyButton.update();
zButton.update();
setButton.update();
miscButton.update();
And setup if statements below for each one as shown in the example...
When pressing one of the buttons, it will perform the action assigned to all buttons with a matching voltage rating, regardless of pin. For example, if i have a circuit with 3 buttons on it attached only to A3 (with code setup to differing actions for A0,A1, and A2 but those pins not connected). And I push button (2) of 3, it will properly perform the button (2) action. However it will also perform the button (2) action for the A0, A1, A2 pins even though they are not being triggered.
Now, if I turn off all the other updates and only .update() one pin, it will work correctly and I can switch which pin I update and maintain proper function. So there is only disfunction if i .update() more than one pin (which is obviously required if I'm going to have more than one set of buttons). It's as if when another instance of the class is updated, the state variables within the class are getting shared/overwritten, causing downstream problems.
I should add, that while my code is fairly more complex, the simple sketch below will reproduce the issue:
#include <AnalogMultiButton.h>
int vals[6] = {0,167,338,510,680,850};
#define pin1 A0
#define pin2 A1
AnalogMultiButton button1(pin1,6,vals,80);
AnalogMultiButton button2(pin2,6,vals,80);
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
Serial.println("initialize");
}
void loop() {
// put your main code here, to run repeatedly:
button1.update();
button2.update();
if (button1.onRelease(2)){
Serial.println("Button1 #2 pressed");
}
if (button2.onRelease(2)){
Serial.println("Button2 #2 pressed");
}
}