MIDI IN too slow

Hi everybody,
I will try to explain...
I use 8 encoders with 8 leds rings.
This works but when i send CC1 on channel 2, the led ring corresponding to channel 1 moves like ignoring the channel...
When i send CC 2 channel 2 the second encoder's leds moves.
Also the first led ring works good and the others are very slow. Do i need to use separate datas like in ArrayOfLedArrays from fastled

This is just a smaller program where i'm having troubles

#include <FastLED.h>
#define DATA_PIN 13
#define NUM_LEDS 64
#define NUM_ENCODERS 8
CRGB leds[NUM_LEDS];
long LEDSCOLOR=0xFFFFFF;
uint8_t BRIGHTNESS=10;
int counterencoder[NUM_ENCODERS] = {0, 0, 0, 0, 0, 0, 0, 0}; 

int CHANNEL_NUMBER_ENCODER[NUM_ENCODERS] = {};
int CHANNEL_NUMBER[NUM_ENCODERS] = {176,177,178,179,180,181,182,183};
int CC_NUMBER[NUM_ENCODERS] = {1,2,3,4,5,6,7,8};
int CC_VALUE[NUM_ENCODERS] = {};

byte state = 0;
byte statusByte;
byte statusByteIN;
byte dataByte1;
byte dataByte2;


int COLORVALUE=0;
int BRIGHTNESSVALUE=10;



void setup() {
  // put your setup code here, to run once:
  Serial.begin (31250); 
  FastLED.addLeds<WS2812B, DATA_PIN, GRB>(leds, NUM_LEDS);
}
void loop() {
  // put your main code here, to run repeatedly:
MidiIn();
}





void MidiIn(){      
  for (int i=0; i<NUM_ENCODERS; i++){ 
while (Serial.available() > 0) {
byte incomingByte = Serial.read();  

switch (state){ 
  
case 0:
if (incomingByte == CHANNEL_NUMBER[i]){       
statusByteIN = incomingByte;
state=1;                           
}
else state=0;     

case 1:                           
if(incomingByte < 0x80 /*&& statusByteIN == CHANNEL_NUMBER[i]*/) {           
dataByte1 = incomingByte;           
state = 2;    
}
else state=0;        
break;                              

case 2: 
if(dataByte1 == CC_NUMBER[i]/* && statusByteIN == CHANNEL_NUMBER[i]*/) {         
dataByte2 = incomingByte;         
 CC_VALUE[i]=dataByte2;
counterencoder[i]=CC_VALUE[i];
}
   ledsdisplay();
         state = 0;                
         break;                     
  }
}
}
}



