Frequently Asked Questions - rlogiacco/AnalogButtons GitHub Wiki

The following is a list of frequently asked questions on the AnalogButtons library usage.

How can I make the library react faster to button clicks?

How fast the library reacts depends on the debounce counter and how frequently you call the check() function. In the very likely hypothesis you are calling check() once per loop:

  • if your loop() function gets executed once every 100ms and you have debounce set to 3 you need to hold the button for 300ms before it gets activated (that's a little slow reaction time)
  • if you loop faster than 50 times per second (once every 20ms) and have debounce set to 5, it will trigger after 100ms (this is because I limit the check frequency to 50Hz)
  • if a loop takes 50ms (20Hz) and you have 4 as debounce, it will trigger after 200ms

Please consider human reaction time is around 120-180ms, so going faster than that is probably useless.

Reducing too much the debounce parameter is probably going to fire unwanted clicks.

How can I check multiple buttons on multiple pins?

Multiple pins can be controlled by just defining multiple AnalogButtons instances, one per analog pin. As usual, you will have to define a Button instance for each button you desire to control and associate to the corresponding actions.

During the firmware setup() you will assign each Button to the AnalogButtons instance corresponding to the pin the button is attached to.

// Maps to pin A0
AnalogButtons a0(A0, 30);
// Maps to pin A1
AnalogButtons a1(A1, 30);

// Buttons
Button b1 = Button(1013, &b1Click);
Button b2 = Button(1002, &b2Click, &b2Hold);
Button b3 = Button(970, &b3Click, &b3Hold);
....
Button b11 = Button(1013, &b11Click);
Button b12 = Button(1002, &b12Click, &b12Hold);
Button b13 = Button(970, &b13Click, &b13Hold);
....

void setup() {
  // Binds buttons b1 to b10 to A0
  a0.add(b1);
  a0.add(b2);
  a0.add(b3);
  ....

  // Binds buttons b11 to b20 to A1
  a1.add(b11);
  a1.add(b12);
  a1.add(b13);
  ....
}

The downside for this is you will have to call the check() function for each of the AnalogButtons you have defined.

void loop() {
  a0.check();
  a1.check();
}