Arduino Helpers master
Utility library for Arduino
Blink-Frequency-Buttons.ino

Blink-Frequency-Buttons

This examples shows how to use two push buttons to set the frequency at which an LED blinks.

Boards:
AVR, AVR USB, Nano Every, Nano 33 IoT, Nano 33 BLE, UNO R4, Pi Pico, Due, Teensy 3.x, ESP8266, ESP32

Connections

The internal pull-up resistors will be enabled.

Behavior

Written by PieterP, 2019-12-10
https://github.com/tttapa/Arduino-Helpers

const unsigned long maxInterval = 2000; // ms
const unsigned long minInterval = 100; // ms
const unsigned long defaultInterval = 500; // ms
const int intervalDelta = 100; // ms
Timer<millis> timer = defaultInterval;
void setup() {
pinMode(LED_BUILTIN, OUTPUT);
buttons.begin();
Serial.begin(115200);
}
void loop() {
// toggle the LED when the given number of milliseconds have passed
if (timer)
digitalWrite(LED_BUILTIN, digitalRead(LED_BUILTIN) ? LOW : HIGH);
// read the buttons, and change interval accordingly
switch (buttons.update()) {
timer.setInterval(
AH::max(timer.getInterval() - intervalDelta, minInterval));
break;
timer.setInterval(
AH::min(timer.getInterval() + intervalDelta, maxInterval));
break;
timer.setInterval(defaultInterval);
break;
default: break;
}
// print the new interval if a button was pushed
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.
A class for buttons that increment and decrement some counter or setting.
@ DecrementLong
The counter must be decremented (after long press).
@ Nothing
The counter should not be incremented.
@ IncrementShort
The counter must be incremented (after short press).
@ IncrementHold
The counter must be incremented (still pressed).
@ DecrementHold
The counter must be decremented (still pressed).
@ DecrementShort
The counter must be decremented (after short press).
@ Reset
The counter should be reset to the initial value.
@ IncrementLong
The counter must be incremented (after long press).
A class for easily managing timed events.
unsigned long getInterval() const
Get the interval of the timer.
void setInterval(unsigned long interval)
Set the interval of the timer.
void pinMode(pin_t pin, PinMode_t mode)
An ExtIO version of the Arduino function.
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.
Definition: MinMaxFix.hpp:12
constexpr auto max(const T &a, const U &b) -> decltype(a< b ? b :a)
Return the larger of two numbers/objects.
Definition: MinMaxFix.hpp:19