Control Surface master
MIDI Control Surface 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, 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
IncrementDecrementButtons buttons {2, 3};
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()) {
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;
}
// 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.
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:15
constexpr auto max(const T &a, const U &b) -> decltype(a< b ? b :a)
Return the larger of two numbers/objects.
Definition: MinMaxFix.hpp:22