Ultra sonic sensor to trigger midi

Hello!

Im really new in programming arduino, and im wondering if anyone could help with a code for ultrasonic sensor to trigger a midi to play?

Hi Tommy,

welcome to the Arduino-Forum.

Yes more than one user can help with that.
My understanding of this forum is: help others that they can help themselves.
Teach fishing and cooking instead of serving cooked fish.

So it is up to you to decide with wich part you want to begin with
ultra-sonic sensor or midi?

There are a lot of more questions like
when I want to do midi which microcontroller would yu recommend?
and a lot of questions like "where can I find ....?"

If you expected - that, from such a generalised question like you have posted above - somebody will write down a 10 pages "from beginner to ultra-sonic midi-ing" I have to disapoint you. This will not happen.

You can ask as many questions as you like.
As long as your postings show some own effort in learning programming and electronics
you will always get answers. There are some threads that are on post number 256 or more.

You should give an overview what you want to do all in all at the end.
Is it just a triad fanfare you want to play as a doorbell
or do you plan a highly interactive art-work where - depending on - what do I know for factors - a polyphonic meldody is created interactively by a visitor.

best regards Stefan

other users have done stuff with US sensors and midi. search for "ultrasound midi" in the top bar

image

This sketch uses an HC-SR04. With it, given a detection event, the on-board LED ('pin 13') goes On. Instead of turning on the LED, or in addition to that, you would/could do your MIDI thing.
(I know nothing of MIDI.)

#define echoPin 2 // attach pin D2 Arduino to pin Echo of HC-SR04
#define trigPin 3 // attach pin D3 Arduino to pin Trig of HC-SR04

// defines variables
float duration; // var for the duration of sound wave travel
float distance; // var for the distance measurement

void setup() 
{
  pinMode(trigPin, OUTPUT); // Sets the trigPin as an OUTPUT
  pinMode(echoPin, INPUT);  // Sets the echoPin as an INPUT

  pinMode(13,OUTPUT);
  digitalWrite(13,LOW);

  Serial.begin(19200);
  Serial.print("\r\n ok \r\n\r\n");
}

void loop() 
{
  // Clears the trigPin condition
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  // Sets the trigPin HIGH (ACTIVE) for 10 microseconds
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);
  // Reads the echoPin, returns the sound wave travel time in microseconds
  duration = pulseIn(echoPin, HIGH);
  // Calculating the distance
  distance = duration * 0.034 / 2.0; // Speed of sound wave divided by 2 (go and back)
  
  // Displays the distance on the Serial Monitor
  Serial.println(duration);
  Serial.print("Distance: ");
  Serial.print(distance);
  Serial.println(" cm");

  if(distance < 90)
  {
    digitalWrite(13,HIGH);
    delay(1000);
    digitalWrite(13,LOW);
  }
  else
  {
    digitalWrite(13,LOW);
  }
}

The previous sketch, though it 'worked', had some int/long/float inconsistencies. The Serial() stuff could be commented/deleted but is included for purposes of verification and calibration.

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