BLYNK
HOME       📲 GETTING STARTED       📗 DOCS       ❓HELP CENTER       👉 SKETCH BUILDER

Trouble with BLE blank integrated to project


#1

Hello, I wanted to try to integrate blynk BLE (HM-10) to control some MIDI control changes (value) with the help of “virtual” buttons from the blynk app. But as soon as I tried integrating it to my button sketch that send MIDI to USB the buttons stopped working (They are not sending anything). I am using a pro micro (clone) which sends MIDIUSB to my computer (main Serial. stream) when a button is pressed. The problem is that as soon as I integrate the “Blynk.begin(SerialBLE, auth)” line into the setup the buttons stop working. I have tried removing parts of the blynk code for troubleshooting what causes it and I ended up with the “Blynk.begin” line to be causing the problem. IT works perfectly when I remove only the Blynk.begin line. I tried looking in the “SerialBLE” scripts from blynks source but I didn’t find anything that would cancel out my buttons function in there? Thanks for taking your time

Code below (The commented things etc you can skip since this is a big work of progress with different functions):

#include <MIDIUSB.h>
//#include <MIDI.h>
//#include <midi_UsbTransport.h>

char auth[] = "57828136a344445eb1631b89cb79ac5a";
#define BLYNK_INFO_CONNECTION "SerialBLE"
//
//static const unsigned sUsbTransportBufferSize = 16;
//typedef midi::UsbTransport<sUsbTransportBufferSize> UsbTransport;
//
//UsbTransport sUsbTransport;
//
//MIDI_CREATE_INSTANCE(UsbTransport, sUsbTransport, MIDI);
//int incomingByte = 0;

#include <BlynkSimpleSerialBLE.h>
#include <SoftwareSerial.h>
SoftwareSerial SerialBLE(14, 15);
#define NUM_BUTTONS 8

int  defaultkey = 60;   //48
int  Currentroot = 60;
int  keyroot1 = Currentroot;
int  keyroot2 = keyroot1 + 2;
int  keyroot3 = keyroot2 + 2;
int  keyroot4 = keyroot3 + 1;
int  keyroot5 = keyroot4 + 2;
int  keyroot6 = keyroot5 + 2;
int  keyroot7 = keyroot6 + 2;
int  keyroot8 = keyroot7 + 2;
//int  keyroot1 = 36; //Kick
//int  keyroot2 = 38; //Snare
//int  keyroot3 = 57; //Crash
//int  keyroot4 = 48; //Hi-mid tom
//int  keyroot5 = 45; //LOW-FLOOR-TOM
//int  keyroot6 = 42; //closed hi hat
//int  keyroot7 = 46; //open HH
//int  keyroot8 = 59; //RIDE CYMBAL
const uint8_t  button1 = 2;
const uint8_t  button2 = 3;
const uint8_t  button3 = 4;
const uint8_t  button4 = 5;
const uint8_t  button5 = 6;
const uint8_t  button6 = 7;
const uint8_t  button7 = 8;
const uint8_t  button8 = 9;
//const uint8_t  intensityPot = 0;  //A0 input
const uint8_t buttons[NUM_BUTTONS] = {button1, button2, button3, button4, button5, button6, button7, button8};
const byte noteFunction[NUM_BUTTONS] = {keyroot1, keyroot2, keyroot3, keyroot4, keyroot5, keyroot6, keyroot7, keyroot8};   //  Indicator LED
uint8_t  notesTime[NUM_BUTTONS];
uint8_t  pressedButtons = 0x00;
uint8_t  previousButtons = 0x00;
uint8_t  intensity = 127;
uint8_t prevChange = 0x00;
int transpose = 0;
int pitchbendPot = 5;




//struct MySettings : public midi::DefaultSettings
//{
//  static const bool Use1ByteParsing = false;
//  static const unsigned SysExMaxSize = 1026; // Accept SysEx messages up to 1024 bytes long.
//  static const long BaudRate = 31250;
//};
//#define DBGSERIAL if (0) SERIAL_PORT_MONITOR
//
//#ifdef USBCON
//#define MIDI_SERIAL_PORT Serial1
//#else
//#define MIDI_SERIAL_PORT Serial
//#endif
//
//MIDI_CREATE_CUSTOM_INSTANCE(HardwareSerial, MIDI_SERIAL_PORT, MIDIUART, MySettings);

