I'm trying to build a midi controller as a first Arduino UNO project, I want to add a 4x4 Analog Keypad as a MIDI Keyboard.
I want each button pressed to move up a note. Im using analogRead but it's getting values on a constant loop so it's sending the MIDI message repeatedly. I've tried other methods with scripts I've found that will stop the looping but messes with the keypad voltage.
This method causes the buttons to loop repeatedly
/////////////////////////////////////////////
// LIBRARIES
#include <MIDI.h> // by Francois Best
MIDI_CREATE_DEFAULT_INSTANCE();
MIDI_CREATE_INSTANCE(HardwareSerial,Serial, midiOut);
/////////////////////////////////////////////
// MIDI
byte midiCh = 1; //* MIDI channel to be used
byte note = 36; //* Lowest note to be used
byte cc = 1; //* Lowest MIDI CC to be used
void setup()
{
Serial.begin(115250);
}
void loop()
{
matrix();
}
void matrix() {
int buttonValue;
buttonValue = analogRead(A1); //Read analog value from A1 pin
//For 1st button:
if (buttonValue>=1000)
midiOut.sendNoteOn(36,127,1);
else if(buttonValue>900)
midiOut.sendNoteOn(37,127,1);
else if(buttonValue>820)
midiOut.sendNoteOn(38,127,1);
else if(buttonValue>750)
midiOut.sendNoteOn(39,127,1);
else if(buttonValue>660)
midiOut.sendNoteOn(40,127,1);
else if(buttonValue>620)
midiOut.sendNoteOn(41,127,1);
else if(buttonValue>585)
midiOut.sendNoteOn(42,127,1);
else if(buttonValue>540)
midiOut.sendNoteOn(43,127,1);
else if(buttonValue>500)
midiOut.sendNoteOn(44,127,1);
else if(buttonValue>475)
midiOut.sendNoteOn(45,127,1);
else if(buttonValue>455)
midiOut.sendNoteOn(46,127,1);
else if(buttonValue>425)
midiOut.sendNoteOn(47,127,1);
else if(buttonValue>370)
midiOut.sendNoteOn(48,127,1);
else if(buttonValue>300)
midiOut.sendNoteOn(49,127,1);
else if(buttonValue>260)
midiOut.sendNoteOn(50,127,1);
else if(buttonValue>200)
midiOut.sendNoteOn(51,127,1);
else if (buttonValue>=0)
midiOut.sendNoteOn(36,0,0);
delay(150);
}
Is it simple to get set up a MIDI keypad with an analog button matrix in a single analog pin or are the voltage reads too unpredictable to get it to run smoothly? Should I just use a digital keypad instead?
Thanks