Control Surface  1.1.1
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 33, 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));
// read the buttons, and change interval accordingly
switch (buttons.update()) {
case IncrementDecrementButtons::Increment:
timer.setInterval(max(timer.getInterval() - intervalDelta, minInterval));
break;
case IncrementDecrementButtons::Decrement:
timer.setInterval(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());
}
AH::ExtIO::pinMode
void pinMode(pin_t pin, uint8_t mode)
An ExtIO version of the Arduino function.
Definition: ExtendedInputOutput.cpp:36
MillisMicrosTimer.hpp
AH::ExtIO::digitalRead
int digitalRead(pin_t pin)
An ExtIO version of the Arduino function.
Definition: ExtendedInputOutput.cpp:60
AH::min
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
Arduino_Helpers.h
Dummy header file for Arduino builder. You have to add this file first, so the other headers are in t...
OUTPUT
const uint8_t OUTPUT
Definition: ExtendedInputOutput.hpp:50
AH::ExtIO::digitalWrite
void digitalWrite(pin_t pin, uint8_t val)
An ExtIO version of the Arduino function.
Definition: ExtendedInputOutput.cpp:48
AH::max
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
IncrementDecrementButtons.hpp