void BLE(const char * command) {
Serial.print("Command send :");
Serial.println(command);
SerialBLE.println(command);
//wait some time
delay(600);

char reply[100];
int i = 0;
while (SerialBLE.available()) {
 reply[i] = SerialBLE.read();
 i += 1;
}
//end the string
reply[i] = '\0';
Serial.print(reply);
Serial.println("Reply end");
}

//void USBSystemExclusive(unsigned size, byte *data, bool markersIncluded) {
//  uint8_t bytesOut;
//  midiEventPacket_t evt;
//
//  while (size > 0) {
//    bytesOut = min(3, size);
//    evt.byte1 = *data++;
//    size -= bytesOut;
//    switch (bytesOut) {
//      case 1:
//        evt.header = 0x05;
//        evt.byte2 = evt.byte3 = 0;
//        break;
//      case 2:
//        evt.header = 0x06;
//        evt.byte2 = *data++;
//        evt.byte3 = 0;
//        break;
//      case 3:
//        evt.header = (size > 0) ? 0x04 : 0x07;
//        evt.byte2 = *data++;
//        evt.byte3 = *data++;
//        break;
//      default:
//        break;
//    }
//    MidiUSB.sendMIDI(evt);
//  }
//}

//inline uint8_t writeUARTwait(uint8_t *p, uint16_t size)
//{
//  // Apparently, not needed. write blocks, if needed
//  //  while (MIDI_SERIAL_PORT.availableForWrite() < size) {
//  //    delay(1);
//  //  }
//  return MIDI_SERIAL_PORT.write(p, size);
//}
//
//uint16_t sysexSize = 0;
//
//void sysex_end(uint8_t i)
//{
//  sysexSize += i;
//  DBGSERIAL.print(F("sysexSize="));
//  DBGSERIAL.println(sysexSize);
//  sysexSize = 0;
//}
//



//void readIntensity()
//{
//  int val = analogRead(intensityPot);
//  intensity = (int) (map(val, 0, 1023, 0, 127));
//}

//void pitchBend()
//{}


// playMidi(0xE0, low, high);
//void convUsb(byte channelx, byte pitchx, byte velocityx) {
//  midiEventPacket_t xx = {0x09, 0x90 | channelx, pitchx, velocityx};
//
//  if (xx.header != 0) {
//    switch (xx.header & 0x0F) {
//      case 0x00:  // Misc. Reserved for future extensions.
//        break;
//      case 0x01:  // Cable events. Reserved for future expansion.
//        break;
//      case 0x02:  // Two-byte System Common messages
//      case 0x0C:  // Program Change
//      case 0x0D:  // Channel Pressure
//        writeUARTwait(&xx.byte1, 2);
//        break;
//      case 0x03:  // Three-byte System Common messages
//      case 0x08:  // Note-off
//      case 0x09:  // Note-on
//      case 0x0A:  // Poly-KeyPress
//      case 0x0B:  // Control Change
//      case 0x0E:  // PitchBend Change
//        writeUARTwait(&xx.byte1, 3);
//        break;
//      case 0x04:  // SysEx starts or continues
//        sysexSize += 3;
//        writeUARTwait(&xx.byte1, 3);
//        break;
//      case 0x05:  // Single-byte System Common Message or SysEx ends with the following single byte
//        sysex_end(1);
//        writeUARTwait(&xx.byte1, 1);
//        break;
//      case 0x06:  // SysEx ends with the following two bytes
//        sysex_end(2);
//        writeUARTwait(&xx.byte1, 2);
//        break;
//      case 0x07:  // SysEx ends with the following three bytes
//        sysex_end(3);
//        writeUARTwait(&xx.byte1, 3);
//        break;
//      case 0x0F:  // Single Byte, TuneRequest, Clock, Start, Continue, Stop, etc.
//        writeUARTwait(&xx.byte1, 1);
//        break;
//    }
//  }
//
//}
void readButtons()
{
for (int i = 0; i < NUM_BUTTONS; i++)
{
 if (digitalRead(buttons[i]) == LOW)
 {
   bitWrite(pressedButtons, i, 1);
   delay(50);
 }
 else
   bitWrite(pressedButtons, i, 0);
}
}


