I'm working on a midi device with a pro micro, it's reading as a midi device, but not outputting any notes, here's my program: const int button1 = 2; const int button2 = 3; const int button3 = 4; const int button4 = 5; const int button5 = 6; const int button6 = 7;
int note = 0;
const int c3 = 36;
const int d3 = 38;
const int e3 = 40;
const int f3 = 41;
const int g3 = 43;
const int a3 = 45;
void setup() {
pinMode(button1,INPUT);
pinMode(button2,INPUT);
pinMode(button3,INPUT);
pinMode(button4,INPUT);
pinMode(button5,INPUT);
pinMode(button6,INPUT);
Serial.begin(31250);
}
void loop() {
int button1State = digitalRead(button1);
int button2State = digitalRead(button2);
int button3State = digitalRead(button3);
int button4State = digitalRead(button4);
int button5State = digitalRead(button5);
int button6State = digitalRead(button6);
if (button1State == HIGH) {
note = c3;
noteOn(0x90, note, 0x64);
}
if (button1State == LOW) {
note = c3;
noteOn(0x90, note, 0x00);
}
if (button2State == HIGH) {
note = d3;
noteOn(0x90, note, 0x64);
}
if (button2State == LOW) {
note = d3;
noteOn(0x90, note, 0x00);
}
if (button3State == HIGH) {
note = e3;
noteOn(0x90, note, 0x64);
}
if (button3State == LOW ) {
note = e3;
noteOn(0x90, note, 0x00);
}
if (button4State == HIGH) {
note = f3;
noteOn(0x90, note, 0x64);
}
if (button4State == LOW) {
note = f3;
noteOn(0x90, note, 0x00);
}
if (button5State == HIGH) {
note = g3;
noteOn(0x90, note, 0x64);
}
if (button5State == LOW) {
note = g3;
noteOn(0x90, note, 0x00);
}
if (button6State == HIGH) {
note = a3;
noteOn(0x90, note, 0x64);
}
if (button6State == LOW ) {
note = a3;
noteOn(0x90, note, 0x00);
}
}
void noteOn(int cmd, int pitch, int velocity) {
Serial.write(cmd);
Serial.write(pitch);
Serial.write(velocity);
}
2 Answers 2
You are handling your buttons in completely the wrong way. You are basically saying:
While I press this button send a constant stream of NOTE ON commands. Otherwise send a constant stream of NOTE OFF commands.
At the same time, send more NOTE OFF commands because the other buttons aren't pressed.
So you can see it's going to be a bit of a mess.
Instead you need to detect changes in the state of a button.
For example:
void loop() {
static bool oldButton1 = LOW;
bool button1State = digitalRead(button1);
if (button1State != oldButton1) { // It changed from last time
oldButton1 = button1State; // Remember it for next time
if (button1State) { // Pressed
noteOn(0x90, c3, 0x64);
} else { // Released
noteOn(0x80, c3, 0x00); // Note: 0x80 is NOTE OFF, not 0x90.
}
}
... etc for the other buttons ...
}
Check:
- If your serial properties are ok (baudrate 31250, 8 bits, 1 stop bit)
- Check if noteOn is really called (print out the values which are sent)
- Show your MIDI circuit, assuming you think no MIDI notes are send because you have a MIDI connector attached.
- Use a protocol analyzer if you have one to check the MIDI signals sent
- Check the components of your circuit, e.g. if you use an opto-coupler, what types is it? some are too slow for MIDI
- Do not use virtual serial ports, I tried with MIDI and they give bad results (checked on Uno).