Help with latency updating Ws2812 Leds with midi noteOn

Status
Not open for further replies.

Coyne

Member
Hi guys.

I am experiencing some problem with latency in my setup. Maybe some of you guys can spot the problem and point me in the right direction?

The setup is as follows.

I am sending usbMidiNoteOn to teensy 3.2 running the merge of Fastled and Pauls WS2812Serial to lit the leds respectively. On the midiNoteOff i am trying to make the leds fade out with nScale8 at a value i set with a midi cc message. Everything works fine when the fade out is instant. But when i make the fade longer i experience some kind of latency. Its like the noteOn/noteOffs stack up and get executed later when the leds have updated in the while loop and i experience a latency like the noteOn/off gets executed several seconds after i stopped sending notes.

At first i thought this could be due to how the usb stacks get sent but because i dont get the same problem when the fade out is instant i think its rather something with the while loop on the fade out off the leds thats gets in the way.

My idea then was to try and make some code that stops to execute the while loop when a new usbMidiNoteOn arrives to not stack them up. hence the Booleans noteOff1Active etc. But i doesnt seem to work. I get the same Time of the previous fade out even when i new noteOn arrives which makes me think something wrong with the code.

Hope anyone of you guys can enlighten me.
Best regards Niklas
 
Here is my code.

Code:
#include <WS2812Serial.h>
#define USE_WS2812SERIAL
#include <FastLED.h>
#include <MIDI.h>



///MIDI_CREATE_INSTANCE(HardwareSerial, Serial1, MIDI);


#define DATA_PIN 1
#define NUM_LEDS 256
#define COLOR_ORDER GRB

CRGB leds[NUM_LEDS];

byte Alpha1 = 0;
byte Alpha2 = 0;
byte Alpha3 = 0;
byte Alpha4 = 0;

byte Fader = 0;

byte color = 200;
byte saturat = 100;


boolean noteOff1Active = false;
boolean noteOff2Active = false;
boolean noteOff3Active = false;
boolean noteOff4Active = false;


void setup() {

  Serial.begin(57600);
  


  //MIDI.begin();
  usbMIDI.begin();

  //MIDI.setHandleNoteOn(OnNoteOn); // set handle for Note On message as function named "OnNoteOn"
  //MIDI.setHandleNoteOff(OnNoteOff); // set handle for Note Off message as function named "OnNoteOff"
  //MIDI.setHandleControlChange(OnControlChange); // set event handler for CC

  usbMIDI.setHandleNoteOn(OnNoteOn); // set handle for Note On message as function named "OnNoteOn"
  usbMIDI.setHandleNoteOff(OnNoteOff); // set handle for Note Off message as function named "OnNoteOff"
  usbMIDI.setHandleControlChange(OnControlChange); // set event handler for CC


  LEDS.addLeds<WS2812SERIAL,DATA_PIN,RGB>(leds,NUM_LEDS);
  LEDS.setBrightness(255);
  
}



void loop() {

  //MIDI.read();
  //usbMIDI.read(); // read the USB MIDI bus every loop
  while (usbMIDI.read()) {};
  

}



void fade1() {

  while (leds[1] && noteOff1Active) {
    
  
    for (int i = 0; i < 64; i++) {
      leds[i].nscale8(Fader);
    }
    FastLED.show();
    Serial.println("noteOff1Active true");
    
     
  }
}

void fade2() {

  while (leds[65] && noteOff2Active){
    
    for (int i = 64; i < 128; i++) {
      leds[i].nscale8(Fader);
    }
    FastLED.show();
  }

}

void fade3() {

    while (leds[129] && noteOff3Active) {
      
    for (int i = 128; i < 192; i++) {
      leds[i].nscale8(Fader);
    }
    FastLED.show();
  }

}

void fade4() {

    while (leds[193] && noteOff4Active);
 {
    for (int i = 192; i < 256; i++) {
      leds[i].nscale8(Fader);
    }
    FastLED.show();
  }

}

void lit () {

  fill_solid( leds, 64, CHSV(color, saturat, Alpha1));
  FastLED.show();

}


void lit2 () {

  fill_solid(leds+64, 64, CHSV(color, saturat, Alpha2));
  FastLED.show();

}

void lit3 () {

  fill_solid(leds+128, 64, CHSV(color, saturat, Alpha3));
  FastLED.show();

}

void lit4 () {

  fill_solid(leds+192, 64, CHSV(color, saturat, Alpha4));
  FastLED.show();

}


void OnNoteOn(byte channel, byte pitch, byte velocity) {

  if (channel == 1 && pitch == 36 ) {
    Alpha1 = velocity * 2 ;

    noteOff1Active = false;
    Serial.println("noteOff1Active false");
    
    lit();
  }

  if (channel == 1 && pitch == 37 ) {
    
    Alpha2 = velocity * 2 ;
    noteOff2Active = false;
    lit2();
  }

  if (channel == 1 && pitch == 38 ) {
    Alpha3 = velocity * 2 ;
    noteOff3Active = false;
    lit3();
  }

  if (channel == 1 && pitch == 39 ) {
    Alpha4 = velocity * 2 ;
    noteOff4Active = false;
    lit4();
  }
  
}


void OnNoteOff(byte channel, byte pitch, byte velocity) {

  if (channel == 1 && pitch == 36 && velocity < 70) {

    noteOff1Active = true;
    
    /*fill_solid(leds, 64, CHSV(0, 0, 0));
    FastLED.show();
    Serial.println("noteOff");*/

    fade1();

  }

  

  if (channel == 1 && pitch == 37 ) {

    noteOff2Active = true;

    /*fill_solid(leds+64, 64, CHSV(0, 0, 0));
    FastLED.show();*/

    fade2();

  }

  if (channel == 1 && pitch == 38 ) {

    noteOff3Active = true;

    /*fill_solid(leds+128, 64, CHSV(0, 0, 0));
    FastLED.show();*/

    fade3();

  }

  if (channel == 1 && pitch == 39 ) {

    noteOff4Active = true;

    /*fill_solid(leds+192, 64, CHSV(0, 0, 0));
    FastLED.show();*/

    fade4();

}
  }

void OnControlChange(byte channel, byte control, byte value)
{


    if ((channel == 1) && (control == 2)) {


    Fader = map(value, 0, 127, 0, 255);


  }

  
  if ((channel == 1) && (control == 1)) {


    color = map(value, 0, 127, 0, 255);


  }

  if ((channel == 1) && (control == 6)) {


    saturat = map(value, 0, 127, 0, 255);


  }

}
 
Last edited by a moderator:
Status
Not open for further replies.
Back
Top