Hairless Midi <-> Serial Bridge.....NEED HELP ASAP :(

Hello everyone, :smiley_cat: .
I am relatively new to Arduino and am attempting to build a “MIDI theremin” with a ultrasonic sensor hc-sr04 for function inside of Ableton’s Live 10.

I previously built a MIDI controller using pots and the Arduino Max For Live device to map parameters within Live 10, which worked well.

(Using this tutorial, with slight self modifications):

Although now I am attempting to send raw MIDI signal without use of the Max For Live device using the ultrasonic sensor to play notes as the Arduino/Max device only allows mapping of parameters rather than note outputs.

I referenced this forum post for the ultrasonic sensor MIDI code:

I copied the code exactly

#include <MIDI.h>


float val = 0;
int lastVal = 0;


const int trigPin = 9;
const int echoPin = 10;

long duration;
int distance;


int sensorPin = 0;
int sensorValue = 0;


#define CC 0xB
#define CHANNEL_VOLUME 0x07


void setup()
{

pinMode (trigPin, OUTPUT);
pinMode (echoPin, INPUT);

   Serial.begin(9600);       // Set the speed of the midi port to the same as we will be using in the Hairless Midi software
}




/* The format of the message to send via serial. We create a new data structure, that can store 3 bytes at once.  This will be easier to send as MIDI. */
typedef struct {
  unsigned int channel : 4;   // second nibble : midi channel (0-15) (channel and status are swapped, because Arduino is Little Endian)
  unsigned int status : 4;    // first  nibble : status message (NOTE_ON, NOTE_OFF or CC (control change)
  uint8_t data1;              // second byte   : first value (0-127), controller number or note number
  uint8_t data2;              // third  byte   : second value (0-127), controller value or velocity
}
t_midiMsg;          // We call this structure 't_midiMsg'



void MIDISend(uint8_t status, uint8_t channel, uint8_t data1, uint8_t data2) {
  t_midiMsg msg;
  msg.status = status & 0xF;
  msg.channel = (channel-1) & 0xF; // channels are 0-based
  msg.data1 = data1 & 0x7F;
  msg.data2 = data2 & 0x7F;
  Serial.write((uint8_t *)&msg,3);
}


void loop()
{

digitalWrite (trigPin, LOW);
delayMicroseconds(2);

digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite (trigPin, LOW);

duration = pulseIn(echoPin, HIGH);
distance = duration*0.034/2;


if (distance<31)
{
   val = (distance * 4.2)*-1;
   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.
   {
   MIDISend(CC, 1, CHANNEL_VOLUME, val);
   lastVal = val;
   delay(20);
   }
}
   
}

I was receiving signal within the Hairless MIDI bridge although it was not translating into Ableton Live 10, BUT, the Hairless MIDI bridge was showing midi inputting and giving valid velocities and such, so while I was trying to figure that after re-uploading the sketch and such it just randomly stopped receiving signal altogether within the bridge-ware.

I was able to get signal inputting to the Hairless MIDI bridge after 30+mins of tinkering by haphazardly starting things in different order and detaching the MIDI to USB connection and removing 5pin midi jack I had on my breadboard. although it would just display an error if it was receiving info in the serial port and not send it to the MIDI out.
I now cant replicate it at all.

The error in the debug screen was:

“Error: got unexpected data byte 0x0”

I should also mention the sensor seems to be working correctly as I am getting values in the serial monitor indicative that its recording my distance from the sensor. (baud values are the same)

“17:22:55.193 → ⸮p⸮k⸮g⸮g⸮p⸮p⸮c⸮t⸮k⸮g⸮g⸮c⸮c⸮⸮[⸮V⸮t⸮V⸮t⸮⸮”

Please let me know if any more info needs to be provided.

New to the forum, sorry if the post layout is terrible.

Please help if you can!
Been stressing myself out over not being able to replicate the problem or figure out where its stemming from. :confused: :confused:

Thank-you a ton…

Salem

Serial.begin(9600);

Is too slow for MIDI, especially with Hairless. Open up Hairless and look at the default speed it is set to and set the speed in the Arduino program to match.