granteeric

midi code update

May 22nd, 2023 (edited)
1,043
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <MIDI.h>
  2. #include <Adafruit_NeoPixel.h>
  3.  
  4. // Define the MIDI channel and note number
  5. const uint8_t midiChannel = 1;
  6. const uint8_t midiNote = 69;
  7.  
  8. // Define the NeoPixel strip
  9. const uint8_t numberOfPixels = 24;
  10. const uint8_t pin = 12;
  11.  
  12. //creation de l'objet de la bande de led
  13. Adafruit_NeoPixel strip = Adafruit_NeoPixel(numberOfPixels, pin, NEO_GRB + NEO_KHZ800);
  14.  
  15. // Initialize the MIDI library
  16. MIDI_CREATE_INSTANCE(HardwareSerial, Serial, MIDI);
  17.  
  18. // This function will be called whenever a MIDI message is received
  19. void onMIDIMessage(byte channel, byte command, byte data1, byte data2) {
  20.  
  21.   // Only process messages on the specified channel
  22.   if (channel == midiChannel) {
  23.     // If the message is a note on message...
  24.     if (command == NOTE_ON) {
  25.       // ...then set the brightness of the NeoPixels to the velocity of the note
  26.       uint8_t green = (data1 * 255) / 127;
  27.       for (uint8_t i = 0; i < numberOfPixels; i++) {
  28.         strip.setPixelColor(i, green, 0, 0);
  29.       }
  30.     }
  31.     else if (command == NOTE_OFF) {
  32.       // If the message is a note off message...
  33.         stip.clear();    
  34.     }
  35.     // Update the NeoPixels
  36.     strip.show();
  37.     }
  38. }
  39.  
  40. void setup() {
  41.   // Initialize the serial port
  42.   Serial.begin(9600);
  43.  
  44.   // Initialize the MIDI library
  45.   MIDI.begin(MIDI_CHANNEL_OMNI);
  46.  
  47.   // Attach the onMIDIMessage function to the MIDI library
  48.   MIDI.setHandleNoteOn(onMIDIMessage);
  49.   MIDI.setHandleNoteOff(onMIDIMessage);
  50.  
  51.   strip.begin();
  52.   strip.show();
  53. }
  54.  
  55. void loop() {
  56. // Do nothing
  57. }
  58.  
  59.  
Advertisement
Add Comment
Please, Sign In to add comment