Hello, I am programming my arduino micro (original) to send midi BLE data to my iPhone using keypad buttons. The code I have so far is working but when I add the sequencer library it won't work with my BLE code.
When I tell arduino to serial print the data its trying to send when the sequencer is running, the sequencer sends different values compared to the (working)buttons. I am guessing the external sequencer library functions is converting my data into hex but I don't know how to convert it back before putting into the BLE midi function?
Serial print says:
Sent Note On
0
38
144
^ This is the working button
PRESSED
Sent Note On
0
9
38
^ This is the sequenced data
Thanks for reading
Below is the code
#include <SPI.h>
#include <BLEPeripheral.h>
//#include <MIDI.h>
#include <Keypad.h>
#include <FifteenStep.h>
#define BLE_REQ A5
#define BLE_RDY 2
#define BLE_RST A4
// MIDI USB
//#if defined(USBCON)
//#include <midi_UsbTransport.h>
//
//static const unsigned sUsbTransportBufferSize = 16;
//typedef midi::UsbTransport<sUsbTransportBufferSize> UsbTransport;
//UsbTransport sUsbTransport;
//MIDI_CREATE_INSTANCE(HardwareSerial, Serial1, MIDI);
// MIDI_CREATE_INSTANCE(UsbTransport, sUsbTransport, MIDI);
//#else // No USB available, fallback to Serial
//MIDI_CREATE_DEFAULT_INSTANCE();
//MIDI_CREATE_INSTANCE(SoftwareSerial, Serial, MIDI);
//MIDI_CREATE_INSTANCE(HardwareSerial, Serial1, MIDI);
BLEPeripheral blePeripheral = BLEPeripheral(BLE_REQ, BLE_RDY, BLE_RST);
BLEService service = BLEService("03B80E5A-EDE8-4B33-A751-6CE34EC4C700");
BLECharacteristic characteristic("7772E5DB-3868-4112-A1A9-F2669D106BF3", BLERead | BLEWriteWithoutResponse | BLENotify, 20);
//BLECharCharacteristic characteristic = BLECharCharacteristic("7772E5DB-3868-4112-A1A9-F2669D106BF3",
//BLERead | BLEWriteWithoutResponse | BLENotify, 20);
BLEDescriptor descriptor = BLEDescriptor("2902", "0");
uint8_t msgBuf[5];
unsigned long msOffset = 0;
#define MAX_MS 0x01FFF //13 bits, 8192 dec
//KEYPAD
const byte ROWS = 4; //four rows
const byte COLS = 3; //four columns
char keys[ROWS][COLS] = {
{46, 44, 54},
{42, 48, 40},
{57, 47, 38},
{55, 45, 36}
};
//char keys[ROWS][COLS] = {
// {'1', '2', '3'},
// {'4', '5', '6'},
// {'7', '8', '9'},
// {'*', '0', '#'}
//};
byte rowPins[ROWS] = {4, 9, 8, 6}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {5, 3, 7}; //connect to the column pinouts of the keypad
int intensity;
// 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(keys), rowPins, colPins, ROWS, COLS);
////SEQUENCER
FifteenStep seq = FifteenStep();
int steps = 16;
int tempo;
int seqmode;
int velocity = 90;
/////////////////////////////////////////////
///// SEQUENCER
/////////////////////////////////////////////
//
//void seqmidi(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;
//
//
// }
//}
//void seqNoteOff(byte keyOn) {
//sendNoteOn(keyOn);
//serial.println("Sequenced: keyOn");
//}
//void seqNoteOff(byte keyOff) {
//sendNoteOn(keyOff);
//serial.println("Sequenced: keyOff");
//}
void step(int current, int last) {
// blink on even steps
if(current % 2 == 0)
digitalWrite(13, HIGH);
else
digitalWrite(13, LOW);
}
void setupBLE() {
blePeripheral.setLocalName("BLEMIDI2"); // optional
blePeripheral.setAdvertisedServiceUuid(service.uuid()); // optional
blePeripheral.setDeviceName("BLEMIDI2");
// add attributes (services, characteristics, descriptors) to peripheral
blePeripheral.addAttribute(service);
blePeripheral.addAttribute(characteristic);
blePeripheral.addAttribute(descriptor);
// set initial value
characteristic.setValue(0);
// begin initialization
blePeripheral.begin();
Serial.println("BLE configuration read");
}
void setup() {
Serial.begin(115200);
Serial1.begin(115200);
//// B L E . P E R I P H E R A L
setupBLE();
//// M . I . D . I
// MIDI.begin();
// MIDI.turnThruOff();
// MIDI.setHandleNoteOn(handleNoteOn);
// MIDI.setHandleNoteOff(handleNoteOff);
//KEYPAD
//customKeypad.addEventListener(keypadEvent);
// SEQUENCER
tempo = 100;
seq.begin(tempo, steps);
seq.setMidiHandler(sendNote);
seq.setStepHandler(step);
seq.start();
intensity = 90;
}
void sendNote(byte channel, byte keypadnote, byte onoff)
{
uint8_t statusByte = ((uint8_t)onoff | 0 & 0x0f);
uint8_t msgBuf[5]; //Outgoing buffer
//Calculate timestamp.
uint16_t currentTimeStamp = millis() & 0x01FFF;
msgBuf[0] = ((currentTimeStamp >> 7) & 0x3F) | 0x80; //6 bits plus MSB
msgBuf[1] = (currentTimeStamp & 0x7F) | 0x80; //7 bits plus MSB
msgBuf[2] = statusByte;
//Note
msgBuf[3] = keypadnote;
//Velocity
msgBuf[4] = intensity;
characteristic.setValue(msgBuf, 5);
Serial.println("Sent Note");
Serial.println(channel);
Serial.println(keypadnote);
Serial.println(onoff);
}
// void sendNoteOn(byte keypadnote)
// {
// uint8_t statusByte = ((uint8_t)144 | 0 & 0x0f);
// uint8_t msgBuf[5]; //Outgoing buffer
//
// //Calculate timestamp.
// uint16_t currentTimeStamp = millis() & 0x01FFF;
//
// msgBuf[0] = ((currentTimeStamp >> 7) & 0x3F) | 0x80; //6 bits plus MSB
// msgBuf[1] = (currentTimeStamp & 0x7F) | 0x80; //7 bits plus MSB
// msgBuf[2] = statusByte;
// //Note
// msgBuf[3] = keypadnote;
// //Velocity
// msgBuf[4] = intensity;
// characteristic.setValue(msgBuf, 5);
// Serial.print("Sent Note On");
//
//
// }
// void sendNoteOff(byte keypadnote)
// {
// uint8_t statusByte = ((uint8_t)128 | 0 & 0x0f);
// uint8_t msgBuf[5]; //Outgoing buffer
//
// //Calculate timestamp.
// uint16_t currentTimeStamp = millis() & 0x01FFF;
//
// msgBuf[0] = ((currentTimeStamp >> 7) & 0x3F) | 0x80; //6 bits plus MSB
// msgBuf[1] = (currentTimeStamp & 0x7F) | 0x80; //7 bits plus MSB
// msgBuf[2] = statusByte;
// //Note
// msgBuf[3] = keypadnote;
// //Velocity
// msgBuf[4] = 0;
// characteristic.setValue(msgBuf, 5);
// Serial.print("Sent Note Off");
//
// }
void loop() {
//// DIN TO BLE
//Prep the timestamp
BLECentral central = blePeripheral.central();
if (central) {
while (central.connected()) {
// central still connected to peripheral
// uint16_t write ( uint8_t singlebyte); if (key)
if (customKeypad.getKeys())
{
for (int i = 0; i < LIST_MAX; i++) // Scan the whole key list.
{
if (customKeypad.key[i].stateChanged ) // Only find keys that have changed state.
{
switch (customKeypad.key[i].kstate) {
case PRESSED:
// sendNoteOn(customKeypad.key[i].kchar);
sendNote(0, customKeypad.key[i].kchar, 144);
seq.setNote(0, customKeypad.key[i].kchar, 144);
Serial.println("PRESSED");
break;
case RELEASED:
// sendNoteOff(customKeypad.key[i].kchar);
sendNote(0, customKeypad.key[i].kchar, 128);
seq.setNote(0, customKeypad.key[i].kchar, 128);
Serial.println("RELEASED");
break;
case HOLD:
break;
case IDLE:
break;
}
}
}
}
// parseMIDIonDIN();
seq.run();
}
}
// central disconnected
Serial.print(F("Disconnected from central: "));
Serial.println(central.address());
delay(500);
}