help with a sketch using MIDI shield

Hi,
I was hoping to get some guidance on a sketch for project I am working on. I am trying to hook up a Drum Module to a Roland Sonic Cell synthesizer that then is hooked up via MIDI cable going to a sparkfun MIDI shield (used as a decoder and communicating with the arduino) that is connected to an Arduino that then plays physical instruments. I am trying to turn on and off the outputs from the MIDI shield processing the signals of the Roland Sonic Cell synthesizer. The main part I am having a hard time grasping is using the MIDI shield as a decoder and sending signals to the Arduino. Below is the sketch I have been working on minus the part for the MIDI shield (decoder).
Can anyone help with this?

//set gate time for each device in milliseconds
int crash=15;
int snare=20;
int smalltom=20;
int largetom=20;
int cowbell=20;

int gatetime [5] = {crash,snare,smalltom,largetom,cowbell};

unsigned long timenow [5];

void setup()
{
for (int i=8;i<15;i++)
{
pinMode(i, OUTPUT);
}
for (int i=2;i<8;i++)
{
pinMode (i, INPUT);
}
}

void loop()
{
if (digitalRead(7)==LOW)
{
digitalWrite(14, HIGH);
for (int i=0; i<5; i++)
{
digitalWrite(i+8,LOW);
}
}
else
{
digitalWrite (14, LOW);
for (int i=0;i<5;i++)
{
if (digitalRead(i+2)==HIGH)
{
timenow*=millis();*

  • digitalWrite(i+8, HIGH);*
  • }*
    if (millis() > timenow_+gatetime*)
    {
    digitalWrite (i+8,LOW);
    }
    }
    }
    }*_

Start with reading the first 3 posts from here: Planning and Implementing an Arduino Program

It's not clear what this program is supposed to do. There's only one comment. Put more comments in your code. While it is obvious to you now what the code does, in a month it will be impossible for you to work out what you wrote without comments.

This is a neat trick:

for (int i=8;i<15;i++)
  {
   pinMode(i, OUTPUT);
  }

But then why do you do this later?

 for (int i=0; i<5; i++)
     {
      digitalWrite(i+8,LOW);
     }

The "8" here is what the computer scientists call a "magic number." Why is it 8? I don't know; it is necessary for the magic spell to work. It's not immediately obvious that these two pieces of code cover almost but not quite exactly the same pins.

Try defining these pin numbers with meaningful names:

#define OutputPinOffset 8
#define NumInstruments 5
...
void setup() {
  ...
  for(int i = 0; i < NumInstruments; i++) {
     pinMode(i + OutputPinOffset, OUTPUT);
  }

It's still a little fragile: what if you want to add a new output on pin 2 and keep the others the same? Most arduino projects end up naming each pin's function individually rather than trying to use a loop.

Yes it is difficult to know what you are doing here. If it is the MIDI bit then why leave it out of the code you posted incorrectly, there are no code tags. Have you read the how to use the forum sticky post?

The code would be MUCH easier to read and understand if you used arrays instead of contiguously numbered ranges of pins:

// Instruemnts: crash, snare, smalltom, largetom, cowbell
const int InstrmentCount = 5;
const int InstrumentPins[InstrmentCount]   = {2,3,4,5,6};
const int InstruemntDwells[InstrmentCount] = {15,20,20,20,20}; // Milliseconds
unsigned long InstrumentTime[InstrmentCount];

// Triggers?
const int TriggerCount = 7;
const int TriggerPins[TriggerCount] = {8,9,10,11,12,13,A0};

void setup() {
  for (int i=0; i<InstrumentCount; i++)
    pinMode(InstrumentPins[i], OUTPUT);
  
  for (int i=0; i<TriggerCount; i++)
    pinMode (TriggerPins[i], INPUT);
}

Note: Pin 14 is NOT A0 on the Leonardo or MEGA so your code would fail in strange ways if you moved it to one of those boards. Best to use the names (A0-A5) instead of the numbers so the code is portable.

Sorry for the delay, I appreciate the help on this. Below is the sketch with the comments requested.
I will post another one after this with the MIDI information I have been working on with this sketch. This sketch has been tested and does work for both digital and analog outputs while multiple electronic drums are being played at the same time. The analog sounds are being generated by a Roland Sonic Cell synthesizer.

The major part I am having trouble with is The sparkfun MIDI shield to interpret the MIDI message (used as a decoder board). I am new to MIDI and could use a point in the right direction.

//set gate time for each device in milliseconds
int crash=15;
int snare=20;
int smalltom=20; //Durations for each motor to be activated/turned on
int largetom=20;
int cowbell=20;

int gatetime [5] = {crash,snare,smalltom,largetom,cowbell}; //Array that stores gate times for each instrument

unsigned long timenow [5];//Array that stores the current duration for each note, see below for more

void setup() //Assigns appropriate roles to pins
{
for (int i=8;i<15;i++)//pins 8-14
{
pinMode(i, OUTPUT);//pins 8-14 are outputs
}
for (int i=2;i<8;i++)//2-7 inputs from MIDI decoder board
{
pinMode (i, INPUT); //pin 2-7 are inputs
}
}

void loop()//Pin 7 determines what type of action if it is high- synth, low = actual instrument
{
if (digitalRead(7)==LOW)//inputs low = analog (synth music)
{
digitalWrite(14, HIGH);//pin 14 is on (enables power to the speaker relay, turns speaker on)
for (int i=0; i<5; i++)//pins 8 to 12 low loop
{
digitalWrite(i+8,LOW);//digital outputs low (turns of actual instruments)
}
}
else //this means pin 7 is HIGH - actual instruments to be played
{
digitalWrite (14, LOW);//analog speaker relay power off, no synth music
for (int i=0;i<5;i++)//loops through all the input pins for actual instruments
{
if (digitalRead(i+2)==HIGH)//tests if the current pin is HIGH, if so... go below
{
timenow*=millis();//The slot in timenow gets the current program lifetime*

  • digitalWrite(i+8, HIGH);//write HIGH to the corresponding output pin for actual drum that matches the input pin*
  • }*
    if (millis() > timenow_+gatetime*)//if the current program lifetime is greater than how long the instrument should be playing plus when it started playing, then go on...
    {
    digitalWrite (i+8,LOW);//turn the instrument off...this means the motor has been playing for long enough (gatetime for the drum)
    }
    }
    }
    }*_

Below is the sketch where I am trying to incorporate the MIDI decoder using the sparkfun MIDI shield. This is where i need more help and better understanding

//set gate time for each device in milliseconds
int crash=15;
int snare=20;
int smalltom=20; //Durations for each motor to be activated/turned on
int largetom=20;
int cowbell=20;

int gatetime [5] = {crash,snare,smalltom,largetom,cowbell}; //Array that stores gate times for each instrument

unsigned long timenow [5];//Array that stores the current duration for each note, see below for more

byte incomingByte;
byte note;
byte velocity

int action = 13; //select the pin for the LED

int action=2; //0=note off; 1=note on; 2= nada

//-----------------------------------------------------------------------------------------------
void setup() //Assigns appropriate roles to pins
{
for (int i=8;i<15;i++)//pins 8-14

pinMode(i, OUTPUT);//pins 8-14 are outputs

for (int i=2;i<8;i++)//2-7 inputs from MIDI decoder board

pinMode (i, INPUT); //pin 2-7 are inputs

pinMode(statusLed,OUTPUT)//declare the LED's pin as output

MIDI.begin(MIDI_CHANNEL_OMNI);// chooses all MIDI chanels

Serial.begin(31250)

digitalWrite(statusLED,HIGH);
}
//-----------------------------------------------------------------------------------------------
void loop()//Pin 7 determines what type of action if it is high- synth, low = actual instrument
{
if (Serial.available() > 0){
incomingByte = Serial.read();//read incoming byte
if (incomingByte== 144)//note on message starting
action=1;
}else if (incomingByte== 128){ //note off message starting
action=0;
}else if ( (action==0)&&(note==0) ){ //if we receive a "note off", we wait for the note (databyte)
note=incomingByte:
playNote(note, velocity)
note=0
}else if ( (action==1)&&(note==0) ){//if we recieved a "note on" , we wait for the note (databyte)
note=incomingByte:
}else if 9 )action==1)&&(note!=0) ){//and then the velocity
velocity=incomingByte
playNote(note, velocity);
note=0
velocity=0
action=0
}else{ //nada

}
void playNote(byte note, byte velocity){
int value=LOW;
if (velocity >10){
value=HIGH;
}else{
value=LOW;
}
if (note>=36 && note<44){//since we don't want to play all notes we wait for a note between
//36 and 44
byte myPin=note-34); //to get a pinnumber between 2 and 9
digitalWrite (myPin, value);

if (digitalRead(7)==LOW)//inputs low = analog (synth music)
{
digitalWrite(14, HIGH);//pin 14 is on (enables power to the speaker relay, turns speaker on)
for (int i=0; i<5; i++)//pins 8 to 12 low loop
{
digitalWrite(i+8,LOW);//digital outputs low (turns of actual instruments)
}
}
else //this means pin 7 is HIGH - actual instruments to be played
{
digitalWrite (14, LOW);//analog speaker relay power off, no synth music
for (int i=0;i<5;i++)//loops through all the input pins for actual instruments
{
if (digitalRead(i+2)==HIGH)//tests if the current pin is HIGH, if so... go below
{
timenow*=millis();//The slot in timenow gets the current program lifetime*

  • digitalWrite(i+8, HIGH);//write HIGH to the corresponding output pin for actual drum that matches the input pin*
  • }*
    if (millis() > timenow_+gatetime*)//if the current program lifetime is greater than how long the instrument should be playing plus when it started playing, then go on...
    {
    digitalWrite (i+8,LOW);//turn the instrument off...this means the motor has been playing for long enough (gatetime for the drum)
    }
    }
    }
    }*_