MIDI input works but not for all MIDI signals

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:

Coming slightly late to this discussion; the problem appears to be getting a good low level, with an optoisolator output.

I've seen a circuit that gets around that, though I didn't realise at the time. It just adds a PNP transistor stage, acting as an inverter. The signal then needs inverting again, I guess, but you could probably just do that in software.

I have a schematic, as an image (saved a while ago - I've forgotten where from). I don't see any way to attach it here though.

I expect you can picture it anyway. The emitter of the PNP transistor is connected to Vcc, the collector connects through a resistor to ground. The base is fed via a resistor from the output side of the optoisolator.

If the output of the optoisolator is off, the base of the PNP transistor is at Vcc, so the transistor is off (output low, close to 0V thanks to the collector resistor connected to ground). If the optoisolator output is on, it only needs to drop by a little over 0.6V, to turn the PNP transistor on (output high, close to Vcc - 0.2V or so). So, even a dip of 1V for a zero, should be fine, depending on the base resistor value, and the gain of the transistor.

The diagram showed a PC817 optoisolator, with a 390 Ohm series resistor on the output side, and the same value resistor as the base resistor of the PNP transistor, shown as a BC640. The collector resistor is shown as 1k5 Ohms. Almost any small PNP transistor would do, I expect, and the resistor values aren't likely to be at all critical.