Skip to content

Instantly share code, notes, and snippets.

@robertcedwards
Created October 27, 2016 18:46
Show Gist options
  • Save robertcedwards/43e5adf20e727a7ca2066b80c5afb3ae to your computer and use it in GitHub Desktop.
Save robertcedwards/43e5adf20e727a7ca2066b80c5afb3ae to your computer and use it in GitHub Desktop.
usbmidi library example with FastLED library.
#include <FastLED.h>
#define NUM_LEDS 14
#define DATA_PIN 5 // Green
#define CLOCK_PIN 6 // Blue
CRGB leds[NUM_LEDS];
void OnProgramChange(byte channel, byte program) {
delay(500);
if (usbMIDI.getChannel() == 1 && usbMIDI.getData1() == 8) // MIDI CC 8, Channel 1
{
lightsOn();
}
else if (usbMIDI.getChannel() == 1 && usbMIDI.getData1() == 9) // MIDI CC 8, Channel 1
{
lightsOff();
}
}
void setup()
{
FastLED.addLeds<APA102, DATA_PIN, CLOCK_PIN>(leds, NUM_LEDS);
FastLED.show();
usbMIDI.setHandleProgramChange(OnProgramChange);
}
void loop()
{
usbMIDI.read();
}
// function fading lights on
void lightsOn() {
int lightsOnMaxBrightness = 64;
int lightsOnMinBrightness = 2;
for (int i = lightsOnMinBrightness; i < lightsOnMaxBrightness; i++) {
lightsOnMinBrightness = i;
FastLED.show();
for ( int j = 0; j < NUM_LEDS; j++) {
leds[j] = CRGB(lightsOnMinBrightness);
}
}
}
void lightsOff() {
int lightsOffMaxBrightness = 64;
int lightsOffMinBrightness = 2;
for (int i = lightsOffMaxBrightness; i > lightsOffMinBrightness; i--) {
lightsOffMaxBrightness = i;
FastLED.show();
for ( int j = 0; j < NUM_LEDS; j++) {
leds[j] = CRGB(lightsOffMaxBrightness);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment