Go Down

Topic: Output MIDI from For Loop (Read 648 times) previous topic - next topic

technoducky

Nov 05, 2011, 07:48 pm Last Edit: Nov 05, 2011, 09:35 pm by technoducky Reason: 1
I have a project that uses digital switches to output MIDI, I used a very long piece of code to do the note output part, it was basically this for all of the notes:
Code: [Select]
if(digitalRead(20)==LOW){
     if(note50prev == 0){
       MIDI.sendNoteOn(50, velocity, MIDIchan);
       note50prev = 1;
     }
   } else {
     MIDI.sendNoteOff(50, 0, MIDIchan);
     note50prev = 0;
   }

But I want to do this using a for loop, so this is my new code, now I get all of the control changes, but no notes through MIDI:
Code: [Select]
#include <MIDI.h>

#define MIDIchan 1
#define PBzero 200
int velocity;

int noteCount = 5;
int notes[] = {50, 55, 60, 65, 70};
int pins[]  = {40, 41, 42, 43, 44};
int nPrev[] = {0, 0, 0, 0, 0};

void setup() {
 MIDI.begin(4);
 pinMode(40, INPUT);
 pinMode(41, INPUT);
 pinMode(42, INPUT);
 pinMode(43, INPUT);
 pinMode(44, INPUT);
 digitalWrite(40, HIGH);
 digitalWrite(41, HIGH);
 digitalWrite(42, HIGH);
 digitalWrite(43, HIGH);
 digitalWrite(44, HIGH);
}

void loop() {
 getVelocity();
 getPitchBend();
 getModulation();
 getNotes();
}

void getVelocity() {
 int vel = analogRead(0);
 velocity = vel / 8;
}

void getPitchBend() {
 int pb = analogRead(1);
 int pitchbend = (pb - 512) * 16;
 if(-PBzero < pitchbend && pitchbend < PBzero) {
   pitchbend = 0;
 }
 MIDI.sendPitchBend(pitchbend, MIDIchan);
}

void getModulation() {
 int mod = analogRead(2);
 int modulation = mod / 8;
 MIDI.sendControlChange(1, modulation, MIDIchan);
}

void getNotes() {
 for(int thisNote = 0; thisNote < noteCount; thisNote++)
 {
   int prevVal = nPrev[thisNote];
   int noteVal = notes[thisNote];
   int notePin = pins[thisNote];
   if(digitalRead(notePin)==LOW){
     if(prevVal == 0){
       MIDI.sendNoteOn(noteVal, velocity, MIDIchan);
       nPrev[thisNote] = 1;
     }
   } else {
     MIDI.sendNoteOff(noteVal, 0, MIDIchan);
     nPrev[thisNote] = 0;
   }
 }
}


Have tried the code in a simple sketch using Serial to check if the buttons are working properly and the code still works in the for loop, that worked, also changed the note to the same note to see if it was a problem with the MIDI library, it wasn't.

Any help would be greatly appreciated.

Grumpy_Mike

I don't see any recursion in this sketch, where is it.
You have several variable definitions inside a for loop. That is wrong.

Grumpy_Mike

Code: [Select]
    int prevVal = nPrev[thisNote];
    int noteVal = notes[thisNote];
    int notePin = pins[thisNote];

This bit is totally redundant, just use the variables from the array.
If it works with serial print it works with sending MIDI so it must be the numbers that are wrong.

Grumpy_Mike

But you had that in the original post didn't you?

Go Up