Trying to store midi velocity as a LED state, not working at all

Hi all,

im having a major headache here, basically i am building a midi controller and all was well, i decided i wanted to add more functionality to the leds, i want them to light when they receive a velocity of 127, turn off when they receive a vellocity of 0, i had this working fine. but now i want them to flash when they rceive a velcoity of 1 and alternate flash on a 2 (i’l worry about the flash timer later)

at the moment for testing, im trying to save the incoming velocity to an int ledstatus, and have each state light a different led, for testing purposes. i cant get it to work at all. ive tried making if statements for the to store the velocity to the led status but that was so slow and inreliable. so now im trying to store the velocity to the int directly and read that with if statements but its just not working!!! any help would be greatly appreciated, im sure im just making a rookie mistake here.

#define MIDI_CHANNEL 1




#define Led33 A8
#define Led34 A9
#define Led35 A10
#define Led36 A11
#define Led37 A15
#define Led38 A14
#define Led39 A13
#define Led40 A12


byte incomingChannel;    
byte incomingNote;       
byte incomingVelocity;   
int ledStatus[128];


//Blink variables
int ledState[128] = {
  LOW};             // ledState used to set the LED
long previousMillis[128] = {
  0};        // will store last time LED was updated
unsigned long currentMillis[128] = {
  0};

long interval = 500;           // interval at which to blink (milliseconds)


int page;

//##########################################################################################
void setup()
{

  Serial.begin (115200);          // this is the BAUD rate for serial ( change to 31250 for Midi, and 115200 for serial to midi software on your computer)

  //8 Non Muxed LEDs
  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()
{

  // LED section
  if(Serial.available() > 2)


    incomingChannel = Serial.read();
  incomingNote = Serial.read();
  incomingVelocity = Serial.read();



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

  {
    if (ledStatus[0] == 0)
      digitalWrite(Led33,HIGH);
  }
  {
    if (ledStatus[0] == 1)
      digitalWrite(Led34,HIGH);
  }
  { 
    if (ledStatus[0] == 2)
      digitalWrite(Led35,HIGH);
  }
  { 
    if (ledStatus[0] == 3)
      digitalWrite(Led36,HIGH);
  }
  /*
{
   if (ledStatus[0] == 3){
   currentMillis[0] = millis();
   if(currentMillis[0] - previousMillis[0] > interval) {
   previousMillis[0] = currentMillis[0];
   if (ledState[0] == LOW)
   ledState[0] = HIGH;
   else
   ledState[0] = LOW;
   digitalWrite(Led34,ledState[0]);}}
   
   
   
   
   }
   */

} //VOID LOOP OUT
int ledStatus[128];


//Blink variables
int ledState[128] = {
  LOW};             // ledState used to set the LED
long previousMillis[128] = {
  0};        // will store last time LED was updated
unsigned long currentMillis[128] = {
  0};

Have you determined how many bytes of SRAM are needed to store all these values? Have you looked up how much you have?

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

Useless curly braces should be deleted. NOTHING goes on the same line as any }. There should be a carriage return, or at least a space, before the {.

I gave up at this point. The useless curly braces are too distracting.

  if(Serial.available() > 2)


    incomingChannel = Serial.read();
  incomingNote = Serial.read();
  incomingVelocity = Serial.read();

So if you have three or more bytes arrived then read one of them. Then irrespective of anything else read two more byte if they are there or not. you have to get a grip of your braces.

hey, thanks for taking the time to reply to me, any help at all is greatly appreciated.

ive deleted the curly braces, and hit ctrl T, no change with regards to the leds lighting yet.

i hadnt even considered the SRAM, turns out i have 8kb, (mega2560) i think that may be enough, not sure though as i will have more variables in the actual end sketch. i haven't counted them yet to be honest. heres the code without braces

#define MIDI_CHANNEL 1




#define Led33 A8
#define Led34 A9
#define Led35 A10
#define Led36 A11
#define Led37 A15
#define Led38 A14
#define Led39 A13
#define Led40 A12


byte incomingChannel;    
byte incomingNote;       
byte incomingVelocity;   
int ledStatus[128];


//Blink variables
int ledState[128] = {
  LOW};             // ledState used to set the LED
long previousMillis[128] = {
  0};        // will store last time LED was updated
unsigned long currentMillis[128] = {
  0};

long interval = 500;           // interval at which to blink (milliseconds)


int page;

//##########################################################################################
void setup()
{

  Serial.begin (115200);          // this is the BAUD rate for serial ( change to 31250 for Midi, and 115200 for serial to midi software on your computer)

  //8 Non Muxed LEDs
  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()
{

  // LED section
  if(Serial.available() > 2)


    incomingChannel = Serial.read();
  incomingNote = Serial.read();
  incomingVelocity = Serial.read();



  {
    if(incomingChannel == MIDI_CHANNEL+143)    
      if(incomingNote == 0) 
        ledStatus[0] = incomingVelocity;


    if (ledStatus[0] == 0)
      digitalWrite(Led33,HIGH);

    if (ledStatus[0] == 1)
      digitalWrite(Led34,HIGH);

    if (ledStatus[0] == 2)
      digitalWrite(Led35,HIGH);

    if (ledStatus[0] == 3)
      digitalWrite(Led36,HIGH);
  }
  /*
{
   if (ledStatus[0] == 3){
   currentMillis[0] = millis();
   if(currentMillis[0] - previousMillis[0] > interval) {
   previousMillis[0] = currentMillis[0];
   if (ledState[0] == LOW)
   ledState[0] = HIGH;
   else
   ledState[0] = LOW;
   digitalWrite(Led34,ledState[0]);}}
   
   
   
   
   }
   */

} //VOID LOOP OUT

Aha!

changed it to this

 // LED section
  if(Serial.available() > 2){


    incomingChannel = Serial.read();
    incomingNote = Serial.read();
    incomingVelocity = Serial.read();
  }

its working perfectly now, thanks mike!

you are correct though, i really do need to get a handle on all of this, i still feel like there is so much i dont know that i really ought to.

also thank you Paul, i will try not to over use the braces in the future, i find it tricky to understand what needs to be isolated from another bit of code, my train of thought was to isolate everything, and read back to ints and the like

Braces group commands not isolate them. You need them 1) round a function 2) after a conditional function lie an if or while 3) round a switch body