MIDI input works but not for all MIDI signals

#include <avr/pgmspace.h>
#include "LUT.h"

//PIN DEFINITIONS 

#define DATAOUT 3 //MOSI, DIN Pin 15 on AD5668
#define SPICLK 4 //Serial Clock In, SCLK Pin 16
#define SLAVESELECT 5//SYNC Active Low Control Input, Pin 2
#define LDAC 6//LDAC, Pin 1, Pulse this pin low to update DAC registers, can be tied permanently low.
#define CLR 7
#define PWRLED 8
#define RXLED 9

//OUTLET DEFINITIONS FOR MIDI X0X
#define GATE 0
#define CV 1
#define ACC 2
#define SLD 3
#define FILT 4

//AD5668 Command definitions
#define WRITE 0
#define UPDATE 1
#define WRITE_UPDATE_ALL 2
#define WRITE_UPDATE_N 3
#define POWER 4
#define LOAD_CC_REG 5
#define LOAD_LDAC_REG 6
#define RESET 7
#define SETUP_INTERNAL_REGISTER 8

//MIDI VARIABLES
byte incomingByte;
int action = 0; //0 not implemented/do nothing, 1 note on, 2 note off, 3 cc, 4 sysex
int note = -1;  //use -1 to indicate the value has not yet been set, this should be changed
int velocity = -1;  //in future versions to save memory (i.e. use a char or a byte)
int extra1 = -1;
int extra2 = -1;

int RX = LOW;                // previous value of the LED
unsigned long interval = 100;           // interval to blink (milliseconds)

boolean noteOn = false;
boolean sysex = false;
unsigned int voltage1 = 13400;
unsigned int voltage2 = 26500;
unsigned int voltage3 = 39550;
unsigned int voltage4 = 52625;
long voltage5 = 65735;

void setup() {
  //set pin modes

  pinMode(DATAOUT, OUTPUT);
  pinMode(SPICLK, OUTPUT);
  pinMode(SLAVESELECT, OUTPUT);
  pinMode(LDAC, OUTPUT);
  pinMode(CLR, OUTPUT);
  pinMode(PWRLED, OUTPUT);
  pinMode(RXLED, OUTPUT);
  //disable DAC to start with
  digitalWrite(DATAOUT,LOW);
  digitalWrite(SPICLK, LOW);
  digitalWrite(SLAVESELECT, LOW);
  digitalWrite(LDAC, HIGH);
  digitalWrite(PWRLED, HIGH);
  digitalWrite(RXLED, HIGH);
  digitalWrite(CLR, LOW);
  delay(500);
  digitalWrite(CLR, HIGH);
  delay(500);
  delay(1000);
  write_dac(SETUP_INTERNAL_REGISTER, 0, 1); //set up internal register on DAC
  delay(1000);
  write_dac(POWER, 0, 0);
  delay(1000);
  write_dac(RESET, 0, 0);
  delay(1000);
  digitalWrite(RXLED, LOW);
  Serial.begin(31250); //change this when you want to graduate from usb midi to real midi
}


