I am currently attempting to create a midi controller with my Arduino UNO which will take inputs from push buttons beneath giant piano keys and play different notes for each button. I am using a usb-midi converter which i purchased on Amazon to send the serial data to the computer, and using Ableton Live to interpret the data and play notes. Currently I am running into issues, and I am not sure whether they are due to my code, or the Live software. Live currently has trouble recognizing the signals, and often will play two notes at once when I press only one button. Another problem I run into is that the notes will often transfer both when the button is pushed and released. Ideally it will only transmit when the button is initially pushed. Can anybody suggest any improvements to my code, or perhaps suggest a good alternative to Ableton live? Below is my code, and attached is a screenshot of the midi signals my computer receives when each key is pressed.
const int switchPin = 5;
const int switchPin1 = 9;
const int switchPin2 = 10;
const int LEDpin = 13;
// Variables:
void setup() {
pinMode(switchPin, INPUT);
pinMode(switchPin1, INPUT);
pinMode(LEDpin, OUTPUT);
Serial.begin(31250);
}
int keyPush = 0;
int keyPush1 = 0;
int keyPush2 = 0;
int loopMaxOutput = 50;
void loop() {
//KEY 1
//If key is pressed, play note once
if (digitalRead(switchPin) == 1 && keyPush == 0) {
//Loop sends the noteOn function multiple times. When sent only once, the computer did not register it.
for(int i = 0; i < loopMaxOutput; i++)
{
noteOn(0x90, 40, 0x60);
}
keyPush = 1;
}
//KEY 2
if (digitalRead(switchPin1) == 1 && keyPush1 == 0) {
for(int i = 0; i < loopMaxOutput; i++)
{
noteOn(0x90, 50, 0x60);
}
keyPush1 = 1;
}
//KEY 3
if (digitalRead(switchPin2) == 1 && keyPush2 == 0) {
for(int i = 0; i < loopMaxOutput; i++)
{
noteOn(0x90, 60, 0x60);
}
keyPush2 = 1;
}
if(digitalRead(switchPin) == 0 && keyPush == 1) {
keyPush = 0;
}
if(digitalRead(switchPin1) == 0 && keyPush1 == 1) {
keyPush1 = 0;
}
if(digitalRead(switchPin2) == 0 && keyPush2 == 1) {
keyPush2 = 0;
}
}
void noteOn(byte cmd, byte data1, byte data2) {
Serial.write(cmd);
Serial.write(data1);
Serial.write(data2);
}
Moderator edit: [code] ... [/code] tags added. (Nick Gammon)
