Skip to content

Instantly share code, notes, and snippets.

@toptensoftware
Created April 14, 2021 23:41
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/3b2011f25674a57033e4513010becf41 to your computer and use it in GitHub Desktop.
Save toptensoftware/3b2011f25674a57033e4513010becf41 to your computer and use it in GitHub Desktop.
Arduino script to flash LED using Cantabile Tempo LEDs binding
in#include <Adafruit_NeoPixel.h>
#include <MIDI.h>
#define PIN_COLOR_LEDS 6
#define CC_TEMPO_LEDS 127
#define BEAT_MASK 0x0F
#define COLOR_DOWNBEAT 0x001000
#define COLOR_OTHERBEATS 0x080000
#define MILLIS_FLASH 100
Adafruit_NeoPixel colorLeds = Adafruit_NeoPixel(8, PIN_COLOR_LEDS, NEO_GRB + NEO_KHZ800);
MIDI_CREATE_DEFAULT_INSTANCE();
// time stamp start of last flash
unsigned long _millisFlashOn = 0;
// 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 beat = MIDI.getData2() & BEAT_MASK;
// Update LEDs
colorLeds.clear();
colorLeds.setPixelColor(0, beat == 0 ? COLOR_DOWNBEAT : COLOR_OTHERBEATS);
colorLeds.show();
_millisFlashOn = millis();
}
}
// Turn of LED after timeout
if (_millisFlashOn != 0 && millis() - _millisFlashOn > MILLIS_FLASH)
{
_millisFlashOn = 0;
colorLeds.clear();
colorLeds.show();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment