Problem with code I found online.

The following is the code I found online, I am very new to coding especially in this language, and would greatly appreciate if anyone could see what the flaws are in my code.


//Pin connected to latch pin (ST_CP) of 74HC595
const int latchPin = 9;
//Pin connected to clock pin (SH_CP) of 74HC595
const int clockPin = 8;
//Pin connected to Data in (DS) of 74HC595
const int dataPin = 10;

int pitch, cmd, velocity;
int light;
boolean state;

byte lightData1 = 0, lightData2 = 0, lightData3 = 0; //
void setup () {
   pinMode (12, OUTPUT); 
    pinMode (14, OUTPUT);  
      pinMode (11, OUTPUT);


    void loop ()

    //read the midi signal
;     if (Serial.available() > 2) {
      cmd =;
      pitch =;
      velocity =;

      //the lights are set according to the data on MIDI ch.1
      if (cmd == 144) state = 0;          //144 ch1 noteON ; 0=relayON
      else if (cmd == 128) state = 1;   //128 ch1 noteOFF  ; 1=relayOFF

      // so that lights don't change while the new data is being shifted
      digitalWrite(latchPin, LOW);

      light = getBit(pitch);
      // set the corresponding bit in lightData
      if (light < 8)
        bitWrite(lightData1, light, state);
      else if (light < 16) {
        light -= 8;
        bitWrite(lightData2, light, state);
      else {
        light -= 16;
        bitWrite(lightData3, light, state);

      // shift out the data
      shiftOut(dataPin, clockPin, MSBFIRST, lightData3);
      shiftOut(dataPin, clockPin, MSBFIRST, lightData2);
      shiftOut(dataPin, clockPin, MSBFIRST, lightData1);

      // turn on the lights according to the new data
      digitalWrite(latchPin, HIGH);

Where is the rest of it? That code ends part way thru the loop() function.

You should be suspicious of most code you find "on line". There is no quality control.

Include links for everything you wish to discuss, including libraries and #includes.

Where is the rest of it? That code ends part way thru the loop() function.

Actually, it ends right at the beginning because of the semicolon:

    void loop ()

    //read the midi signal
;     if (Serial.available() > 2) {

So loop() does nothing at all.

The following is the code I found online, I am very new to coding especially in this language, and would greatly appreciate if anyone could see what the flaws are in my code.

You code is perfectly fine as far as I can see. What is making you think that there is anything wrong with it?

I have gotten the following error codes from the code. There is to many to put in here so I have to put a added on file. I should have known not to trust some random code I found online. I pasted the entire code I have here it just ends like that.

error codes.pdf (56.1 KB)

I pasted the entire code I have here it just ends like that.

Cool. You are a total beginner. You need to do a tutorial. The arduino IDE has examples, and you should google “C++ tutorial” to learn about the C++ language.

You will need to start with “toy” examples - copy/pasting a huge slab of code that you don’t know what it does and you don’t understand how it does it from the web usually won’t work. It’s like learning absolutely anything else.

A semicolon does not end a function. However, a missing curly bracket is a BIG problem.


// read the midi signal

if (Serial.available() > 2) {
      cmd =;
      pitch =;
      velocity =;

That’s a terrible MIDI parser. One running status or one 1-byte or 2-byte MIDI message and your code will break.
I’d recommend using a library to do the MIDI parsing for you.

It looks like you want MIDI note events to turn on or off outputs of a chain of shift registers. I did the exact same project some time ago, I’ve documented it in this short tutorial.

Since you’re using the Serial port for MIDI, you’ll have to use a HardwareSerialMIDI_Interface instead of a USBMIDI_Interface. You seem to be using three daisy-chained shift registers instead of one, so change the number of outputs of the shift register to 24 instead of 8:

// Include the library
#include <Control_Surface.h>
// Instantiate a MIDI Interface to use, using the first UART (Serial),
// and the default MIDI baud rate (31250, for standard Serial MIDI and
// 5-pin DIN).
HardwareSerialMIDI_Interface midi = {Serial, MIDI_BAUD};
// Instantiate a chain of shift registers as output for the LEDs.
// The chain has 24 outputs in total, e.g. using three 8-bit shift registers.
SPIShiftRegisterOut<24> sreg = {
  10,       // Latch pin (ST_CP)
  MSBFIRST, // Byte order
using namespace MIDI_Notes;
// Create an array of LEDs that listen to MIDI Note messages, turning on and off
// the LEDs connected to the 24 output pins of the shift register
NoteValueLED leds[] = {
  {, note(C, 4)}, // LED pin, address (note number, channel, cable)
  {, note(D, 4)}, //
  {, note(E, 4)}, //
  {, note(F, 4)}, //
  {, note(G, 4)}, //
  {, note(A, 4)}, //
  {, note(B, 4)}, //
  {, note(C, 5)}, //
  {, note(D, 5)}, //
  {, note(E, 5)}, //
  // ...
  {, note(F, 5)}, //
// Initialize the Control Surface
void setup() {
// Update the Control Surface
void loop() {

As the name implies, the SPIShiftRegisterOut class uses the SPI bus to drive the shift registers. This is much more efficient than using the shiftOut function.
Connect the shift register clock pin (SH_CP) to the SPI CLK pin (pin 13 on an UNO), and connect the shift register data pin (DS) to the SPI MOSI pin (pin 11 on an UNO). You can use any pin as latch pin, the code above uses pin 10.

If you really can’t use these pins, you can use the ShiftRegisterOut class (without SPI), it allows you to use any pins for clock and data, because it uses shiftOut under the hood. This is shown in the BitBang-Blink example.

I’d highly recommend following a beginner-level C++ tutorial before copy-and-pasting code that you don’t understand.


If you are new to programming I recommend working through this tutorial

Of course you can ask here in the forum. But then you have to wait for the answers. If you start to learn how coding works you become more and more independent of waiting for answers.

Take a look into this tutorial:

Arduino Programming Course

It is easy to understand and has a good mixture between explaining important concepts and example-codes to get you going. So give it a try and report your opinion about this tutorial.

I'm a somehow advanced programmer and this has the effect of beeing partially blind about beginners difficulties. But I would like to become an "expert" about beginners difficulties and therefore I appreciate feedback about that and to receive questions what is still hard to understand.

best regards Stefan

Whenever I read "The following is the code I found online.."

All I see is : "Wow I found this hamburger, should I eat it?"

-jim lee

Thank you to all of you who respectfully replied and even offered good places to learn. I will go ahead and use those resources. Thank you to those who actually helped with the code as well I appreciate it.