void write_dac(byte command, byte address, unsigned int data) {
  switch (command) {
  case WRITE_UPDATE_N:
    {
      byte b1 = B11110000|command; //padding at beginning of byte
      ////Serial.print("b1 ");
      ////Serial.println(b1, BIN);
      byte b2 = address << 4 | data >> 12; //4 address bits and 4 MSBs of data
      ////Serial.print("b2 ");
      ////Serial.println(b2, BIN);
      byte b3 = (data << 4) >> 8; // middle 8 bits of data
      ////Serial.print("b3 ");
      ////Serial.println(b3, BIN);
      byte b4 = (data << 12) >> 8 | B00001111;
      ////Serial.print("b4 ");
      ////Serial.println(b4, BIN);
      ////Serial.println();
      digitalWrite(SLAVESELECT, LOW);
      delayMicroseconds(1);
      shiftOut(DATAOUT, SPICLK, MSBFIRST, b1);
      shiftOut(DATAOUT, SPICLK, MSBFIRST, b2);
      shiftOut(DATAOUT, SPICLK, MSBFIRST, b3);
      shiftOut(DATAOUT, SPICLK, MSBFIRST, b4);
      delayMicroseconds(1);
      digitalWrite(LDAC, LOW);
      delayMicroseconds(1);
      digitalWrite(LDAC, HIGH);
      delayMicroseconds(1);
      digitalWrite(SLAVESELECT, HIGH);
      break;
    }
  case SETUP_INTERNAL_REGISTER:
    {
      byte b1 = B11111000; //padding at beginning of byte
      ////Serial.print("b1 ");
      ////Serial.println(b1, BIN);
      byte b2 = B00000000;
      ////Serial.print("b2 ");
      ////Serial.println(b2, BIN);
      byte b3 = B00000000;
      ////Serial.print("b2 ");
      ////Serial.println(b3, BIN);
      byte b4 = B00000000|data;
      ////Serial.print("b4 ");
      ////Serial.println(b4, BIN);
      ////Serial.println();
      digitalWrite(SLAVESELECT, LOW);
      delayMicroseconds(1);
      shiftOut(DATAOUT, SPICLK, MSBFIRST, b1);
      shiftOut(DATAOUT, SPICLK, MSBFIRST, b2);
      shiftOut(DATAOUT, SPICLK, MSBFIRST, b3);
      shiftOut(DATAOUT, SPICLK, MSBFIRST, b4);
      delayMicroseconds(1);
      digitalWrite(SLAVESELECT, HIGH);
      break;
    }
  case RESET:
    {
      byte b1 = B11110111; //padding at beginning of byte
      ////Serial.print("b1 ");
      ////Serial.println(b1, BIN);
      byte b2 = B00000000;
      ////Serial.print("b2 ");
      ////Serial.println(b2, BIN);
      byte b3 = B00000000;
      ////Serial.print("b2 ");
      ////Serial.println(b3, BIN);
      byte b4 = B00000000|data;
      ////Serial.print("b4 ");
      ////Serial.println(b4, BIN);
      ////Serial.println();
      digitalWrite(SLAVESELECT, LOW);
      delayMicroseconds(1);
      shiftOut(DATAOUT, SPICLK, MSBFIRST, b1);
      shiftOut(DATAOUT, SPICLK, MSBFIRST, b2);
      shiftOut(DATAOUT, SPICLK, MSBFIRST, b3);
      shiftOut(DATAOUT, SPICLK, MSBFIRST, b4);
      delayMicroseconds(1);
      digitalWrite(SLAVESELECT, HIGH);
      break;
    }
  case POWER:
    {
      byte b1 = B11110100; //padding at beginning of byte
      ////Serial.print("b1 ");
      ////Serial.println(b1, BIN);
      byte b2 = B00000000;
      ////Serial.print("b2 ");
      ////Serial.println(b2, BIN);
      byte b3 = B00000000;
      ////Serial.print("b2 ");
      ////Serial.println(b3, BIN);
      byte b4 = B11111111;
      ////Serial.print("b4 ");
      ////Serial.println(b4, BIN);
      ////Serial.println();
      digitalWrite(SLAVESELECT, LOW);
      delayMicroseconds(1);
      shiftOut(DATAOUT, SPICLK, MSBFIRST, b1);
      shiftOut(DATAOUT, SPICLK, MSBFIRST, b2);
      shiftOut(DATAOUT, SPICLK, MSBFIRST, b3);
      shiftOut(DATAOUT, SPICLK, MSBFIRST, b4);
      delayMicroseconds(1);
      digitalWrite(SLAVESELECT, HIGH);
      break;
    }
  }
}
void playNote(int note_, int velocity) {
  unsigned int note = note_;
  unsigned int voltage = 0;
  if ((note >= 12) && (note < 24)) {
    ////Serial.println("note >= 12 && note < 24");
    voltage = (voltage1)*((note - 12)/12.0);
  } 
  else if ((note >= 24) && (note < 36)) {
    ////Serial.println("note >= 24 && note < 36");
    voltage = (voltage2 - voltage1)*((note - 24)/12.0) + voltage1;
  } 
  else if ((note >= 36) && (note < 48)) {
    ////Serial.println("note >= 36 && note < 48");
    voltage = (voltage3 - voltage2)*((note - 36)/12.0) + voltage2;
  } 
  else if ((note >= 48) && (note < 60)) {
    ////Serial.println("note >= 48 && note < 60");
    voltage = (voltage4 - voltage3)*((note - 48)/12.0) + voltage3;
  } 
  else if ((note >= 60) && (note < 72)) {
    ////Serial.println("note >= 60 && note < 72");
    voltage = (voltage5 - voltage4)*((note - 60)/12.0) + voltage4;
  }
  noteOn = true;
  /*
  if ((velocity >= 1) && (velocity < 64)) { //normal note
    write_dac(WRITE_UPDATE_N, ACC, 0);
    write_dac(WRITE_UPDATE_N, CV, voltage);
    write_dac(WRITE_UPDATE_N, GATE, 65535);
    //Serial.print("cv = ");
    //Serial.print(voltage);
    //Serial.println(", accent = OFF");
  } 
  else if (velocity >= 64) { //accent 
    write_dac(WRITE_UPDATE_N, ACC, 65535);
    write_dac(WRITE_UPDATE_N, CV, voltage);
    write_dac(WRITE_UPDATE_N, GATE, 65535);
    //Serial.print("cv = ");
    //Serial.print(voltage);
    //Serial.println(", accent = ON");
  }
  */
  write_dac(WRITE_UPDATE_N, CV, voltage);
  write_dac(WRITE_UPDATE_N, GATE, 65535);
}

