Go Down

Topic: MIDI serial over Bluetooth : CODE issue [Please HELP] (Read 1 time) previous topic - next topic

Sdreoos

Hello everyone, this sketch is the result of 3 example code found on the web assembled by me (not expert). It's a Midi CC controller made of a Gyroscope and 5 potentiometers and it works fine as MIDI - Serial over USB (Hairless MIDI) but I added a Bluetooth module HC-06 to transmit it wireless. The state-of-art the 5 potentiometers are working via BT but not the 3 CC generated by the Gyroscope axis because I'm not able to assign the "MIDI.sendControlChange" to the "Bth.write" 
Could you help me please?

Code: [Select]
#include "Wire.h"
#include "MIDI.h"
#include <SoftwareSerial.h>

int cc1 ;
int cc2 ;
int cc3 ;

const int MPU_addr=0x68;  // I2C address of the MPU-6050
int16_t AcX,AcY,AcZ,GyX,GyY,GyZ;

int val = 0; //Our initial pot values. We need one for the first value and a second to test if there has been a change made. This needs to be done for all 3 pots.
int lastVal = 0;
int val2 = 0;
int lastVal2 = 0;
int val3 = 0;
int lastVal3 = 0;
int val4 = 0;
int lastVal4 = 0;
int val5 = 0;
int lastVal5 = 0;

#define BTH_RX 11
#define BTH_TX 12

SoftwareSerial Bth(BTH_RX, BTH_TX);

MIDI_CREATE_DEFAULT_INSTANCE();

void setup()
{
  MIDI.begin();
  Serial.begin(9600);      // Set the speed of the midi port to the same as we will be using in the Hairless Midi software
  Bth.begin(9600);
  Bth.print("AT+ROLE=S"); 
  delay(100);
  Bth.print("AT+RESET"); 
  delay(250);

  Wire.begin();
  Wire.beginTransmission(MPU_addr);
  Wire.write(0x6B);  // PWR_MGMT_1 register
  Wire.write(0);     // set to zero (wakes up the MPU-6050)
  Wire.endTransmission(true);
 
}

void loop()

{
  Wire.beginTransmission(MPU_addr);
  Wire.write(0x3B);  // starting with register 0x3B (ACCEL_XOUT_H)
  Wire.endTransmission(false);
  Wire.requestFrom(MPU_addr,14,true);  // request a total of 14 registers
 AcX=Wire.read()<<8|Wire.read();  // 0x3B (ACCEL_XOUT_H) & 0x3C (ACCEL_XOUT_L)     
 AcY=Wire.read()<<8|Wire.read();  // 0x3D (ACCEL_YOUT_H) & 0x3E (ACCEL_YOUT_L)
 AcZ=Wire.read()<<8|Wire.read();  // 0x3F (ACCEL_ZOUT_H) & 0x40 (ACCEL_ZOUT_L)
 // Tmp=Wire.read()<<8|Wire.read();  // 0x41 (TEMP_OUT_H) & 0x42 (TEMP_OUT_L)
 GyX=Wire.read()<<8|Wire.read();  // 0x43 (GYRO_XOUT_H) & 0x44 (GYRO_XOUT_L)
 GyY=Wire.read()<<8|Wire.read();  // 0x45 (GYRO_YOUT_H) & 0x46 (GYRO_YOUT_L)
 GyZ=Wire.read()<<8|Wire.read();  // 0x47 (GYRO_ZOUT_H) & 0x48 (GYRO_ZOUT_L)
 
 
//  MIDI Values

cc2= map(AcX, 0,16300,0,127);
cc1= map(AcY, 0,16300,0,127);
cc3= map(AcZ,-16300,16300,127,0);

if (cc1>=127)
{
  cc1=127;
  }
if (cc1 <=0)
{
  cc1=0;
  }

if (cc2>=127)
{
  cc2=127;
  }
if (cc2 <=0)
{
  cc2=0;
  }

if (cc3>=127)
{
  cc3=127;
  }
if (cc3 <=0)
{
  cc3=0;
  }


  {
  MIDI.sendControlChange(16,cc1,1);
  }
 
  {
  MIDI.sendControlChange(17,cc2,1);
  }
 
  {
  MIDI.sendControlChange(18,cc3,1);
  }
 
 val = analogRead(0)/8;   // Divide by 8 to get range of 0-127 for midi
   if (val != lastVal) // If the value does not = the last value the following command is made. This is because the pot has been turned. Otherwise the pot remains the same and no midi message is output.
   {
   MIDImessage(176,11,val);}         // 176 = CC command (channel 1 control change), 1 = Which Control, val = value read from Potentionmeter 1
   lastVal = val;

   val2 = analogRead(1)/8;   // Divide by 8 to get range of 0-127 for midi
   if (val2 != lastVal2)
   {
   MIDImessage(176,12,val2);}         // 176 = CC command, 2 = Which Control, val = value read from Potentionmeter 2
   lastVal2 = val2;
   
   val3 = analogRead(2)/8;   // Divide by 8 to get range of 0-127 for midi
   if (val3 != lastVal3)
   {
   MIDImessage(176,13,val3);}         // 176 = CC command, 3 = Which Control, val = value read from Potentionmeter 3
   lastVal3 = val3;
   
   val4 = analogRead(11)/8;   // Divide by 8 to get range of 0-127 for midi
   if (val4 != lastVal4)
   {
   MIDImessage(176,14,val4);}         // 176 = CC command, 4 = Which Control, val = value read from Potentionmeter 4
   lastVal4 = val4;
   
   
   val5 = analogRead(6)/8;   // Divide by 8 to get range of 0-127 for midi
   if (val5 != lastVal5)
   {
   MIDImessage(176,15,val5);}         // 176 = CC command, 5 = Which Control, val = value read from Potentionmeter 5
   lastVal5 = val5;
  delay(10);
 }
 
void MIDImessage(byte command, byte data1, byte data2) //pass values out through standard Midi Command

{
 
   // Seriale BT
  Bth.write(command);
  Bth.write(data1);
  Bth.write(data2);
 
}

slipstick

MIDI_CREATE_DEFAULT_INSTANCE(); sets up the MIDI to use hardware Serial on pins 0, 1. Your bluetooth isn't connected to pins 0, 1. That's why the MIDI doesn't go out over bluetooth.

Try using MIDI_CREATE_INSTANCE() telling it the Serial port you actually want it to use. What does that do?

Steve



Sdreoos

Hello Steve, got it!

I didn't know that MIDI_CREATE_DEFAULT_INSTANCE(); send the serial to 0\1!  :o 
So I've solved erasing it and replacing the "MIDI.sendControlChange" with "MIDImessage(176,xx,val)" and now it working good!
Thank you very much for the tip.

Regards

Gio

Go Up