void playNotes()
{
for (int i = 0; i < NUM_BUTTONS; i++)
{
 if (bitRead(pressedButtons, i) != bitRead(previousButtons, i))
 {
   if (bitRead(pressedButtons, i))
   {
     bitWrite(previousButtons, i , 1);
     Serial.print("Pressed");
     noteOn(0, noteFunction[i], intensity);
     MidiUSB.flush();
     //       convUsb(0, noteFunction[i], intensity);
   }
   else
   {
     bitWrite(previousButtons, i , 0);
     noteOff(0, noteFunction[i], 0);
     MidiUSB.flush();
     //       convUsb(0, noteFunction[i], 0);
     Serial.print("Pressed off");
   }
 }
}
}
//
//
//void pitchwheelChange(int value) {
//  unsigned int change = 0x2000 + value;  //  0x2000 == No Change
//  unsigned char lowValue = change & 0x7F;  // Low 7 bits
//  unsigned char highValue = (change >> 7) & 0x7F;  // High 7 bits
//  midiEventPacket_t event = {0x0E, 0xE0 | 0, lowValue, highValue};
//  MidiUSB.sendMIDI(event);
//
//}
//void readPitchbend() {
//  pitchwheelChange(map(analogRead(pitchbendPot), 0, 1023, -8000, 8000));
//  //  } if pitchwheelChange(change != prevChange
//
//}



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);
}

void controlChange(byte channel, byte control, byte value)
{ midiEventPacket_t event = {0x0B, 0xB0 | channel, control, value};
MidiUSB.sendMIDI(event);
}
//void handleNoteOn(byte inChannel, byte inNumber, byte inVelocity)
//{
//}
//void handleNoteOff(byte inChannel, byte inNumber, byte inVelocity)
//{
//}
//BLYNK_WRITE(V1)
//{
//  int pinValue = param.asInt(); // assigning incoming value from pin V1 to a variable
//  // You can also use:
//  // String i = param.asStr();
//  // double d = param.asDouble();
//  Serial.print("V1 Slider value is: ");
//  Serial.println(pinValue);
//}


void setup() {
//DBGSERIAL.begin(115200);
Serial.begin(9600);
SerialBLE.begin(9600);
//  BLE("AT+NAMEBROR");
//   BLE("AT+ROLE0");
//     BLE("AT+BAUD4");
Blynk.begin(SerialBLE, auth);
// while (!Serial);
// MIDI.begin();
// MIDI.setHandleNoteOn(handleNoteOn);
// MIDI.setHandleNoteOff(handleNoteOff);
for (int i = 0; i < NUM_BUTTONS; i++)
 pinMode(buttons[i], INPUT_PULLUP);
// MIDIUART.begin(MIDI_CHANNEL_OMNI);
// MIDIUART.turnThruOff();

}

void loop() {

/* MIDI UART -> MIDI USB */

//  if (MIDIUART.read()) {
//    midi::MidiType msgType = MIDIUART.getType();
//    DBGSERIAL.print(F("UART "));
//    DBGSERIAL.print(msgType, HEX);
//    DBGSERIAL.print(' ');
//    DBGSERIAL.print(MIDIUART.getData1(), HEX);
//    DBGSERIAL.print(' ');
//    DBGSERIAL.println(MIDIUART.getData2(), HEX);
//    switch (msgType) {
//      case midi::InvalidType:
//        break;
//      case midi::NoteOff:
//      case midi::NoteOn:
//      case midi::AfterTouchPoly:
//      case midi::ControlChange:
//      case midi::ProgramChange:
//      case midi::AfterTouchChannel:
//      case midi::PitchBend:
//        {
//          midiEventPacket_t tx = {
//            (byte)(msgType >> 4),
//            (byte)((msgType & 0xF0) | ((MIDIUART.getChannel() - 1) & 0x0F)), /* getChannel() returns values from 1 to 16 */
//            MIDIUART.getData1(),
//            MIDIUART.getData2()
//          };
//          MidiUSB.sendMIDI(tx);
//          MidiUSB.flush();
//          break;
//        }
//      case midi::SystemExclusive:
//        USBSystemExclusive(MIDIUART.getSysExArrayLength(),
//                           (byte *)MIDIUART.getSysExArray(), true);
//        MidiUSB.flush();
//        break;
//      case midi::TuneRequest:
//      case midi::Clock:
//      case midi::Start:
//      case midi::Continue:
//      case midi::Stop:
//      case midi::ActiveSensing:
//      case midi::SystemReset:
//        {
//          midiEventPacket_t tx = { 0x0F, (byte)(msgType), 0, 0 };
//          MidiUSB.sendMIDI(tx);
//          MidiUSB.flush();
//          break;
//        }
//      case midi::TimeCodeQuarterFrame:
//      case midi::SongSelect:
//        {
//          midiEventPacket_t tx = { 0x02, (byte)(msgType), MIDIUART.getData1(), 0 };
//          MidiUSB.sendMIDI(tx);
//          MidiUSB.flush();
//          break;
//        }
//      case midi::SongPosition:
//        {
//          midiEventPacket_t tx = { 0x03, (byte)(msgType), MIDIUART.getData1(), MIDIUART.getData2() };
//          MidiUSB.sendMIDI(tx);
//          MidiUSB.flush();
//          break;
//        }
//      default:
//        break;
//    }
//  }
//
//  /* MIDI USB -> MIDI UART */
//  midiEventPacket_t rx = MidiUSB.read();
//  if (rx.header != 0) {
//    DBGSERIAL.print(F("USB "));
//    DBGSERIAL.print(rx.header, HEX);
//    DBGSERIAL.print(' ');
//    DBGSERIAL.print(rx.byte1, HEX);
//    DBGSERIAL.print(' ');
//    DBGSERIAL.print(rx.byte2, HEX);
//    DBGSERIAL.print(' ');
//    DBGSERIAL.println(rx.byte3, HEX);
//    switch (rx.header & 0x0F) {
//      case 0x00:  // Misc. Reserved for future extensions.
//        break;
//      case 0x01:  // Cable events. Reserved for future expansion.
//        break;
//      case 0x02:  // Two-byte System Common messages
//      case 0x0C:  // Program Change
//      case 0x0D:  // Channel Pressure
//        writeUARTwait(&rx.byte1, 2);
//        break;
//      case 0x03:  // Three-byte System Common messages
//      case 0x08:  // Note-off
//      case 0x09:  // Note-on
//      case 0x0A:  // Poly-KeyPress
//      case 0x0B:  // Control Change
//      case 0x0E:  // PitchBend Change
//        writeUARTwait(&rx.byte1, 3);
//        break;
//      case 0x04:  // SysEx starts or continues
//        sysexSize += 3;
//        writeUARTwait(&rx.byte1, 3);
//        break;
//      case 0x05:  // Single-byte System Common Message or SysEx ends with the following single byte
//        sysex_end(1);
//        writeUARTwait(&rx.byte1, 1);
//        break;
//      case 0x06:  // SysEx ends with the following two bytes
//        sysex_end(2);
//        writeUARTwait(&rx.byte1, 2);
//        break;
//      case 0x07:  // SysEx ends with the following three bytes
//        sysex_end(3);
//        writeUARTwait(&rx.byte1, 3);
//        break;
//      case 0x0F:  // Single Byte, TuneRequest, Clock, Start, Continue, Stop, etc.
//        writeUARTwait(&rx.byte1, 1);
//        break;
//    }
//  }

readButtons();
playNotes();
Blynk.run();
}
//  mySerial.println();
´´´

#2

Please repost code, properly formatted as instructed, thanks. - Moderator

Blynk - FTFC


#3

Kind of like saying “my car makes strange sounds when running… the solution was to remove the engine” :stuck_out_tongue_winking_eye:

After you properly repost the code, someone can look at it… but be aware, this is a site that teaches how Blynk works, not as an on-call code mechanic :wink:


#4

Fixed code formatting, I tried to find a button for this in the new topic window but I couldn’t find it :sweat_smile:
I guess thats true with the engine, but removing the other lines did nothing or keeping them etc so I figured it would say something about where to look for the problem. I figured that including a whole library without running it could also cause interruptions in functions just by them having the same keywords? I don’t know.


#5

Read this to start…

those functions are trying to run thousands of times a second.

Also, the Blynk.begin() command is a blocking command… until a proper connection is made, the code stalls out at this point.

Blynk.config() is a non-blocking alternative (after arranging Blynk connection method), but I don’t know if it works with BT/BLE connection type.