Hello everyone. I am having some trouble with my side project of creating a midi drum sequencer through sending midi thru USB. I am using Arduino (Original) Micro and osx/ios for ble. For the moment im using a generic numpad (keypad) matrix until further on. I am using the Blynk library to communicate through BLE with arduino for the purpose of mostly learning but also making the drum machine more compact by implementing virtual switches/knobs for potentiometers instead of physical ones.
So far I have tackled most problems and I got the numpad to work fine except that it is sending the wrong midi notes (not entirely wrong). For troubleshooting I put keypad button 1 to send midi note 1 and so on. But the problem is that its sending 31 when im running the sketch.
I attached a picture of midi monitor log when I pressed keys 1-12 on the numpad.
As you can see on the picture it puts 3 in front of the note number so it goes 31, 32, 33 etc. When it goes to midi note 10 it starts sending 30 again. When I serial print (key) it gives me the correct number so I am assuming there is something with my midi code?
Thanks for taking your time
The main code for midi is inside the Keypad event section
/* @file CustomKeypad.pde
|| @version 1.0
|| @author Alexander Brevig
|| @contact alexanderbrevig@gmail.com
||
|| @description
|| | Demonstrates changing the keypad size and key values.
|| #
*/
#include <Keypad.h>
#include <MIDIUSB.h>
#include <FifteenStep.h>
////////BLYNK/////
#define SerialBLE Serial1
#include <BlynkSimpleSerialBLE.h>
char auth[] = "2eb76ef79c144b53a6e23126124c2522";
WidgetLED led1(V13);
///
FifteenStep seq = FifteenStep();
//SEQUENCER
int steps = 16;
int tempo;
int seqmode;
int velocity = 0;
const byte ROWS = 4; //four rows
const byte COLS = 3; //four columns
//define the cymbols on the buttons of the keypads
//36, 38, 57, 48, 45, 42, 46, 59}
// {'OPEN-HH', 'PEDAL-HIHAT', 'TAMBOURINE'},
// {'H-H', 'TOM', 'RIMSHOT?'},
// {'CRASH', 'TOM', 'SNARE'},
// {'CYMBAL', 'TOM', 'KICK'}
//char hexaKeys[ROWS][COLS] = {
// {'46', '44', '54'},
// {'42', '48', '40'},
// {'57', '47', '38'},
// {'55', '45', '36'}
//};
char hexaKeys[ROWS][COLS] = {
{'1', '2', '3'},
{'4', '5', '6'},
{'7', '8', '9'},
{'10', '11', '12'}
};
bool pressed = false;
byte rowPins[ROWS] = {7, 2, 3, 5}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {6, 8, 4}; //connect to the column pinouts of the keypad
int intensity = 127;
/// BLYNK FUNCTIONS///
BLYNK_WRITE(V0) //STEPS
{
switch (param.asInt()) {
case 1:
seq.setSteps(32);
Serial.println("Steps: 32");
break;
case 2:
seq.setSteps(24);
Serial.println("Steps: 824");
break;
case 3:
seq.setSteps(16);
Serial.println("Steps: 16");
break;
case 4:
{ seq.setSteps(12);
Serial.println("Steps: 12");
}
case 5:
seq.setSteps(8);
Serial.println("Steps: 8");
break;
case 6:
seq.setSteps(4);
Serial.println("Steps: 4");
break;
case 7:
seq.setSteps(2);
Serial.println("Steps: 2");
break;
}
}
BLYNK_WRITE(V1)
{
int pinValue = param.asInt(); // assigning incoming value from pin V1 to a variable
intensity = pinValue;
// process received value
}
BLYNK_WRITE(V2) {
switch (param.asInt())
{
case 1: // Item 1
seq.start();
break;
case 2: // Item 2
seq.pause();
break;
case 3: // Item 2
seq.panic();
break;
}
}
BLYNK_WRITE(V3)
{
int pinValue2 = param.asInt(); // assigning incoming value from pin V1 to a variable
seq.setTempo(pinValue2);
// process received value
}
BLYNK_WRITE(V4)
{
seq.panic();
}
// Order pinout C2 R1, C1 R4 C3, R3, R2
// Pin-in Arduino 8 7 6 5 4 3 2
//initialize an instance of class NewKeypad
Keypad customKeypad = Keypad( makeKeymap(hexaKeys), rowPins, colPins, ROWS, COLS);
/////////////////////////////////////////////
///// SEQUENCER
/////////////////////////////////////////////
void midi(byte channel, byte command, byte arg1, byte arg2) {
// init combined byte
byte combined = command;
// shift if necessary and add MIDI channel
if (combined < 128) {
combined <<= 4;
combined |= channel;
// send midi message
midiEventPacket_t event = {command, combined, arg1, arg2};
MidiUSB.sendMIDI(event);
MidiUSB.flush();
}
}
void step(int current, int last) {
// blink on even steps
if (current % 2 == 0)
{ digitalWrite(13, HIGH);
led1.on();
}
else
{
digitalWrite(13, LOW);
led1.off();
}
}
void noteOn(byte channel, byte pitch, byte velocity) {
midiEventPacket_t noteOn = {0x09, 0x90 | channel, pitch, velocity};
MidiUSB.sendMIDI(noteOn);
}
void noteOff(byte channel, byte pitch, byte velocity) {
midiEventPacket_t noteOff = {0x08, 0x80 | channel, pitch, velocity};
MidiUSB.sendMIDI(noteOff);
}
/// READS
void keypadEvent(KeypadEvent key) {
switch (customKeypad.getState()) {
case PRESSED:
noteOn(0, key, intensity);
MidiUSB.flush();
seq.setNote(0, key, intensity);
delay(45);
break;
case RELEASED:
noteOff(0, key, 0);
MidiUSB.flush();
seq.setNote(0, key, 0);
break;
case HOLD:
break;
}
}
void setup() {
Serial.begin(115200);
customKeypad.addEventListener(keypadEvent);
tempo = 120;
seq.begin(tempo, steps);
seq.setMidiHandler(midi);
seq.setStepHandler(step);
seq.start();
intensity = 127;
SerialBLE.begin(9600);
Blynk.begin(SerialBLE, auth);
}
void loop() {
char key = customKeypad.getKey();
//
// if (key) {
// noteOn(0, key, intensity);
// MidiUSB.flush();
// seq.setNote(0, key, intensity);
// delay(75);
// }
if (key) {
Serial.println(key);
}
seq.run();
Blynk.run();
}