Skip to content

Instantly share code, notes, and snippets.

@toptensoftware
Created April 14, 2021 23:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save toptensoftware/db329ec84a3f99c5ffde08f100865acf to your computer and use it in GitHub Desktop.
Save toptensoftware/db329ec84a3f99c5ffde08f100865acf to your computer and use it in GitHub Desktop.
Arduino script to display tempo beat using Cantabile Tempo LEDs binding
#include <Adafruit_NeoPixel.h>
#include <MIDI.h>
#define PIN_COLOR_LEDS 6
#define CC_TEMPO_LEDS 127
#define BEAT_MASK 0x0F
#define BEATS_SHIFT 4
#define BEATS_OFFSET 2
#define BEATS_MASK 0x0F
Adafruit_NeoPixel colorLeds = Adafruit_NeoPixel(8, PIN_COLOR_LEDS, NEO_GRB + NEO_KHZ800);
MIDI_CREATE_DEFAULT_INSTANCE();
// One time setup
void setup()
{
// Clear LEDs
colorLeds.begin();
colorLeds.clear();
colorLeds.show();
// Init MIDI
MIDI.begin(MIDI_CHANNEL_OMNI); // Listen to all incoming messages
}
void loop()
{
// Read MIDI events
while (MIDI.read())
{
// Is it the correct CC
if (MIDI.getType() == midi::ControlChange && MIDI.getData1() == CC_TEMPO_LEDS)
{
// Read the current beat number and beat count
int beats = ((MIDI.getData2() >> BEATS_SHIFT) & BEATS_MASK) + BEATS_OFFSET;
int beat = MIDI.getData2() & BEAT_MASK;
// Update LEDs
colorLeds.clear();
for (int i=0; i<beats; i++)
{
colorLeds.setPixelColor(i, i == beat ? 0x040404 : 0x000001);
}
colorLeds.show();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment