Strange MIDI issues while using very basic two button sketch....plz help!!!

I’ve been trying to hack together a fairly basic multibutton midi controller using an Uno. For now I’m just using two basic push buttons and am just trying to turn guitar pedals on and off in FL Studio for testing. I’m hoping someone more experienced could scan my code and make some suggestions on how to improve it. I suspect there is some silly error that I’m not experienced enough to see which is causing it to misbehave.

When I couldn’t get it going properly, I studied the midi spec and started adding in all the parts that most people don’t seem to ever need. First I realized I didn’t have the 220 resistors for the midi out. Until I added those I didn’t get any data at all but then all I got was a steady stream of gibberish in midi ox. So then I added a 7404 hex inverter. My code works mostly as it is supposed to when I use a 7404 but only when I don’t ground the chip or the unused pins! It really shouldn’t work at all like this but it does. If I ground the chip it stops working. So then I added a 2n3904 buffer after the chip and it works for a button push or two but then quits. I also get an extra command or two that shouldn’t be present. It’s very strange behavior and I’m wondering if anyone else has seen these issues or knows what might be causing it. Most of the sketches I’ve looked at seem to work without ever needing a 7404 or a buffer. This is why I think my code must be jacked but it does sometimes work! Very confusing…

If anyone here has messed around with midi buttons please have a look and see if you see an easy fix. It’s a very basic sketch but I’m just a hacker not a pro. Thanks!! :slight_smile:

#include <MIDI.h>

#define LEDA 13
#define LEDB 12

MIDI_CREATE_DEFAULT_INSTANCE();

const int buttonPinA = 7;
const int buttonPinB = 6;
int buttonStateA = 0;
int buttonStateB = 0;
int lastButtonStateA = 0;
int lastButtonStateB = 0;
int toggleStateA = 0;
int toggleStateB = 0;

void setup() {
  MIDI.begin(MIDI_CHANNEL_OFF);
  pinMode(buttonPinA, INPUT);
  pinMode(LEDA, OUTPUT);
  pinMode(buttonPinB, INPUT);
  pinMode(LEDB, OUTPUT);
}

void loop() {
  buttonStateA = digitalRead(buttonPinA);
  buttonStateB = digitalRead(buttonPinB);

    // check whether the input is HIGH (button pressed)
  if (buttonStateA == HIGH){
    digitalWrite(LEDA,HIGH); // turn LED on
  }else{
    digitalWrite(LEDA,LOW);
  }
 
  if (buttonStateA != lastButtonStateA && buttonStateA == 1 && toggleStateA == 0) {
    MIDI.sendControlChange(1,127,1);
    delay(50);
  toggleStateA = 1;
  }
  else if (buttonStateA != lastButtonStateA && buttonStateA == 1 && toggleStateA == 1) {
    MIDI.sendControlChange(1,0,1);
    delay(50);
  toggleStateA = 0;
  }
    lastButtonStateA = buttonStateA;

    // check whether the input is HIGH (button pressed)
  if (buttonStateB == HIGH){
    digitalWrite(LEDB,HIGH); // turn LED on
  }else{
    digitalWrite(LEDB,LOW);
  }

  if (buttonStateB != lastButtonStateB && buttonStateB == 1 && toggleStateB == 0) {
    MIDI.sendControlChange(2,127,1);
    delay(50);
  toggleStateB = 1;
  }
  else if (buttonStateB != lastButtonStateB && buttonStateB == 1 && toggleStateB == 1) {
    MIDI.sendControlChange(2,0,1);
    delay(50);
  toggleStateB = 0;
  }
    lastButtonStateB = buttonStateB;
}

Here is a picture of the chip and buffer. The 220’s are soldered on to the pins of the jack. The TX wire does go to pin one on the chip but it is hard to see.

There are no spaces in the digital write (LEDA,HIGH) and so on.

Should be (LEDA, HIGH)

kidamarie:
There are no spaces in the digital write (LEDA,HIGH) and so on.

Should be (LEDA, HIGH)

No, you can have spaces here it makes no odds.

A schimitic would be good to help untangle that mess. Having no resistor could have damaged the receiving MIDI device. However from the description it sounds like you have missed a ground connection and something is floating.

Grumpy_Mike: No, you can have spaces here it makes no odds.

A schimitic would be good to help untangle that mess. Having no resistor could have damaged the receiving MIDI device. However from the description it sounds like you have missed a ground connection and something is floating.

Thanks for the reply. We went and tried everything we could think of to explore a ground issue. Finally we decided we should try to recreate someone elses work as exactly as possible to see why other people seem to have no issues. We went with the "notes and volts" push button project and it also didn't work until we used a second usb like he does. He has his project powered by a USB cable and then connects his midi jack via USB as well. So two USB connections to the same computer. We found that powering from any other USB source DOES NOT WORK. The only way we can achieve a stable MIDI sketch that does what it is supposed to do is by powering through a second USB cable to the same computer. Up until now we were using a wall power adapter for arduino power. Also interesting is that once we powered with USB we no longer needed a 7404 or a buffer. I'm assuming this is some sort of ground issue from introducing a separate power source but I don't know. I wish I had seen this mentioned somewhere but I have never noticed it before. FWIW we also tried a 9V battery to the power port and that doesn't work either. We have now confirmed that our own sketch works fine but Dear God I think my brain is scrambled now. I have wasted so many hours troubleshooting this. :(

You are not there yet

So two USB connections to the same computer. We found that powering from any other USB source DOES NOT WORK

Sorry but that is total rubbish. You still have something wrong but what is hard to say as you will not cooperate with us.

Hi,

Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png? Including how you connect your various USB and power supplies.

Thanks.. Tom.... :)