Using MIDI library, button debounce and SoftTimer library coexistance

Coming back to Arduino development after several (6-7) years. Trying to make a MIDI footswitch pedal board using an Arduino UNO clone (CH340 based).

From my past brush with developing some non-trivial Arduino project, I remember having some hard time using the standard arduino libraries (ones that are usually expected to be invoked from a synchronous, loop() based program) since some of the libraries made conflicting usage of the AVR Timer function. Eventually, I had to rewrite some of the libraries and directly handle interrupts.

For the MIDI footswitch, I need to be able to read 6 SPST momentary buttons, debounce them, then use the MIDI library to send MIDI events over the serial line to PC, and update the flashing pattern of 4 LEDs etc. Wondering without looking into the library code, if the standard MIDI library, will play well with SoftTimer library, fast-debounce library etc. ?

TIA.

falcon74:
From my past brush with developing some non-trivial Arduino project, I remember having some hard time using the standard arduino libraries (ones that are usually expected to be invoked from a synchronous, loop() based program) since some of the libraries made conflicting usage of the AVR Timer function. Eventually, I had to rewrite some of the libraries and directly handle interrupts.

I don't see how any of that is relevant to a simple MIDI footswitch. Using interrupts for these kinds of things are the wrong approach.

Debouncing the momentary buttons and sending MIDI is trivial using the Control Surface library I'm working on.
Example with push button that sends MIDI notes
It does MIDI input as well: Example that turns on/off LED when it receives MIDI notes

It handles debouncing internally, and doesn't block, so it keeps loop free for you to do other things.

Pieter

Thanks for the reply @PieterP. I took a quick look at your project on Github, and it is very interesting (and relevant). However, from a quick look at the example it seems that you invoke your library's loop function from the Arduino main loop. Doesn't that leave a possibility of starvation for either your library's loop or the rest of the logic in main loop(), i.e. if either of them do something unexpected, s.a. spend more time than they should.

OTOH, the sequential approach is easier to wrap ones head around than an asynchronous event-base model. I suppose, your library is not usable with Arduinos without native MIDI over USB capability (like the UNO R3 clones, or Micro / Micro Pro clones with FTDI or CH340 chips) right ?

cheers.

falcon74:
Doesn't that leave a possibility of starvation for either your library's loop or the rest of the logic in main loop(), i.e. if either of them do something unexpected, s.a. spend more time than they should.

OTOH, the sequential approach is easier to wrap ones head around than an asynchronous event-base model.

I think you have some misconceptions about the event-based model. There is no true asynchronous programming on an Arduino, everything runs sequentially, there's no notion of multiple threads.

The SoftTimer library, for example, replaces the "loop" with its own event loop. (arduino-softtimer/SoftTimer.cpp at 6cd57f739d9a7be7db8f46a128b7178df7064d54 · prampec/arduino-softtimer · GitHub)
The same limitations apply: if one of the events takes too much time, that'll ruin the timing of other events.

You could argue that interrupts are truly asynchronous, but the problem persists: if your interrupt handler takes too much time to execute, everything else stalls (the main loop and other interrupts).

As long as your code doesn't use any blocking functions like delay, there shouldn't be any problems calling Control_Surface.loop() frequently enough in your loop.
Control_Surface.loop itself will not block, it takes exactly the time necessary to update its inputs and refresh its outputs. There is no way to magically speed that up using asynchronous techniques.

falcon74:
I suppose, your library is not usable with Arduinos without native MIDI over USB capability (like the UNO R3 clones, or Micro / Micro Pro clones with FTDI or CH340 chips) right ?

It supports all MIDI techniques I'm aware of (USB MIDI on devices that support it, MIDI over serial for Hairless, MIDI over serial for custom MIDI USB firmware, etc.)

Here's an overview of MIDI interfaces you can use: Control Surface: MIDI Interfaces

Just to follow!