Skip to content

Commit 5ee1f92

Browse files
committed
Update
- Added new `setSampleUs()` function to allow adjusting the sample period from 0-65535μs. - Some fixes and updates to examples and code.
1 parent 233d2f6 commit 5ee1f92

File tree

7 files changed

+35
-24
lines changed

7 files changed

+35
-24
lines changed

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,12 @@ sw1.setInputMode(sw1.inMode::input_input); // high impedance input
7474
sw1.setInputMode(sw1.inMode::input_pullup); // pullup resistor enabled (default)
7575
sw1.setInputMode(sw1.inMode::input_pulldown); // not used
7676
sw1.setInputMode(sw1.inMode::input_logic); // uses a byte variable for input data
77+
78+
void setSampleUs(uint16_t sampleUs);
79+
80+
// default sample period is 5000μs, range is 0-65535μs (0-65ms)
81+
// using 8 samples, the range for debounce will be (0-524ms)
82+
sw1.setSampleUs(5000); //μs
7783
```
7884
7985
#### Example Sketch:

examples/Toggle_Basic/Toggle_Basic.ino

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ const byte ledPin = LED_BUILTIN;
1414
byte ledState = LOW;
1515

1616
Toggle sw1(buttonPin);
17-
Toggle sw2(buttonPin + 1);
1817

1918
void setup() {
2019
pinMode(ledPin, OUTPUT);
@@ -24,11 +23,10 @@ void setup() {
2423

2524
void loop() {
2625
sw1.poll();
27-
sw2.poll();
28-
if (sw1.ONtoOFF()) Serial.println(F("sw1: ON⇒OFF"));
2926
if (sw1.OFFtoON()) {
3027
Serial.println(F("sw1: OFF⇒ON"));
3128
ledState = !ledState;
3229
digitalWrite(ledPin, ledState);
3330
}
31+
if (sw1.ONtoOFF()) Serial.println(F("sw1: ON⇒OFF"));
3432
}

keywords.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ isOFF KEYWORD2
3535
ONtoOFF KEYWORD2
3636
OFFtoON KEYWORD2
3737
setInputMode KEYWORD2
38-
getInputMode KEYWORD2
38+
setSampleUs KEYWORD2
3939

4040
##########################################
4141
# Constants (LITERAL1)

library.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "Toggle",
3-
"version": "2.2.0",
3+
"version": "2.2.1",
44
"description": "Arduino library for deglitching and debouncing signals, stored data, buttons and switches. Decodes transistions (depicting direction) for SPST, SPDT or SP3T contacts. Captures one-shot transitions and current status. Simple to use.",
55
"keywords": "debounce, toggle, button, switch, data, deglitch",
66
"repository":

library.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name=Toggle
2-
version=2.2.0
2+
version=2.2.1
33
author=David Lloyd
44
maintainer=David Lloyd <[email protected]>
55
sentence=Arduino library for deglitching and debouncing signals, stored data, buttons and switches. Decodes transistions (depicting direction) for SPST, SPDT or SP3T contacts.

src/Toggle.cpp

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/****************************************************
2-
Toggle Library for Arduino - Version 2.2.0
2+
Toggle Library for Arduino - Version 2.2.1
33
by dlloydev https://github.com/Dlloydev/Toggle
44
Licensed under the MIT License.
55
****************************************************/
@@ -14,6 +14,8 @@ Toggle::Toggle(uint8_t *logic) : _logic(logic) { }
1414
void Toggle::poll() {
1515
if (firstRun) {
1616
firstRun = false;
17+
lastUs = micros();
18+
statesOne |= 0b00010000; // lastOFF = 1
1719

1820
if (_pinA) {
1921
if (_inputMode == static_cast<uint8_t>(inMode::input_pullup)) pinMode(_pinA, INPUT_PULLUP);
@@ -27,9 +29,8 @@ void Toggle::poll() {
2729
}
2830
lastRegA = regA;
2931
lastRegB = regB;
30-
31-
if (millis() - sampleTime > 5) {
32-
sampleTime = millis();
32+
if (micros() - lastUs > _sampleUs) {
33+
lastUs += _sampleUs;
3334
if (_inputMode <= 2) {
3435
regA = regA << 1 | digitalRead(_pinA);
3536
if (_pinB) regB = regB << 1 | digitalRead(_pinB);
@@ -44,7 +45,7 @@ void Toggle::poll() {
4445
}
4546

4647
void Toggle::setStatesOne() {
47-
// when using one input b3:ONtoOFF, b2:OFFtoON, b1:isON, b0:isOFF;
48+
// when using one input b4:lastOFF, b3:ONtoOFF, b2:OFFtoON, b1:isON, b0:isOFF;
4849
if (lastRegA == 0xFF) {
4950
statesOne |= 0b00000001; // isOFF = 1
5051
statesOne &= 0b11111101; // isON = 0
@@ -54,10 +55,13 @@ void Toggle::setStatesOne() {
5455
statesOne &= 0b11111110; // isOFF = 0
5556
}
5657
statesOne &= 0b11111011; // OFFtoON = 0
57-
//if (lastRegA == 0xFF && regA == 0xFE) statesOne |= 0b00000100; // OFFtoON = 1
58-
if ((lastRegA & 1) == 0 && (regA & 3) == 0) statesOne |= 0b00000100; // OFFtoON = 1
58+
if ((statesOne & 0b00010000) && (statesOne & 0b00000001) == 0) statesOne |= 0b00000100; // OFFtoON = 1
59+
5960
statesOne &= 0b11110111; // ONtoOFF = 0
60-
if (lastRegA == 0x7F && regA == 0xFF) statesOne |= 0b00001000; // ONtoOFF = 1
61+
if ((statesOne & 0b00010000) == 0 && (statesOne & 0b00000001)) statesOne |= 0b00001000; // ONtoOFF = 1
62+
63+
if (statesOne & 1) statesOne |= 0b00010000; // lastOFF = 1
64+
else statesOne &= 0b11101111; // lastOFF = 0
6165
}
6266

6367
void Toggle::setStatesTwo() {
@@ -91,8 +95,8 @@ void Toggle::setInputMode(inMode inputMode) {
9195
_inputMode = static_cast<uint8_t>(inputMode);
9296
}
9397

94-
uint8_t Toggle::getInputMode() {
95-
return static_cast<uint8_t>(_inputMode);
98+
void Toggle::setSampleUs(uint16_t sampleUs) {
99+
_sampleUs = sampleUs;
96100
}
97101

98102
bool Toggle::isOFF() {

src/Toggle.h

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,20 +26,23 @@ class Toggle {
2626
bool DNtoMID();
2727
bool MIDtoUP();
2828
void setInputMode(inMode inputMode);
29-
uint8_t getInputMode();
29+
void setSampleUs(uint16_t sampleUs);
3030

31-
private:
31+
private:
3232
void setStatesOne();
3333
void setStatesTwo();
34-
float *_Input;
34+
35+
uint8_t _inputMode = static_cast<uint8_t>(inMode::input_pullup);
36+
uint8_t _pinA, _pinB;
3537
uint8_t *_logic;
36-
uint8_t statesOne, statesTwo;
37-
uint8_t regA = 0xFF, regB = 0xFF, lastRegA = 0xFF, lastRegB = 0xFF;
38-
uint32_t sampleTime;
38+
float *_Input;
39+
3940
bool firstRun = true;
4041
inMode inputMode = inMode::input_pullup;
41-
uint8_t _inputMode = static_cast<uint8_t>(inMode::input_pullup);
42-
uint8_t _pinA, _pinB;
42+
uint8_t regA = 0xFF, regB = 0xFF, lastRegA = 0xFF, lastRegB = 0xFF;
43+
uint8_t statesOne, statesTwo;
44+
uint16_t _sampleUs = 5000;
45+
uint32_t lastUs;
4346

4447
};
4548
#endif

0 commit comments

Comments
 (0)