Skip to content

Commit 4f1172a

Browse files
committed
Bug Fix/Doc Update: Spurious 'pinMode' spoil the work of the lib, update of the readme and code comments.
1 parent 6e211ac commit 4f1172a

File tree

4 files changed

+47
-12
lines changed

4 files changed

+47
-12
lines changed

README.md

+17-2
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@ Acctually we have the following features implemented:
88

99
* Up to 8 buttons in a single Analog pin _(by default limited to 8 to keep firmware footprint low, it's tunable in software)_.
1010
* Secondary functions (press & hold) in each button, configured on demand.
11-
* Intuitive behavior in the case you held a button pressed (_on the original AnalogButtons library when you held a button you got two events, first a click and then a hold one, that's not acceptable for some uses._)
11+
* Intuitive behavior in the case you held a button pressed (_on the original AnalogButtons library when you held a button you got two events, first a click and then a hold one, that's not intuitive._)
12+
* Single click event (the click does not repeat itself)
13+
* Hold event will repeat at a pace defined by the hold limit (if you keep the button pressed long enough)
1214

1315
## Inspiration ##
1416

@@ -21,7 +23,20 @@ This library is based in the following source codes:
2123

2224
The first thing to do is determine how many buttons you will manage in the analog pin. By default max number of buttons per pin is limited to 8 to limit memory consumption (you can declare less or more buttons as your need commands), it can be controlled defining the `BUTTONS_COUNT` macro **before** including this library.
2325

24-
You then create an instance of the lib to be configured and user later in our code; like this:
26+
It's time to play with your hardware, to setup a resistor network you have two main variants:
27+
28+
* As a fixed resistors stair values
29+
* As a multiple resistors values
30+
31+
The two main configuration can be seen on the file Resistors_variants.png in the project folder.
32+
33+
**Note:** _In theory you can declare up to ~21 buttons with a +/- 20 units of tolerance and a guard zone of 10 units_
34+
35+
_That means that you have to setup your resistors in a way that the specific central values are at least 3 units of tolerance away from each other, if not you will have problems_
36+
37+
_For example if your resistor network yields a values of 256, 270 and 350 and you use the default 10 units of tolerance the first two will make you in trouble as 270 - 256 = 14; as you can see 14 is less than the recommended 30 units (3 * 10)_
38+
39+
Now you create an instance of the lib at the beginning of your code; like this:
2540

2641
```
2742
BMux abm;

examples/AnalogButtonMuxer/AnalogButtonMuxer.ino

+3-3
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434

3535
#include <BMux.h>
3636

37-
#define ANALOG_PIN A1
37+
#define ANALOG_PIN A2
3838

3939
// First define the callback functions for the buttons action handling,
4040
// in this example just a Serial notification
@@ -86,7 +86,7 @@ Button b5 = Button(860, &b5Click, &b5Hold, 5000);
8686

8787
void setup() {
8888
Serial.begin(9600);
89-
Serial.println("Testing your Analog buttons 2");
89+
Serial.println("Testing your Analog Button Muxer Library");
9090

9191
abm.init(ANALOG_PIN, 5, 20);
9292
abm.add(b1);
@@ -103,7 +103,7 @@ void loop() {
103103
// To configure the MAX/Min values for each button: uncomment the following
104104
// line and attach a serial terminal to see the data and note down the values
105105

106-
//configure();
106+
configure();
107107
}
108108

109109
void configure() {

src/BMux.cpp

+19-5
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,6 @@ void BMux::init(uint8_t pin, uint8_t debounce, uint8_t margin) {
4848
this->pin = pin;
4949
this->debounce = debounce;
5050
this->margin = margin;
51-
pinMode(pin, INPUT);
5251
}
5352

5453
void BMux::add(Button button) {
@@ -58,21 +57,36 @@ void BMux::add(Button button) {
5857
}
5958

6059
void BMux::check() {
61-
// In case this function gets called very frequently avoid sampling the analog pin too often: max frequency is 50Hz
62-
if (millis() - time > 20) {
60+
// In case this function gets called very frequently avoid sampling the
61+
// analog pin too often: default (max) frequency is 50Hz
62+
if (millis() - time > 1000 / BMUX_SAMPLING) {
6363
time = millis();
6464
uint16_t reading = analogRead(pin);
6565
uint16_t msec = 0;
66+
uint16_t min = 0;
67+
uint16_t max = 0;
6668

6769
// we changed the strategy here, all events will be emitted on button release
6870
for (uint8_t i = 0; i < buttonsCount; i++) {
69-
if (reading >= buttons[i].value - margin && reading <= buttons[i].value + margin) {
71+
// low limit checking (this led to overflows in the past)
72+
if (buttons[i].value <= margin) {
73+
min = 0;
74+
} else {
75+
min = buttons[i].value - margin;
76+
}
77+
78+
// max limit check
79+
max = buttons[i].value + margin;
80+
if (max > 1023) max = 1023;
81+
82+
// checking
83+
if (reading >= min && reading <= max) {
7084
// just increment the count of the pressed buttons
7185
buttons[i].counter += 1;
7286

7387
// check for button held & still pressed
7488
msec = buttons[i].counter;
75-
msec *= 20;
89+
msec *= 1000 / BMUX_SAMPLING;
7690
if (msec >= buttons[i].duration) {
7791
buttons[i].held();
7892
buttons[i].counter = 0;

src/BMux.h

+8-2
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@
4949

5050

5151
// Take into account that by default we use a sampling interval of 50Hz
52+
// but you can change that setting by declaring a external var before
53+
// loading the lib in your code, BMUX_SAMPLING is the var
5254

5355
#include "Arduino.h"
5456

@@ -59,11 +61,15 @@
5961
#define BUTTONS_COUNT 8
6062
#endif
6163

64+
#ifndef BMUX_SAMPLING
65+
#define BMUX_SAMPLING 50 // how many times per second will be sampled
66+
#endif
67+
6268
class Button {
6369
public:
6470
uint16_t value; // to hold a max of 1023
6571
uint16_t duration; // in msecs, max is 65.5 seconds
66-
uint8_t counter; // in sample intervals of 50Hz (20 msecs)
72+
uint8_t counter; // in sample intervals of BMUX_SAMPLING
6773
boolean isHeld; // flag to sign that the button is held
6874

6975
Button() {};
@@ -87,7 +93,7 @@ class Button {
8793

8894
class BMux {
8995
private:
90-
uint8_t debounce; // in sample intervals
96+
uint8_t debounce; // in sample intervals of BMUX_SAMPLING
9197
uint32_t time; // time stamp to define the sampling interval
9298
uint8_t margin; // max is 255, which is bigger than we need
9399
uint8_t pin; // the analog pin

0 commit comments

Comments
 (0)