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
- 2: Momentary push button (other pin to ground)
- 3: Momentary push button (other pin to ground)
The internal pull-up resistors will be enabled.
Behavior
- If you press the first push button, the LED blinks faster.
- If you press the second push button, the LED blinks slower.
- You can press and hold one of the push buttons to change the frequency by multiple steps.
- If you press both buttons at the same time, the frequency is reset to the initial default frequency.
Written by PieterP, 2019-12-10
https://github.com/tttapa/Arduino-Helpers
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::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;
}
if (buttons.getState() != IncrementDecrementButtons::Nothing)
Serial.println(timer.getInterval());
}