Arduino Midi In via MCP-4822 to Korg Monotron Pitc

Hello, trying to provide Pitch values to
an MPC-4822 DAC via Arduino Midi-Note in's
The Pitch is sended, i hear from the Monotron,
but it gets short notes, help me out please.
Here is the Scetch

#define DATAOUT 11//MOSI - serial data input
#define DATAIN 12//MISO - not used, but part of builtin SPI
#define SPICLOCK  13//sck - serial clock input
#define SLAVESELECT0 10//ss for 1st DAC - chip select input for 1st DAC
//#define SLAVESELECT1 9//ss for 2nd DAC  - chip select input for 2nd DAC

#include "Spi.h"
byte incomingByte;
byte note;
byte velocity;
const int kGatePin = 9;
int action=2;

void setup() {
  Serial.begin(31250);
pinMode(kGatePin, OUTPUT);
pinMode(2, OUTPUT);     
  pinMode(3, OUTPUT);  
  digitalWrite(3, LOW); // GND 0 Volt supply to opto-coupler
  digitalWrite(2, HIGH);
Serial.begin(31250); 
}  
void sendNote(int key) {
int pitch = 0xa00L * key / 12;

digitalWrite(SS_PIN, LOW);
Spi.transfer(0x10 + (pitch >> 8));
Spi.transfer(pitch & 0xff);
digitalWrite(SS_PIN, HIGH);

}

void loop() {
if (Serial.available() > 0) {
    // read the incoming byte:
    incomingByte = Serial.read();

    // wait for as status-byte, channel 1, note on or off
    if (incomingByte== 144){ // note on message starting starting
      action=1;
    }else if (incomingByte== 128){ // note off message starting
      action=0;
    }else if (incomingByte== 208){ // aftertouch message starting
       //not implemented yet
    }else if (incomingByte== 160){ // polypressure message starting
       //not implemented yet
    }else if ( (action==0)&&(note==0) ){ // if we received a "note off", we wait for which note (databyte)
      note=incomingByte;
      playNote(note, 0);
      note=0;
      velocity=0;
      action=2;
    }else if ( (action==1)&&(note==0) ){ // if we received a "note on", we wait for the note (databyte)
      note=incomingByte;
    }else if ( (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){
  if (velocity >10){
      digitalWrite(kGatePin, HIGH);
  }else{
   digitalWrite(kGatePin, LOW);
  }

 //since we don't want to "play" all notes we wait for a note between 36 & 44
 if(note>=36 && note<53){
   byte key=note-36; // to get a pinnumber between 2 and 9
sendNote(key);
 }
}

//since we don't want to "play" all notes we wait for a note between 36 & 44
if(note>=36 && note<53){

Rule 1: in case of conflict between code and comment the code is allways right

May not be the cause but it was the first thing noticed.


void setup() {
  [glow]Serial.begin(31250);[/glow]
pinMode(kGatePin, OUTPUT);
pinMode(2, OUTPUT);    
  pinMode(3, OUTPUT);  
  digitalWrite(3, LOW); // GND 0 Volt supply to opto-coupler
  digitalWrite(2, HIGH);
[glow]Serial.begin(31250);  Why two times?[/glow]
}

int pitch = 0xa00L * key / 12;
think pitch overflows as an int is only 16 bits. 0xA00L is 32 bit ; times key is even bigger divides by 12 is 32 bit. mapping it on an 16 bit will loose all significant bit rendering it effectively zero, could explain really short notes. Can you print pitch?

Good evening,
Spi.transfer(0x10 + (pitch >> 8));
Spi.transfer(pitch & 0xff);
Seems to be okay, from the docs i read it looks like
it sends 16 bits, but i do not know for shure.
I think the problem is the code itself, the moment
the kGatepin is triggered LOW, but also for that counts
i am not shure.
I keep tinkening. Let you know when i get the clou,
and......if you have a Monotron and a Arduino, you can
play it with a normaal Midi-keyboard, seems fun. fun.
Arpeggio