Use HC-SR04 with Midi-Over-USB to control Ableton

Dear Community,

there is a beautiful sketch on how to receive MIDI-Notes from an HC-SR04 made by Moritz Geist.

I like the whole configuration, but i want to receive the MIDI via USB to change certain parameters in Ableton. I already downloaded the Arduino Plug-In for Max for Live.

Does somebody know which kind of modification is needed for that purpose?

Thanks for every hint already! :))

/ MIDI THEREMIN
// Moritz Simon Geist
// www.sonicrobots.com
// v.1.0 14 March 2014
////////////////////////////////
// This sketch uses an HC-SR04 Ultrasonic Sensor and produces an MIDI out signal, according to the measured distance form a object
// Be sure to include the latest MIDI Library (here 4.0)
// Arduino Playground - MIDILibrary
//
// Wiring: Ping pin from the HC-SR04 Ultrasonic Sensor --> arduino Pin 13,
// Echo pin from HC-SR04 Ultrasonic Sensor --> Arduino PIN 12
// MIDI-Out --> TX according to this sketch http://arduino.cc/en/uploads/Tutorial/MIDI_schem.png

#include <MIDI.h>
int pingPin = 13;
int inPin = 12;
long duration;
int cm_new = 20;
int cm_old = 20;
int sens = 5; // sensivity range when to launch a new note

void setup() {

Serial.begin(31250); // MIDI Begin
pinMode(pingPin, OUTPUT); // setup the Pins
pinMode(inPin, INPUT); // setup the Pins

}

void loop()
{

// The PING is triggered by a HIGH pulse of 2 or more microseconds.
// Give a short LOW pulse beforehand to ensure a clean HIGH pulse:

digitalWrite(pingPin, LOW);
delayMicroseconds(2);
digitalWrite(pingPin, HIGH);
delayMicroseconds(10);
digitalWrite(pingPin, LOW);
duration = pulseIn(inPin, HIGH); // the singal from the Sensor is measured. The length of the pulse describes the distance form an object to the sensor.

cm_new = map(constrain(duration, 20, 3000), 50, 2000, 96, 48); // contrain --> set a threshold to cut values of the sensor that are to far away of to short (20 .. 3000) MAP --> Map the sensor range to a note values vom 96 (A high C) to 48 (a low C)

if (cm_new != cm_old) // only if a new sensor value is detected, a new nite will be send. IF the object stays stable infront of the sensor, no note will be send.
{
MIDI.sendNoteOff(cm_old,0,1); // Stop the old note
MIDI.sendNoteOn(cm_new,127,1); // Send a new Note (pitch , velocity 127, on channel 1)
cm_old = cm_new;
}
delay(30); // for staiblity
}

I found this sketch now. Which includes actually settings for a whole midi-controller. But with this one i was able to connect the Sensor with a Parameter in Ableton.

Only Problem it switches uncontrollably between three steps only.

Does somebody know how to fix this?

// *********************************************************
// Sketch für Götz Müller-Dürholts MIDI-Controller
// V 1.0
// Komplettes Anfänger-Video-Tutorial: MIDI-Controller selber bauen (Tutorial mit Arduino für Anfänger - deutsch HD) - YouTube
// Mehr Infos und Projekte: http://www.goetzmd.de
// *********************************************************

byte controlChange = 176;
byte controllerNummer[] = {20,21,22,23,24,25,26,27,28,29,30,71,72,73,74,75,76};

int potiWert[16];
byte controllerWert[16];
byte controllerWertAlt[16];

byte taste[] = {LOW, LOW};
byte tasteAlt[] = {LOW, LOW};
boolean tasteGedrueckt[] = {false, false};
byte tastePin[] = {8, 10};
byte tasteLEDPin[] = {9, 11};

byte bit1 = 0;
byte bit2 = 0;
byte bit3 = 0;

void setup() {
//Select-Pins 4051s
pinMode(2, OUTPUT);
pinMode(3, OUTPUT);
pinMode(4, OUTPUT);

//Sensoren
pinMode(5, OUTPUT); //Trigger 1
pinMode(6, INPUT); //Echo 1
pinMode(7, OUTPUT); //Trigger 2
pinMode(12, INPUT); //Echo 2

pinMode(8, INPUT); // Taster 1
pinMode(9, OUTPUT); // Taster 1 LED
pinMode(10, INPUT); // Taster 2
pinMode(11, OUTPUT);// Taster 2 LED

Serial.begin(31250);
}

void loop() {
// Schleife fragt alle Potis an den 4051 Multiplexern ab
for (byte i = 0; i <= 7; i++) {
bit1 = bitRead(i,0);
bit2 = bitRead(i,1);
bit3 = bitRead(i,2);

digitalWrite(2, bit1);
digitalWrite(3, bit2);
digitalWrite(4, bit3);

potisAbfragen(i,A0);
potisAbfragen(i+8,A1);
}

sensorAbfragen(5, 6, controllerNummer[14]);
sensorAbfragen(7, 12, controllerNummer[16]);

tasteAbfragen(0 ,controllerNummer[12]);
tasteAbfragen(1 ,controllerNummer[13]);
}

void potisAbfragen(byte zaehler, int analogPin) {
potiWert[zaehler] = 0.2 * potiWert[zaehler] + 0.8 * analogRead(analogPin);
controllerWert[zaehler] = map(potiWert[zaehler],0,1023,0,127);
if (controllerWert[zaehler] != controllerWertAlt[zaehler]) {
sendeMIDI(controlChange, controllerNummer[zaehler], controllerWert[zaehler]);
controllerWertAlt[zaehler] = controllerWert[zaehler];
}
}

void sensorAbfragen(byte triggerPin, byte echoPin, byte ctrlNummer) {
digitalWrite(triggerPin, LOW);
delayMicroseconds(2);
digitalWrite(triggerPin, HIGH);
delayMicroseconds(10);
digitalWrite(triggerPin, LOW);

long sensorWert = pulseIn(echoPin, HIGH);

if ( (sensorWert <= 2000) && (sensorWert >= 200)) {
byte sensorMIDI = map(sensorWert,2000,200,0,127);
sendeMIDI(controlChange, ctrlNummer, sensorMIDI);
}
}

void tasteAbfragen(byte x, byte ctrlNummer) {
taste[x] = digitalRead(tastePin[x]);

if (taste[x] == HIGH && tasteAlt[x] == LOW) {
if(tasteGedrueckt[x] == false){
sendeMIDI(controlChange, ctrlNummer, 127);
digitalWrite(tasteLEDPin[x], HIGH);
tasteGedrueckt[x] = true;
}
else {
sendeMIDI(controlChange, ctrlNummer, 0);
digitalWrite(tasteLEDPin[x], LOW);
tasteGedrueckt[x] = false;
}
}
tasteAlt[x] = taste[x];
}

void sendeMIDI(byte statusByte, byte dataByte1, byte dataByte2) {
Serial.write(statusByte);
Serial.write(dataByte1);
Serial.write(dataByte2);
}

Did you figure this out by any chance? I'm working on a similar project and having trouble. Nobody responded to my original post so I wanted to see if maybe you had developed your project further and could share.