void ledsdisplay(){
switch (COLORVALUE) {
  case 0:
    LEDSCOLOR=0xFF0000;
    break;  
  case 1:
   LEDSCOLOR=0x008000;
    break;
     case 2:
LEDSCOLOR=0x0000FF;
    break;
     case 3:
LEDSCOLOR=0xFFFF00;
    break;
     case 4:
LEDSCOLOR=0xFF4500;
    break;
     case 5:
LEDSCOLOR=0x800080;
    break;
     case 6:
LEDSCOLOR=0x87CEEB;
    break;
         case 7:
LEDSCOLOR=0xFFFFFF;
    break;
}
for (int i=0; i<NUM_ENCODERS; i++){
BRIGHTNESS = BRIGHTNESSVALUE;
FastLED.setBrightness(  BRIGHTNESS );
if(0<=counterencoder[i] && counterencoder[i]<=15){
leds[i*NUM_ENCODERS+7] = CRGB::Black;
leds[i*NUM_ENCODERS+6] = CRGB::Black;
leds[i*NUM_ENCODERS+5] = CRGB::Black;
leds[i*NUM_ENCODERS+4] = CRGB::Black;
leds[i*NUM_ENCODERS+3] = CRGB::Black;
leds[i*NUM_ENCODERS+2] = CRGB::Black;
leds[i*NUM_ENCODERS+1] = LEDSCOLOR;
leds[i*NUM_ENCODERS+0] = CRGB::Black;
}
if(16<=counterencoder[i] && counterencoder[i]<=31){  
leds[i*NUM_ENCODERS+7] = CRGB::Black;
leds[i*NUM_ENCODERS+6] = CRGB::Black;
leds[i*NUM_ENCODERS+5] = CRGB::Black;
leds[i*NUM_ENCODERS+4] = CRGB::Black;
leds[i*NUM_ENCODERS+3] = CRGB::Black;
leds[i*NUM_ENCODERS+2] = CRGB::Black;
leds[i*NUM_ENCODERS+1] = CRGB::Black;
leds[i*NUM_ENCODERS+0] = LEDSCOLOR;
}
if(32<=counterencoder[i] && counterencoder[i]<=47){ 
leds[i*NUM_ENCODERS+7] = LEDSCOLOR;
leds[i*NUM_ENCODERS+6] = CRGB::Black;
leds[i*NUM_ENCODERS+5] = CRGB::Black;
leds[i*NUM_ENCODERS+4] = CRGB::Black;
leds[i*NUM_ENCODERS+3] = CRGB::Black;
leds[i*NUM_ENCODERS+2] = CRGB::Black;
leds[i*NUM_ENCODERS+1] = CRGB::Black;
leds[i*NUM_ENCODERS+0] = CRGB::Black;
}
if(48<=counterencoder[i] && counterencoder[i]<=63){ 
leds[i*NUM_ENCODERS+7] = CRGB::Black;
leds[i*NUM_ENCODERS+6] = LEDSCOLOR;
leds[i*NUM_ENCODERS+5] = CRGB::Black;
leds[i*NUM_ENCODERS+4] = CRGB::Black;
leds[i*NUM_ENCODERS+3] = CRGB::Black;
leds[i*NUM_ENCODERS+2] = CRGB::Black;
leds[i*NUM_ENCODERS+1] = CRGB::Black;
leds[i*NUM_ENCODERS+0] = CRGB::Black;
}    
if(64<=counterencoder[i] && counterencoder[i]<=79){
leds[i*NUM_ENCODERS+7] = CRGB::Black;
leds[i*NUM_ENCODERS+6] = CRGB::Black;
leds[i*NUM_ENCODERS+5] = LEDSCOLOR;
leds[i*NUM_ENCODERS+4] = CRGB::Black;
leds[i*NUM_ENCODERS+3] = CRGB::Black;
leds[i*NUM_ENCODERS+2] = CRGB::Black;
leds[i*NUM_ENCODERS+1] = CRGB::Black;
leds[i*NUM_ENCODERS+0] = CRGB::Black;
}
if(80<=counterencoder[i] && counterencoder[i]<=95){        
leds[i*NUM_ENCODERS+7] = CRGB::Black;
leds[i*NUM_ENCODERS+6] = CRGB::Black;
leds[i*NUM_ENCODERS+5] = CRGB::Black;
leds[i*NUM_ENCODERS+4] = LEDSCOLOR;
leds[i*NUM_ENCODERS+3] = CRGB::Black;
leds[i*NUM_ENCODERS+2] = CRGB::Black;
leds[i*NUM_ENCODERS+1] = CRGB::Black;
leds[i*NUM_ENCODERS+0] = CRGB::Black;
}
if(96<=counterencoder[i] && counterencoder[i]<=111){  
leds[i*NUM_ENCODERS+7] = CRGB::Black;
leds[i*NUM_ENCODERS+6] = CRGB::Black;
leds[i*NUM_ENCODERS+5] = CRGB::Black;
leds[i*NUM_ENCODERS+4] = CRGB::Black;
leds[i*NUM_ENCODERS+3] = LEDSCOLOR;
leds[i*NUM_ENCODERS+2] = CRGB::Black;
leds[i*NUM_ENCODERS+1] = CRGB::Black;
leds[i*NUM_ENCODERS+0] = CRGB::Black;
}
if(112<=counterencoder[i] && counterencoder[i]<=127){ 
leds[i*NUM_ENCODERS+7] = CRGB::Black;
leds[i*NUM_ENCODERS+6] = CRGB::Black;
leds[i*NUM_ENCODERS+5] = CRGB::Black;
leds[i*NUM_ENCODERS+4] = CRGB::Black;
leds[i*NUM_ENCODERS+3] = CRGB::Black;
leds[i*NUM_ENCODERS+2] = LEDSCOLOR;
leds[i*NUM_ENCODERS+1] = CRGB::Black;
leds[i*NUM_ENCODERS+0] = CRGB::Black;
  }
    }
      FastLED.show();
}

