Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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