Midi Input, Led. almost working but not very well :(

Hi all,
Basically, i'm having issues trying to get my midi controller working, ive been searching for days and cant figure out whats going wrong.
with the code i have cobbled together, i can get 1 led responding to midi, although it sometimes does not respond :frowning:
i also cant for the life of me get the second led to work at all, im not sure if im storing the serial values properly, i dont think the array is being updated. and the first leds unresponsiveness, i am nott 100% sure why its not working. any help on this would be fantastic.

#define MIDI_CHANNEL 1

byte incomingChannel[40];    //Read the incoming MIDI channel
byte incomingNote[40];       //Read the incoming MIDI note
byte incomingVelocity[40];   //Read the incoming note value
int ledStatus[40];


void setup()
{

  Serial.begin (115200);         

  pinMode(A8,OUTPUT);
  pinMode(A9,OUTPUT);
  pinMode(A10,OUTPUT);
  pinMode(A11,OUTPUT);
  pinMode(A12,OUTPUT);
  pinMode(A13,OUTPUT);
  pinMode(A14,OUTPUT);
  pinMode(A15,OUTPUT);                        

}  


void loop()
{



  {
    if (Serial.available() > 2)
    {
      incomingChannel[1] = Serial.read();
      incomingNote[1] = Serial.read();
      incomingVelocity[1] = Serial.read();
      if((incomingChannel[1]== MIDI_CHANNEL+143)&&(incomingNote[1]== 41)&&(incomingVelocity[1]==127)) 
      {
        ledStatus[1] = 1;
      }
      if((incomingChannel[1]== MIDI_CHANNEL+143)&&(incomingNote[1]== 41)&&(incomingVelocity[1]==0))
      {
        ledStatus[1] = 0;
      }
      {
        if (ledStatus[1] == 1)
        {
          digitalWrite(A8,HIGH);
        }  
        if (ledStatus[1] == 0)
        {
          digitalWrite(A8,LOW);
        }
      }
    }
  }  

  {
    if (Serial.available() > 2)
    {
      incomingChannel[2] = Serial.read();
      incomingNote[2] = Serial.read();
      incomingVelocity[2] = Serial.read();
      
      if((incomingChannel[2]== MIDI_CHANNEL+143)&&(incomingNote[2]== 42)&&(incomingVelocity[2]==127)) 
      {
        ledStatus[2] = 1;
      }
      if((incomingChannel[2]== MIDI_CHANNEL+143)&&(incomingNote[2]== 42)&&(incomingVelocity[2]==0))
      {
        ledStatus[2] = 0;
      }
      {
        if (ledStatus[2] == 1)
        {
          digitalWrite(A9,HIGH);
        }  
        if (ledStatus[2] == 0)
        {
          digitalWrite(A9,LOW);
        }
      }
    }
  }     


} //VOID LOOP OUT

Your code is a train wreck, please fix the format so it looks presentable and possibly readable.

i know its bad, im a beginner and ive never coded before. so im not really sure how to tidy it up.

CTRL + T for starters. Next make it readable.

thanks, i didnt know about CTRL+T, i hope that looks better now. im not sure how to make it readable beyond that without writing different code, which i probably need to do. my problem is that my code is built from looking at hundreds of examples, i cant really understand most of what i read, even looking through the arduino reference for what each of the characters do. i still dont really get it, although i am begining to pick up the basics.

anyway, ive updated the code in the original post, and im sorry its not better, im really struggling here, but i'm wiling to follow any advice you can give me, and thanks for taking the time

Allan

thanks to that, the second led is now working! thanks so much!

the issue now is, the arduino ignores some midi note messages, it seems to want to receive the notes when it feels like it, is this a timing thing? or a problem with stoning redundant data into the array? or both?

thnks again
allan

There are better codes out there then what you have, that may help you better. Here is a link, CLICK HERE Take a look at the middle example.

thanks, i already seen that example several times, but i couldnt understand it. i think i get it now, ill go and give it a bash. thanks for pointing that out. the internet is saturated with examples, all different. its hard to figure out what applies to my project. so thanks again, hopefully i can make it work :slight_smile:

Try this one too.

#define MIDI_CHANNEL 1

byte incomingChannel;    //Read the incoming MIDI channel
byte incomingNote;       //Read the incoming MIDI note
byte incomingVelocity;   //Read the incoming note value
byte LEDs[8]={
  A8,A9,A10,A11,A12,A13,A14,A15};
byte ledStatus[8];


void setup()
{
  Serial.begin (115200);         
  for(byte p = 0; p < 8; p++)
  { 
    pinMode(LEDs[p],OUTPUT);
  }  
}  


void loop()
{
  if(Serial.available() > 2)
  {
    incomingChannel = Serial.read();
    incomingNote = Serial.read();
    incomingVelocity = Serial.read();

    if(incomingChannel == MIDI_CHANNEL+143)
    {
      if(incomingNote == 41) 
      {
        if(incomingVelocity == 127)
          ledStatus[0] = 1;
        else
          ledStatus[0] = 0;
      }

      if(incomingNote == 42)
      {
        if(incomingVelocity == 127)
          ledStatus[1] = 1;
        else
          ledStatus[1] = 0;
      }
      
      for(byte D = 0; D < 8;D++)
        digitalWrite(LEDs[D],ledStatus[D]); 

    }
  }
} //End of loop()

hey thanks for that! :slight_smile: i was away seeing to the baby, so thats why i didnt reply sooner.

i changed
byte ledStatus[8];

to
int ledStatus[8];

and it worked flawlessly,

it didnt work till i did that, so maybe a typo?

but now it works great, thanks so much for all your help!

Sincerely
Allan