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
16 changes: 16 additions & 0 deletions src/Bounce2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ Bounce::Bounce()
, interval_millis(10)
, state(0)
, pin(0)
, analog(false)
{}

void Bounce::attach(int pin) {
Expand All @@ -28,6 +29,21 @@ void Bounce::attach(int pin) {
#endif
}

void Bounce::attachAnalog(int pin, uint16_t r_threshold) {
this->pin = pin;
state = 0;
analog = true;
threshold = r_threshold;
if (readCurrentState()) {
setStateFlag(DEBOUNCED_STATE | UNSTABLE_STATE);
}
#ifdef BOUNCE_LOCK_OUT
previous_millis = 0;
#else
previous_millis = millis();
#endif
}

void Bounce::attach(int pin, int mode){
setPinMode(pin, mode);
this->attach(pin);
Expand Down
13 changes: 11 additions & 2 deletions src/Bounce2.h
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ class Bounce
Attach to a pin for advanced users. Only attach the pin this way once you have previously set it up. Otherwise use attach(int pin, int mode).
*/
void attach(int pin);

void attachAnalog(int pin, uint16_t r_threshold);

/**
@brief Sets the debounce interval in milliseconds.
Expand Down Expand Up @@ -177,9 +177,18 @@ class Bounce
uint16_t interval_millis;
uint8_t state;
uint8_t pin;

bool analog;
int16_t threshold;

unsigned long stateChangeLastTime;
unsigned long durationOfPreviousState;
virtual bool readCurrentState() { return digitalRead(pin); }
virtual bool readCurrentState() {
if ( !analog ) {
return digitalRead(pin);
}
return ( analogRead(pin) > threshold );
}
virtual void setPinMode(int pin, int mode) {
#if defined(ARDUINO_STM_NUCLEO_F103RB) || defined(ARDUINO_GENERIC_STM32F103C)
pinMode(pin, (WiringPinMode)mode);
Expand Down