void stopNote(int note, int velocity) {
  //Serial.println("Note off, GATE = 0");
  write_dac(WRITE_UPDATE_N, GATE, 0);
  noteOn = false;
}

void cc(int note_, int velocity_) {
  write_dac(SETUP_INTERNAL_REGISTER, 0, 1); 
  unsigned int note = note_;
  unsigned int velocity = velocity_; //you wouldn't have to convert the ints if you used
                                     //something other than -1 to indicate they were empty,
                                     //just keep them as bytes through out and use 128 as
                                     //empty for MSB and LSB data byte
  if (note == 1) {
    if (velocity >= 64) {
      write_dac(SETUP_INTERNAL_REGISTER, 0, 1);
      //Serial.println("INTERNAL REGISTER ON");
    } 
    else {
      write_dac(SETUP_INTERNAL_REGISTER, 0, 0);
      //Serial.println("INTERNAL REGISTER OFF");
    }
  } else if (note == 12) {
    unsigned int voltage = 65535*velocity/127.0;
    write_dac(WRITE_UPDATE_N, 2, voltage);
  }
  else if (note == 13) {
    unsigned int voltage = 65535*velocity/127.0;
    write_dac(WRITE_UPDATE_N, 3, voltage);
    //Serial.print("OUTPUT 3 = ");
    //Serial.println(voltage);
  } 
  else if (note == 14) {
    unsigned int voltage = 65535*velocity/127.0;
    write_dac(WRITE_UPDATE_N, 4, voltage);
    //Serial.print("OUTPUT 4 = ");
    //Serial.println(voltage);
  } 
  else if (note == 20) {
    unsigned int voltage = 65535*velocity/127.0;
    write_dac(WRITE_UPDATE_N, 5, voltage);
    //Serial.print("OUTPUT 5 = ");
    //Serial.println(voltage);
  } 
  else if (note == 21) {
    unsigned int voltage = 65535*velocity/127.0;
    write_dac(WRITE_UPDATE_N, 6, voltage);
    //Serial.print("OUTPUT 6 = ");
    //Serial.println(voltage);
  } 
  else if (note == 22) {
    unsigned int voltage = 65535*velocity/127.0;
    write_dac(WRITE_UPDATE_N, 7, voltage);
    //Serial.print("OUTPUT 7 = ");
    //Serial.println(voltage);
  }
}

