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!!
#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;
}