Thanks

Yes that is one solution, or you could have separate instances of the Fast led library with different pins used.

Never a good idea to post just a section of code, most of the time the problem is in the part you didn't include. Will the code you posted run stand alone? If so people could try it, and also improve it because it is very rambling.you should make more use of arrays to cut it down.

I am a bit confused as to what you want to do exactly. Is it to match the ring that changes to the channel the MIDI message is coming in on?

Your MIDI read section of code will not work because it doesn't cater for all lengths on MIDI message and does not wait until all MIDI bytes for one message are in.

Thank you Mike!!
This function works and do what i want

void MidiIn(){      
  for (int i=0; i<NUM_ENCODERS; i++){ 
while (Serial.available() > 0) {
byte incomingByte = Serial.read();  

switch (state){ 
  
case 0:
if (incomingByte> 0x80){       
statusByteIN = incomingByte;
state=1;                           
}
else state=0;     

case 1:                           
if(incomingByte < 0x80) {           
dataByte1 = incomingByte;           
state = 2;    
}
else state=0;        
break;                              

case 2: 
if(incomingByte < 0x80){         
dataByte2 = incomingByte;         
if (statusByteIN==CHANNEL_NUMBER[i] && dataByte1== CC_NUMBER[i]){
CC_VALUE[i]=dataByte2;
counterencoder[i]=CC_VALUE[i];

}
ledsdisplay();
         state = 0;                
         break;                     
  }
}
}
}
}

About arrays is it about ledsdisplay function?
Is it a good idea to use multiplexers for data lines to save pins?

Your help is always efficient thanks a lot

No.

It is about code like this

When ever you find yourself writing nearly the same thing over and over and over then you should think about what your doing and use an array.

In this case write a function that turns off all the LEDs call that and then just turn the one you want after the if statement.

No.
Using a multiplexer is a very bad idea for outputs because when you select the output you want to control all the other outputs are HIGH. ( or LOW depending on what sort of multiplexer you have).

To save pins you need to use one of the following.

  1. A shift register.
  2. an addressable latch.
  3. A port expander.

I separated all data lines and moved ledsdisplay function into midiread() and it's better but not very good do you have some ideas to read midi faster?Is it possible?
I use motorized faders for tests and i know arduino loose some midi even if i delete ledsdisplay function.
All led rings have the same speed now.
But with this code it's better... Thanks Mike

#include <FastLED.h>
#define NUM_LEDS_PER_STRIP 8
#define NUM_ENCODERS 8
CRGB leds[NUM_ENCODERS][NUM_LEDS_PER_STRIP];
long LEDSCOLOR=0xFFFFFF;
uint8_t BRIGHTNESS=10;
int counterencoder[NUM_ENCODERS] = {0, 0, 0, 0, 0, 0, 0, 0}; 
int CHANNEL_NUMBER_ENCODER[NUM_ENCODERS] = {};
int CHANNEL_NUMBER[NUM_ENCODERS] = {176,177,176,179,180,181,182,183};
int CC_NUMBER[NUM_ENCODERS] = {1,1,3,4,5,6,7,8};
int CC_VALUE[NUM_ENCODERS] = {};
byte state = 0;
byte statusByte;
byte statusByteIN;
byte dataByte1;
byte dataByte2;
int COLORVALUE=0;
int BRIGHTNESSVALUE=10;

