This examples shows how to use two push buttons to set the frequency at which an LED blinks.
The internal pull-up resistors will be enabled.
const unsigned long maxInterval = 2000;
const unsigned long minInterval = 100;
const unsigned long defaultInterval = 500;
const int intervalDelta = 100;
IncrementDecrementButtons buttons {2, 3};
Timer<millis> timer = defaultInterval;
void setup() {
buttons.begin();
Serial.begin(115200);
}
void loop() {
if (timer)
switch (buttons.update()) {
case IncrementDecrementButtons::IncrementShort:
case IncrementDecrementButtons::IncrementLong:
case IncrementDecrementButtons::IncrementHold:
timer.setInterval(
AH::max(timer.getInterval() - intervalDelta, minInterval));
break;
case IncrementDecrementButtons::DecrementShort:
case IncrementDecrementButtons::DecrementLong:
case IncrementDecrementButtons::DecrementHold:
timer.setInterval(
AH::min(timer.getInterval() + intervalDelta, maxInterval));
break;
case IncrementDecrementButtons::Reset:
timer.setInterval(defaultInterval);
break;
default: break;
}
if (buttons.getState() != IncrementDecrementButtons::Nothing)
Serial.println(timer.getInterval());
}
constexpr PinStatus_t LOW
constexpr PinStatus_t HIGH
constexpr PinMode_t OUTPUT
Dummy header file for Arduino builder.
PinStatus_t digitalRead(pin_t pin)
An ExtIO version of the Arduino function.
void digitalWrite(pin_t pin, PinStatus_t val)
An ExtIO version of the Arduino function.
constexpr auto min(const T &a, const U &b) -> decltype(b< a ? b :a)
Return the smaller of two numbers/objects.
constexpr auto max(const T &a, const U &b) -> decltype(a< b ? b :a)
Return the larger of two numbers/objects.