diff --git a/README.md b/README.md index fc35cbf..a8bfbfd 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/src/BfButtonManager.cpp b/src/BfButtonManager.cpp index c49ed64..27135ca 100644 --- a/src/BfButtonManager.cpp +++ b/src/BfButtonManager.cpp @@ -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(); @@ -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; } @@ -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; } diff --git a/src/BfButtonManager.h b/src/BfButtonManager.h index 4d9cfd7..78ba6ce 100644 --- a/src/BfButtonManager.h +++ b/src/BfButtonManager.h @@ -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. @@ -88,6 +94,8 @@ class BfButtonManager { #else uint16_t _adcResolution = 1024; #endif + + uint16_t _adcLowerBound = 100; unsigned long _lastLoop = 0; uint8_t _loopInterval = 20; @@ -105,4 +113,4 @@ class BfButtonManager { int8_t _readButton(); }; -#endif // BfButtonManager_h \ No newline at end of file +#endif // BfButtonManager_h