void loop() {
  if (Serial.available() > 0) {
    // read the incoming byte:
    incomingByte = Serial.read();
    if ((incomingByte == 144) &! sysex) {
      //incoming note on
      action = 1;
    } 
    else if ((incomingByte == 128) &! sysex) {
      //incoming note off
      action = 2;
    } 
    else if ((incomingByte == 176) &! sysex) {
      action = 3;
    } 
    else if ((incomingByte == 240) &! sysex) {
      action = 4;
      sysex = true;
      //Serial.println("incoming sysex");
    } 
    else if ((incomingByte == 247) && sysex) {
      sysex = false;
      //Serial.println("sysex finished");
    } 
    else if ((action == 1) && (note == -1)) {
      note = incomingByte;
    } 
    else if ((action == 1) && (note != -1)) {
      velocity = incomingByte;
      //Serial.print("note on 144 ");
      //Serial.print(note, DEC);
      //Serial.print(" ");
      //Serial.println(velocity, DEC);
      if (velocity == 0) {
        stopNote(note,velocity);
      } 
      else {
        playNote(note,velocity);
      }
      action = 0;
      note = -1;
      velocity = -1;
    } 
    else if ((action == 2) && (note == -1)) {
      note = incomingByte;
    } 
    else if ((action == 2) && (note != -1)) {
      velocity = incomingByte;
      //Serial.print("note off 128 ");
      //Serial.print(note, DEC);
      //Serial.print(" ");
      //Serial.println(velocity, DEC);
      stopNote(note,velocity);
      action = 0;
      note = -1;
      velocity = -1;
    } 
    else if ((action == 3) && (note == -1)) {
      note = incomingByte;
    } 
    else if ((action == 3) && (note != -1)) {
      velocity = incomingByte;
      //Serial.print("cc ");
      //Serial.print(note, DEC);
      //Serial.print(" ");
      //Serial.println(velocity, DEC);
      cc(note,velocity);
      action = 0;
      note = -1;
      velocity = -1;
    } 
    else if ((action == 4) && (note == -1)) {
      note = incomingByte;
    } 
    else if ((action == 4) && (note != -1) && (velocity == -1)) {
      velocity = incomingByte;
    } 
    else if ((action == 4) && (note != -1) && (velocity != -1) && (extra1 == -1)) {
      extra1 = incomingByte;
    } 
    else if ((action == 4) && (note != -1) && (velocity != -1) && (extra1 != -1) && (extra2 == -1)) {
      extra2 = incomingByte;
      //Serial.print("sysex: ");
      //Serial.print(note);
      //Serial.print(" ");
      //Serial.print(velocity);
      //Serial.print(" ");
      //Serial.print(extra1);
      //Serial.print(" ");
      //Serial.println(extra2);
      switch (note) {
      case 1: 
        voltage1 = velocity*512 + extra1*4 + extra2;
        //Serial.print("voltage1 = ");
        //Serial.println(voltage1);
        break;
      case 2:
        voltage2 = velocity*512 + extra1*4 + extra2;
        //Serial.print("voltage2 = ");
        //Serial.println(voltage2);
        break;
      case 3: 
        voltage3 = velocity*512 + extra1*4 + extra2;
        //Serial.print("voltage3 = ");
        //Serial.println(voltage3);
        break;
      case 4:
        voltage4 = velocity*512 + extra1*4 + extra2;
        //Serial.print("voltage4 = ");
        //Serial.println(voltage4);
        break;
      case 5:
        voltage5 = 65535 + velocity*512 + extra1*4 + extra2;
        //Serial.print("voltage5 = ");
        //Serial.println(voltage5);
        break;
      }
      action = 0;
      note = -1;
      velocity = -1;
      extra1 = -1;
      extra2 = -1;
    }
  }
  if (noteOn) {
    RX = HIGH;
  } 
  else {
    RX = LOW;
  }
  digitalWrite(RXLED, RX);
}
      digitalWrite(SLAVESELECT, LOW);
      delayMicroseconds(1);
      shiftOut(DATAOUT, SPICLK, MSBFIRST, b1);
      shiftOut(DATAOUT, SPICLK, MSBFIRST, b2);
      shiftOut(DATAOUT, SPICLK, MSBFIRST, b3);
      shiftOut(DATAOUT, SPICLK, MSBFIRST, b4);
      delayMicroseconds(1);
      digitalWrite(LDAC, LOW);
      delayMicroseconds(1);
      digitalWrite(LDAC, HIGH);
      delayMicroseconds(1);
      digitalWrite(SLAVESELECT, HIGH);

Why not use SPI.transfer() rather than all these shifts and delays?

crx091081gb:
I made this MIDI to CV box and for years it worked just fine receiving MIDI from my computer's M4UXL USB controlled 4 in / 4 out MIDI box. Then I decided to go back to my roots and use my Atari and the damn thing doesn't work with it, every fourth or fifth MIDI note seems to get through but the rest don't.

Now here's a funny thing. I was playing with a sketch that reads MIDI messages, here:

http://arduino.cc/forum/index.php/topic,100382.0.html

And it worked absolutely fine on one device (a Korg synth) but quite badly on another one (a Roland keyboard). Pretty much the same symptoms - some notes got through, a lot didn't.

I'm going to order the improved optocoupler chip in the next couple of days to see if that fixes it.

It was written along time ago and either the SPI library wasn't there or more likely I didn't know about it, that part of the code has worked solidly these last years so I never saw any need to fiddle with it. Let us know if the better class of opto fixes matters for you. I have to wait till my list of desired components grows to the right amount to get free shipping from Farnell. :0

RuggedCircuits:
Yes. Here are the suggested schematics from the MIDI Manufacturer's Association (which suggests 280 ohms):

