Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@ Powerful button tools for managing various button events of standalone button or

`BfButton` class handled standalone button debouncing, trigger callback function for single press, double press, and long press events. The class can distinguish different pressing pattern and trigger corresponding callback, i.e. single press vs double press.

`BfButtonManager` class manage multiple buttons with single analog pin. The class also provide `printReading()` method for you to check the analog pin reading. In theory you may add more buttons in the circuit.
`BfButtonManager` class manage multiple buttons with single analog pin. The class also provide `printReading()` method for you to check the analog pin reading. In theory you may add more buttons in the circuit.

**This fork exists only to modify the BfButtonManager class, adding support for ADC buttons that pull down to 0.**
A pull request has been submitted to merge this back to the parent project, but no response from the original author (mickey9801) yet.

## Installation

Expand Down
12 changes: 9 additions & 3 deletions src/BfButtonManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,12 @@ BfButtonManager& BfButtonManager::setADCResolution(uint16_t resolution) {
this->_adcResolution = resolution;
return *this;
}

BfButtonManager& BfButtonManager::setADCBounds(uint16_t lowerBound, uint16_t upperBound) {
this->_adcLowerBound = lowerBound;
this->_adcResolution = upperBound;
return *this;
}

BfButtonManager& BfButtonManager::addButton(BfButton* btn, uint16_t minVoltageReading, uint16_t maxVoltageReading) {
uint8_t _b = btn->getID();
Expand Down Expand Up @@ -76,7 +82,7 @@ void BfButtonManager::loop() {
uint16_t BfButtonManager::printReading(uint8_t pin) {
int z;
z = analogRead(pin);
if (z > 100) Serial.println(z);
if (z >= this->_adcLowerBound) Serial.println(z);
return z;
}

Expand All @@ -102,9 +108,9 @@ int8_t BfButtonManager::_readButton() {
}
z = _sum / 4;

if (z >= 100 || z < this->_adcResolution) {
if (z >= this->_adcLowerBound || z <= this->_adcResolution) {
for (int8_t _b = 0; _b < this->_buttonCount; _b++) {
if (z > this->_btnVoltageLowerBounds[_b] && z < this->_btnVoltageUpperBounds[_b]) {
if (z >= this->_btnVoltageLowerBounds[_b] && z <= this->_btnVoltageUpperBounds[_b]) {
_button = _b;
break;
}
Expand Down
10 changes: 9 additions & 1 deletion src/BfButtonManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,12 @@ class BfButtonManager {
* The library will set build-in ADC for Arduino and ESP32 by default.
*/
BfButtonManager& setADCResolution(uint16_t resolution);

/*
* Set the lower & upper bounds to define the acceptable range for ADC buttons (upperBound = resolution)
* defaults: lowerBound = 50, upperBound = 1024 (4096 for ESP32)
*/
BfButtonManager& setADCBounds(uint16_t lowerBound, uint16_t upperBound);

/*
* Add button to button array.
Expand Down Expand Up @@ -88,6 +94,8 @@ class BfButtonManager {
#else
uint16_t _adcResolution = 1024;
#endif

uint16_t _adcLowerBound = 100;

unsigned long _lastLoop = 0;
uint8_t _loopInterval = 20;
Expand All @@ -105,4 +113,4 @@ class BfButtonManager {
int8_t _readButton();
};

#endif // BfButtonManager_h
#endif // BfButtonManager_h