MIDI CONTROLLER - Turn LEDs on with MIDI messages!

Hello everyone, this is my first post so I'm gonna introduce me :open_mouth:
I'm Karím. I'm 14 yrs old and I'm from Argentina.
Some weeks ago, I started my first Arduino project: A MIDI Controller for DAWs and DJs software.
I've almost finished the code for buttons, encoders and potentiometers out. Now I'm in the 'MIDI LED feedback' part.
I want to get LED on when the button is pressed with a code like this:

#define boton1 51
#define boton1led 50

int keyispressed1 = 0;
int noteisplaying1 = 0;

void setup() {
  Serial.begin (115200);
  pinMode(boton1, INPUT);
  pinMode(boton1led, OUTPUT);
  digitalWrite(boton1, HIGH);
}

void loop() {
  keyispressed1 = digitalRead(boton1);

  if (keyispressed1 == LOW){ //BOTON 1
    if(!noteisplaying1){ 
      digitalWrite(boton1led, HIGH);
       noteisplaying1 = 1; 
    }
  }
  else{
    if(noteisplaying1){ 
      digitalWrite(boton1led, LOW);
      noteisplaying1 = 0;
    }
  }
}

( I took off the MIDI out part for give you a shorter code )

But also, if the button I pressed has a toggle function in the software (like "Play", something that still activated after "un-press"), I want to send a message from the software (I tried and it's possible) and somehow get the LED still on. (Sorry for my bad english, this is very hard to explain hahaha).

The software can send:
A "note on" with velocity "127" when the function is activated, and velocity "0" when is desactivated.
Also, it can send a "Control Change" with same configuration of velocity.

I tried with reading "raw" serial and with the MIDI Library too, but I don't get it working...

byte message;
byte note;
byte velocity;
int led = 13; 

void setup() {
  pinMode(led,OUTPUT);  
  Serial.begin(115200);       
  digitalWrite(led,LOW);  
}

void loop () {

 if (Serial.available() > 0) {
    
    message = Serial.read();
    note = Serial.read();
    velocity = Serial.read();
    
    if (message == 144){
      if(note == 72) {
        if(velocity == 127) {
          digitalWrite(led, HIGH);
        } else {
          if(velocity == 0) {
            digitalWrite(led, LOW); 
             }
        }
      }
    } 
 }
}
#include <MIDI.h>

byte led = 13;
byte channel = 1;

void setup() {
  pinMode(led, OUTPUT);
  Serial.begin(115200);
}

void loop() {
  if (MIDI.read(channel)) {
   if (MIDI.getType() == ControlChange) {
     if (MIDI.getData1() == 1 ) {
      if (MIDI.getData2() == 127 ) {
        digitalWrite(led, HIGH);
      } else {
        if (MIDI.getData2() == 0 ) {
          digitalWrite(led, HIGH);
        }
      } 
    }
  }
 }
}

PLEASE HELP ME, I can't figure how get it working!
Thank you!

 if (Serial.available() > 0) {
    
    message = Serial.read();
    note = Serial.read();
    velocity = Serial.read();

So if one byte is available to read you go ahead and read three bytes. See anything wrong in that?

Grumpy_Mike:

 if (Serial.available() > 0) {

message = Serial.read();
    note = Serial.read();
    velocity = Serial.read();




So if one byte is available to read you go ahead and read three bytes. See anything wrong in that?

Thank you! Now the midi in works great! That was easy hahaha

But I've a problem implementing the default "LED on while button is pressed" with the MIDI in from software.
If I activate the control from my computer, the software send the message and LED turn on. But if I press the button, and the message comes in while LED is on by default, when I "unpress" the button the LED just turn off.
Do you understand me? How can this be solved?

This is the full code:

byte message;
byte note;
byte velocity;

int led = 13; 

int boton1 = 51;

int keyispressed1 = 0;
int noteisplaying1 = 0;


void setup() {
  Serial.begin(115200);       
  pinMode(boton1, INPUT);
  pinMode(led,OUTPUT);  
  digitalWrite(led,LOW);  
  digitalWrite(boton1, HIGH);
}


void loop () {

 if (Serial.available() == 3) {
    
    message = Serial.read();
    note = Serial.read();
    velocity = Serial.read();
    
    if (message == 176){
      if(note == 1) {
        if(velocity == 127) {
          digitalWrite(led, HIGH);
        } else {
          if(velocity == 0) {
            digitalWrite(led, LOW); 
             }
        }
      }
    } 
 }

keyispressed1 = digitalRead(boton1);
 
 if (keyispressed1 == LOW){ //BOTON 1
    if(!noteisplaying1){ 
      digitalWrite(led, HIGH);
      MIDI_TX(176,1,127);
       noteisplaying1 = 1; 
    }
  }
  else{
    if(noteisplaying1){ 
       digitalWrite(led, LOW);
      MIDI_TX(176,1,00);  
      noteisplaying1 = 0;
    }
  }
  
}

void MIDI_TX(unsigned char MESSAGE, unsigned char CONTROL, unsigned char VALUE) //pass values out through standard Midi Command
{
   Serial.write(MESSAGE);
   Serial.write(CONTROL);
   Serial.write(VALUE);
}

Thank you!

I need something like a buffer that store the action of 'turn on LED when the midi message arrives' until I release the button, don't?
How can it be done?

But I've a problem implementing the default "LED on while button is pressed" with the MIDI in from software.
If I activate the control from my computer, the software send the message and LED turn on. But if I press the button, and the message comes in while LED is on by default, when I "unpress" the button the LED just turn off.

Sorry but no I don't understand.
What is meant by on by default?
How does it get into that state.
You seam to have an inconsistency in your logic. If a released button turns off an led what is the thing that turns it off when it is on by default?

Grumpy_Mike:

But I've a problem implementing the default "LED on while button is pressed" with the MIDI in from software.
If I activate the control from my computer, the software send the message and LED turn on. But if I press the button, and the message comes in while LED is on by default, when I "unpress" the button the LED just turn off.

Sorry but no I don't understand.
What is meant by on by default?
How does it get into that state.
You seam to have an inconsistency in your logic. If a released button turns off an led what is the thing that turns it off when it is on by default?

If I understood well, when the arduino is receiving midi messages he wants a note off message to turn off the led, and not the button.
I suppose he could use a flag so when it's receiving midi messages the button off part with doesn't happens... I don't understand why he's receiving midi messages from the daw. Something like this? http://www.youtube.com/watch?v=Ztw-4VHIQaQ

Look at these videos:

Here, when you press a button the LED turn on
and when you release it LED turn off.

Here, the LEDs still on although you release the button.
That's because the DJ software send a ControlChange message to the controller.

Software send a message like this:
176-1-127 (For turn LED on)
176-1-0 (For turn LED off)

I've working the two parts, but separately.
When I hit "Play" in software with my mice, the software send the message and the LED turn on.
If I press the button of my controller, the first thing happens (LED on while button is pressed), but as the software message arrives while is this happening, the Arduino ignore it.

Here is a video I shoot.

I put anotations in it for explain a bit more.

I think I need a buffer for save the message until I release the button.

Please forgive me if I can't explain it very well, it's so hard in English for me!

I COULD SOLVE IT BY MYSELF.
ANYWAY, THANK YOU FOR HELP.