I got some 4N35 today (100% CTR). Still with 330 ohms the results aren't very good:

I got better results with a 1K resistor, but I don't totally like the rounded edges:

Anyway, it turns out that the problems I had with the other device were software more than hardware. :slight_smile:

Despite all the documentation about MIDI, it still leaves a bit to be desired when you want to interpret the protocol at the low level.

I am really surprised the 100% CTR devices didn't get the output down lower. Can you confirm the current coming in on the input (LED) side? Is it at least 5mA?

--
The Aussie Shield: breakout all 28 pins to quick-connect terminals

I also got a few SFH618A-4X. With 330 ohms:

They claim to have 160 to 320% CTR at 1 mA.

RuggedCircuits:
I am really surprised the 100% CTR devices didn't get the output down lower. Can you confirm the current coming in on the input (LED) side? Is it at least 5mA?

With my meter in circuit with the wire from the MIDI, I am measuring an average of 0.12 mA DC.

(edit) Maximum of about 0.48 mA

Measuring DC current won't be so helpful since there will be no current flowing when there is no transmission coming in. And when there is, the current will be oscillating and your meter will be confused. Putting an in-line series resistor (10 ohms?) and using differential measuring mode on a scope will give a clearer picture.

The SFH618A-4X certainly gives nicer results, but has a slower-than-expected turn-off (the high bits should really be squarer). I'm also a bit confused about the timing. Looking at the reception that starts at almost exactly 250us into the data record (falling edge), I would have expected it to be over in 10 bit periods (start bit + 8 data bits + stop bit), which at 31250 bps would be 320 microseconds, or 6.4 divisions of 50us on the scope. Well, it looks like another transmission is coming in much earlier than that, as there's a falling edge just about 5.8 divisions later. Could the MIDI baud rate be wrong?

--
The QuadRAM shield: add 512 kilobytes of external RAM to your Arduino Mega/Mega2560

You might not have chosen the start of a byte. The logic analyzer appears to confirm things are OK:

From the start of one byte to the next is 319.75 uS.

RuggedCircuits:
Measuring DC current won't be so helpful since there will be no current flowing when there is no transmission coming in. And when there is, the current will be oscillating and your meter will be confused. Putting an in-line series resistor (10 ohms?) and using differential measuring mode on a scope will give a clearer picture.

OK, then.

That's with 10 ohms, and since that side didn't have an earth reference I just clipped the ground to one side and the probe to the other. I calculate:

current = .080 / 10 = 8 mA

Is that right? So that looks OK.

Everything looks right then. In hindsight 100% CTR isn't really good enough since 8mA*330ohms = 2.64V of voltage drop, down from 5V means that the signal is only expected to go down to 5V-2.64=2.36V, and you observed it going down to 2.0V with the 4N35. So either a slightly bigger resistor (500 ohms should get down to 1V) or a higher CTR isolator (like your SFH618A-4X) would be the way to go for greater robustness.

--
The MegaRAM shield: add 128 kilobytes of external RAM to your Arduino Mega/Mega2560

It's always gratifying when theory and observation meet up, is it not?

Back to the 4N35 and a 560 ohm resistor gives this:

Pretty reasonable wave-form and it comes down to 200 mV.

I'll amend my suggested circuit to incorporate that.

That's a good looking waveform! Hopefully the 4N35 you have is representative of the norm and lot-to-lot variations (and manufacturer-to-manufacturer variation) won't affect things too much.

--
The Rugged Circuits Yellowjacket: 802.11 WiFi module with ATmega328P microcontroller, only 1.6" x 1.2", bootloader

Nick were your problems anything to do with running status? I've been thinking perhaps my code isn't handling this..

http://home.roadrunner.com/~jgglatt/tech/midispec/run.htm

Yes that was exactly it. I was getting multiple bytes with the low-order bit set. I guessed, and tests confirmed, that I was getting multiple note-ons in a row. And to save switching to note off, they were sending note-on with a velocity of zero.

Just confirmed it was my crappy implementation of running status that was responsible. I'm surprised this doesn't come up more often. I'm just surprised that my modern DAW doesn't take advantage of running status to try and keep the MIDI bus as free as possible but then if modern computers could do MIDI with sub millisecond accuracy then I wouldn't have to use an Atari in the first place.

Ho hum.

Ah well, in chasing the hardware bug we both learned something about both hardware and software. So, that was time well spent. :slight_smile:

I think so. :slight_smile: