'midi in" project....

The keyboard glitches were with my keyboard. If anyone else is experiencing similar problems, try turnig off the internal clock in your synth.

I have been trying to modify this sketch by adding an additional cv out that works just like the pitch but coresponds to the mod wheel or an other assigned control and is used to modulate the filter, LFO or whatever on a Modular synth.
Here is what i have:

int gatePin = 4;
int cvPin = 5;
int modwheel = 3; // **declare mod wheel pin  

int statusLED = 13;
int midiEnable = 2;
byte notes[] = {0,4,9,13,17,21,26,30,34,38,43,47,
  51,55,60,64,68,72,77,81,85,89,94,98,
  102,106,111,115,119,123,128,132,136,140,145,149,
  153,157,162,166,170,174,179,183,187,191,196,200,
  204,208,213,217,221,225,230,234,238,242,247,251,255};

byte mods[] = {0,4,9,13,17,21,26,30,34,38,43,47,       //**modulation steps
  51,55,60,64,68,72,77,81,85,89,94,98,
  102,106,111,115,119,123,128,132,136,140,145,149,
  153,157,162,166,170,174,179,183,187,191,196,200,
  204,208,213,217,221,225,230,234,238,242,247,251,255};

 

int tempo = 1;

void setup() {
pinMode(gatePin, OUTPUT);
pinMode(cvPin, OUTPUT);
pinMode(modwheel, OUTPUT);   //** mod wheel as output
pinMode(statusLED, OUTPUT);
pinMode(midiEnable, OUTPUT);
Serial.begin(31250);
digitalWrite(midiEnable, HIGH);
}

void loop() {
readMIDI();
}


// play a note out to cv
void note(byte note, byte velocity){ 
analogWrite(cvPin, notes[note-9]);
if(velocity == 0) {
  digitalWrite(gatePin, LOW);
} else {
  digitalWrite(gatePin, HIGH);
}
}

void mod (byte mod, byte velocity) {  //** modulate function
analogWrite(modwheel, mods[mod-9]);   //** modulate function
}

void blink(int repeat) {
for(int i=0; i<repeat; i++) {
  digitalWrite(statusLED, HIGH);
  delay(5);
  digitalWrite(statusLED, LOW);
  delay(1);
}
}

void readMIDI() {
byte incomingMessage[] = {0,0,0};
byte incomingByte = 0;

if(Serial.available() > 0) {
  incomingByte = Serial.read();
  delay(3);   
  if(incomingByte >= 240) {
    readSystem(incomingByte);
  } else {
    incomingMessage[0] = incomingByte;
  }
  if(Serial.available() > 0) {
    incomingByte = Serial.read();
    if(incomingByte >= 240) {
      readSystem(incomingByte);
    } else {
      incomingMessage[1] = incomingByte;
    }
    if(Serial.available() > 0) {
      incomingByte = Serial.read();
      if(incomingByte >= 240) {
        readSystem(incomingByte);
      } else {
        incomingMessage[2] = incomingByte;
      }
    }
  }
}

processMIDI(incomingMessage);
}

void readSystem(byte incomingByte) {
byte incomingMessage[] = {0,0,0};


incomingMessage[0] = incomingByte;
if(Serial.available() > 0) {
  incomingMessage[1] = Serial.read();
  if(Serial.available() > 0) {
    incomingMessage[2] = Serial.read();
  }
}

processMIDI(incomingMessage);
}

void processMIDI(byte incomingMessage[]) {
switch(incomingMessage[0]) {
  case B10010000:  // Note on, channel 1
    note(incomingMessage[1], incomingMessage[2]);
    break;
  case B10000000:  // Note off, channel 1
    note(incomingMessage[1], 0);
    break;
   case B00100001:  //**mod wheel
     mod (incomingMessage[1], incomingMessage[2]); 
     break;
  
  case B11111000:  // Time code
    tempo++;
    if(tempo == 24) {
      blink(1);
      tempo = 0;
    }
    break;
}
}

My Additions are noted with **. So as you can see... not very original. Its really just a guess. I am trying to copy the pitch cv functons but have it correspond to the mod wheel. I was thinking that I only have to add a few lines to the MIDI processing to read the mod wheel and then another, above, to calculate the modulation steps and send pulses out.
Is the the correct approach?
It doesent seem to work, but there could be any number of problems. I thought that the mod" void mod" commands should have been with void note, but it wouldnt let be declare another variable (byte mod) in the scope. Also I am not sure if I have the right mod wheel message listed in the MIDI processing. 01 is the Mod wheel for midi in, but is different for out... or (more likely) am I totally wrong and need to take another approach