Skip to content

Commit

Permalink
added hold method
Browse files Browse the repository at this point in the history
  • Loading branch information
serek4 committed Dec 10, 2017
1 parent 9812672 commit 7f3265e
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 1 deletion.
8 changes: 8 additions & 0 deletions examples/demo/demo.ino
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,14 @@ void loop() {
digitalWrite(LED2, led2State);
}
break;
case 4:
if (button2.hold()) {
led2State = true;
} else {
led2State = false;
}
digitalWrite(LED2, led2State);
break;
default:
mode = 0;
break;
Expand Down
1 change: 1 addition & 0 deletions keywords.txt
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ press KEYWORD2
repeat KEYWORD2
longPress KEYWORD2
release KEYWORD2
hold KEYWORD2

#######################################
# (KEYWORD3)
Expand Down
2 changes: 1 addition & 1 deletion library.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name=debounced button
version=1.1.0
version=1.2.0
author=serek4
maintainer=serek4
sentence=buttons library with debounce
Expand Down
26 changes: 26 additions & 0 deletions src/debouncedButton.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -104,3 +104,29 @@ boolean button::relese() {
}
return _press;
}

boolean button::hold(int _pressDelay) {
_press = false;
if (!_buttonPressed && digitalRead(_pin) == LOW) { // press
_buttonPressed = _debounceTimer;
_pressTime = millis();
#if DEBUG
Serial.print(_pin);
Serial.print(" hold start: ");
Serial.println(_pressTime);
#endif
} else if (_buttonPressed && millis() - _pressTime > _pressDelay && digitalRead(_pin) == LOW) { // hold
_press = true;
} else if (_buttonPressed && digitalRead(_pin) == HIGH) { // debounce
_buttonPressed++;
#if DEBUG
if (!_buttonPressed) {
_releaseTime = millis();
Serial.print(_pin);
Serial.print(" hold stop: ");
Serial.println(_releaseTime);
}
#endif
}
return _press;
}
5 changes: 5 additions & 0 deletions src/debouncedButton.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,11 @@ class button {
* button activated on relese
*/
boolean relese();
/**
* button active as long as it is pressed
* @param _pressDelay: hold time [in milliseconds], default = 0
*/
boolean hold(int _pressDelay = 0);

private:
int _pin;
Expand Down

0 comments on commit 7f3265e

Please sign in to comment.