How do I extend this ShiftOut code to light two (or more) LEDs at once?

I'm modifying the ShiftOut Example code to accept MIDI input to change LEDs.

Currently I have the LEDs changing one by one, but I need to have one LED blink while the other is still on.

I have this code:

void loop() {
  
  if (Serial.available()>=3) {
  // now at least 3 bytes are in the buffer.
  First = Serial.read() ; 
  Second = Serial.read() ; 
  Third = Serial.read() ; 
  
  int bitToSet = Second - 60; //Set The output pin to the MIDI note number - 60.

 
 if (First == 144){
     if (Third == 127){
       registerWrite(bitToSet, HIGH);
     }
   }
  }
}

This works well, but when a new MIDI signal comes in, it turns off the previous LED. I need to keep the LED lit until a signal with Third == 0 comes through.

I guess to be more clear, I first need to know how to extend my code to keep one LED lit while the next LED gets lit.

Thanks for any help, and here is my complete code:

//Pin connected to latch pin (ST_CP) of 74HC595
const int latchPin = 8;
//Pin connected to clock pin (SH_CP) of 74HC595
const int clockPin = 12;
////Pin connected to Data in (DS) of 74HC595
const int dataPin = 11;
int First = 0;
int Second = 0;
int Third = 0;

void setup() {
  //set pins to output because they are addressed in the main loop
  pinMode(latchPin, OUTPUT);
  pinMode(dataPin, OUTPUT);  
  pinMode(clockPin, OUTPUT);
  Serial.begin(57600);
  Serial.println("reset");
}

void loop() {
  
  if (Serial.available()>=3) {
  // now at least 3 bytes are in the buffer.
  First = Serial.read() ; 
  Second = Serial.read() ; 
  Third = Serial.read() ; 
  
  int bitToSet = Second - 60; //Set The output pin to the MIDI note number - 60.

 
 if (First == 144){
   
     if (Third == 127){
       registerWrite(bitToSet, HIGH);
       registerWrite(bitToSet + 2, HIGH);
     }
   }
  }
}


// This method sends bits to the shift register:

void registerWrite(int whichPin, int whichState) {
// the bits you want to send
  byte bitsToSend = 0;

  // turn off the output so the pins don't light up
  // while you're shifting bits:
  digitalWrite(latchPin, LOW);

  // turn on the next highest bit in bitsToSend:
  bitWrite(bitsToSend, whichPin, whichState);

  // shift the bits out:
  shiftOut(dataPin, clockPin, MSBFIRST, bitsToSend);

    // turn on the output so the LEDs can light up:
  digitalWrite(latchPin, HIGH);

}

I tried to do something simple like this to just get two LEDs to light, but no luck:

if (Third == 127){
       registerWrite(bitToSet, HIGH);
       registerWrite(bitToSet + 2, HIGH);
     }
  int bitToSet = Second - 60; //Set The output pin to the MIDI note number - 60.

The name, and future use, of this variable imply that Second will contain a value between 60 and 67. Will that always be true?

What does the registerWrite() function do?

Change:-
byte bitsToSend = 0;
to
static byte bitsToSend = 0;

In that way the bit you have already turned on will stay on until you turn it off with a call
registerWrite(bitToSet, LOW);

One little word was all it took...works perfectly-Thanks so much Grumpy_Mike!

Hi PaulS, the registerWrite() function is in the above full code at the bottom.

Excellent :slight_smile:

Now your next task is to understand why it made that difference. :wink: