Ultrasonic Sensor MIDI Control
Today, in preparation for the exhibition, I went back and made adjustments to the previous ultrasonic sensor MIDI tests, based on the knowledge gained from the project.
Now, these sensors work much better with Hairless MIDI, and will be available at the exhibition for the attendees to interact with. I’ve programmed the sensors to work in two ways:
Beam-Break
Simply breaking the “beam” of the sensor at any distance will trigger an effect.
Distance
The intensity of an effect will increase/decrease based on proximity to the sensor.
Code used for ‘Break’ version:
#include <MIDI.h> #include <midi_Defs.h> #include <midi_Message.h> #include <midi_Namespace.h> #include <midi_Settings.h>
/*List of Ultrasonic Sensors*/ //Sensor 1 #define trigPin1 3 #define echoPin1 2
/*List of integers for Sensor.*/ long duration, distance, Sensor1;
/*List of values to be stored for each sensor.*/ int val = 0; int lastVal = 0; int breakPoint = 10; int sensorVal = 0;
//Sends out converted MIDI value to Hairless. void MIDImessage(byte command, byte channel, byte output) { Serial.write(command); Serial.write(channel); Serial.write(output); }
void SonarSensor(int trigPin,int echoPin) { /*Sends out pulse for the sensor to read.*/ digitalWrite(trigPin, LOW); delayMicroseconds(2); digitalWrite(trigPin, HIGH); delayMicroseconds(10); digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH); distance = (duration/2) / 29.1; }
long microsecondsToCentimetres(long microseconds) { //Converts values into centimetres. return microseconds / 29 / 2; }
void setup() { Serial.begin(9600);
/*Sensor 1*/ pinMode(trigPin1, OUTPUT); pinMode(echoPin1, INPUT);
}
void loop() {
//Sensor 1 SonarSensor(trigPin1, echoPin1); Sensor1 = distance;
val = distance/8;
if (val != lastVal) { if (val <= breakPoint){ sensorVal = 127; MIDImessage(176,1,sensorVal); }
else { sensorVal = 0; }
MIDImessage(176,1,sensorVal); }
lastVal = val;
delay(10); }
Code used for ‘Proximity’ Version:
#include <MIDI.h> #include <midi_Defs.h> #include <midi_Message.h> #include <midi_Namespace.h> #include <midi_Settings.h>
/*List of Ultrasonic Sensors*/ //Sensor 1 #define trigPin1 3 #define echoPin1 2
/*List of integers for Sensor.*/ long duration, distance, Sensor1;
/*List of values to be stored for each sensor.*/ int val = 0; int lastVal = 0; int difference = val - lastVal; int sensorVal = 0;
//Sends out converted MIDI value to Hairless. void MIDImessage(byte command, byte channel, byte output) { Serial.write(command); Serial.write(channel); Serial.write(output); }
void SonarSensor(int trigPin,int echoPin) { /*Sends out pulse for the sensor to read.*/ digitalWrite(trigPin, LOW); delayMicroseconds(2); digitalWrite(trigPin, HIGH); delayMicroseconds(10); digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH); distance = (duration/2) / 29.1; }
long microsecondsToCentimetres(long microseconds) { //Converts values into centimetres. return microseconds / 29 / 2; }
void setup() { Serial.begin(9600);
/*Sensor 1*/ pinMode(trigPin1, OUTPUT); pinMode(echoPin1, INPUT);
}
void loop() {
//Sensor 1 SonarSensor(trigPin1, echoPin1); Sensor1 = distance;
val = distance/8;
if (lastVal != val) {
if (val == 0) { MIDImessage(176,1,127); }
else if (val == 1) { MIDImessage(176,1,100); }
else if (val == 2) { MIDImessage(176,1,80); }
else if (val == 3) { MIDImessage(176,1,60); }
else if (val == 4) { MIDImessage(176,1,40); }
else if (val == 5) { MIDImessage(176,1,20); }
else { MIDImessage(176,1,0); }
}
lastVal = val;
delay(10); }










