Skip to content

Commit c8c39a6

Browse files
committed
Update
Update Eight_buttons.ino to demonstrate blink(ms) function. The ms timer now stops at 60 seconds (upper limit).
1 parent 73c5424 commit c8c39a6

2 files changed

Lines changed: 12 additions & 6 deletions

File tree

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
1-
/***********************************************************
1+
/******************************************************
22
Eight Buttons (or Switches) Example:
33
====================================
44
An example that checks the status of eight buttons.
55
All input pins will have their pullups enabled.
6-
The LED toggles for each on press transition.
7-
**********************************************************/
6+
The LED blinks for each on press transition.
7+
*****************************************************/
88

99
#include <Toggle.h>
1010

1111
const byte num = 8; // number of buttons
1212
const byte pin[num] = {2, 3, 4, 5, 6, 7, 8, 9}; //button pins
1313
const byte ledPin = 13;
14-
byte ledState = LOW;
14+
const unsigned int blinkMs = 100; // led blink duration (ms)
1515

1616
Toggle *sw = new Toggle[num];
1717

@@ -23,10 +23,14 @@ void setup() {
2323
}
2424

2525
void loop() {
26-
26+
byte ledState = num;
2727
for (int i = 0; i < num; i++) {
2828
sw[i].poll();
29-
if (sw[i].onPress()) ledState = !ledState;
29+
if (sw[i].blink(blinkMs)) {
30+
ledState++;
31+
} else {
32+
ledState--;
33+
}
3034
}
3135
digitalWrite(ledPin, ledState);
3236
}

src/Toggle.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,7 @@ void Toggle::clearTimer() {
141141
}
142142

143143
uint16_t Toggle::getElapsedMs() {
144+
if ((sampleCount * (_samplePeriodUs >> 10)) > 60000) sampleCount--;
144145
return sampleCount * (_samplePeriodUs >> 10);
145146
}
146147

@@ -165,6 +166,7 @@ bool Toggle::releasedFor(uint16_t ms) {
165166
}
166167

167168
bool Toggle::retrigger(uint16_t ms) {
169+
if (getTimerMode()) setTimerMode(0); // start onPress
168170
if (isPressed() && getElapsedMs() > ms) {
169171
clearTimer();
170172
return true;

0 commit comments

Comments
 (0)