void setup() {
  Serial.begin (31250); 
  FastLED.addLeds<WS2812B, 13>(leds[0], NUM_LEDS_PER_STRIP);
  FastLED.addLeds<WS2812B, 12>(leds[1], NUM_LEDS_PER_STRIP);
  FastLED.addLeds<WS2812B, 11>(leds[2], NUM_LEDS_PER_STRIP);
  FastLED.addLeds<WS2812B, 10>(leds[3], NUM_LEDS_PER_STRIP);
  FastLED.addLeds<WS2812B,  9>(leds[4], NUM_LEDS_PER_STRIP);
  FastLED.addLeds<WS2812B,  8>(leds[5], NUM_LEDS_PER_STRIP);
  FastLED.addLeds<WS2812B,  7>(leds[6], NUM_LEDS_PER_STRIP);
  FastLED.addLeds<WS2812B,  6>(leds[7], NUM_LEDS_PER_STRIP);
}

void loop() {
MidiIn();
}

void MidiIn(){      
  for (int i=0; i<NUM_ENCODERS; i++){ 
while (Serial.available() > 0) {
byte incomingByte = Serial.read();  

switch (state){ 
  
case 0:
if (incomingByte> 0x80){       
statusByteIN = incomingByte;
state=1;                           
}
else state=0;     

case 1:                           
if(incomingByte < 0x80) {           
dataByte1 = incomingByte;           
state = 2;    
}
else state=0;        
break;                              

case 2: 
if(incomingByte < 0x80){         
dataByte2 = incomingByte;         
if (statusByteIN==CHANNEL_NUMBER[i] && dataByte1== CC_NUMBER[i]){
CC_VALUE[i]=dataByte2;
counterencoder[i]=CC_VALUE[i];
}
ledsdisplay();
         state = 0;                
         break;                     
  }
}
}
}
}

void ledsdisplay(){
switch (COLORVALUE) {
  case 0:
    LEDSCOLOR=0xFF0000;
    break;  
  case 1:
   LEDSCOLOR=0x008000;
    break;
     case 2:
LEDSCOLOR=0x0000FF;
    break;
     case 3:
LEDSCOLOR=0xFFFF00;
    break;
     case 4:
LEDSCOLOR=0xFF4500;
    break;
     case 5:
LEDSCOLOR=0x800080;
    break;
     case 6:
LEDSCOLOR=0x87CEEB;
    break;
         case 7:
LEDSCOLOR=0xFFFFFF;
    break;
}
      for(int x = 0; x < NUM_LEDS_PER_STRIP; x++){
for (int i=0; i<NUM_ENCODERS; i++){
      leds[i][x] = CRGB::Black;
BRIGHTNESS = BRIGHTNESSVALUE;
FastLED.setBrightness(  BRIGHTNESS );
if(0<=counterencoder[i] && counterencoder[i]<=15){
      leds[i][1] = LEDSCOLOR;
}
if(16<=counterencoder[i] && counterencoder[i]<=31){ 
      leds[i][0] = LEDSCOLOR;
}
if(32<=counterencoder[i] && counterencoder[i]<=47){ 
      leds[i][7] = LEDSCOLOR;
}
if(48<=counterencoder[i] && counterencoder[i]<=63){ 
      leds[i][6] = LEDSCOLOR;
}    
if(64<=counterencoder[i] && counterencoder[i]<=79){
      leds[i][5] = LEDSCOLOR;
}
if(80<=counterencoder[i] && counterencoder[i]<=95){
      leds[i][4] = LEDSCOLOR;
}
if(96<=counterencoder[i] && counterencoder[i]<=111){  
      leds[i][3] = LEDSCOLOR;
}
if(112<=counterencoder[i] && counterencoder[i]<=127){ 
      leds[i][2] = LEDSCOLOR;
}
    }
      }
      FastLED.show();
}

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.