Distance Sensor to single MIDI note

Hi, I'm currently doing a project where I need to make a distance sensor send a MIDI note so that it can trigger a piece of video on Resolume. I'm currently using the code below so that I can read the values from the sensor and that's all working fine and I've set it so that only at 6cm will it detect anything as I need it to be quite precise. I'm not sure how to now how to make it translate this into a MIDI note. I'm still a beginner and working through all the initial project examples but I need to get this finished soon so any help would be massively appreciated. Thank you

#define echoPin 11 
#define trigPin 12 
#define LEDPin 13 

int maximumRange = 7;  
int minimumRange = 5; 
long duration, distance; 

void setup() {
  Serial.begin(9600); //starts the serial communication via USB
  pinMode(trigPin, OUTPUT); 
  pinMode(echoPin, INPUT);
  pinMode(LEDPin, OUTPUT); //board LED for testing

}

void loop() {

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

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

  digitalWrite(trigPin, LOW);
  duration = pulseIn(echoPin, HIGH); 

  

// error handling 
  if (distance >= maximumRange || distance <= minimumRange) {
    Serial.println("-1");
    digitalWrite(LEDPin, HIGH); 
  } else {
    Serial.println(distance); 
    digitalWrite(LEDPin, LOW); 
  }

  delay(50); 
 
}

The variables duration and distance are not the same thing. You never get to convert duration into distance, in fact I can't see anything that changes distance from the value given it in the initial deceleration.

How are you communicating with your MIDI device and what type of Arduino are you using? Once we know that we can advise on how to send a MIDI note.

You may want to read this before you proceed:-
how to get the best out of this forum
It tells you how to ask a question and what information you need to supply.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.