Help interfacing between HC SR04 and Hairless MIDI Program

Hey.

At the moment I am attempting to make a midi controller using an Arduino and the HC SR04 ulrasonic range finder.
When I attempt to use the Hairless MIDI<->Serial bridge, I receive the following error message repeatedly: ‘Error got unexpected data byte 0x**’ where the star is either a letter or number (changes with each instance of the error).

As this is my first project, i have really no idea about what is going wrong and my attempts to google the subject have been fruitless. If I am simply inept and someone else finds a good link, please provide it.

My current coding for the Arduino is

const int trigPin = 2;
const int echoPin = 4;
 
void setup() {
  // initialize serial communication:
  Serial.begin(9600);
}
 
void loop()
{
  // establish variables for duration of the ping, 
  // and the distance result in inches and centimeters:
  long duration, inches, cm;
 
  // The sensor is triggered by a HIGH pulse of 10 or more microseconds.
  // Give a short LOW pulse beforehand to ensure a clean HIGH pulse:
  pinMode(trigPin, OUTPUT);
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);
 
  // Read the signal from the sensor: a HIGH pulse whose
  // duration is the time (in microseconds) from the sending
  // of the ping to the reception of its echo off of an object.
  pinMode(echoPin, INPUT);
  duration = pulseIn(echoPin, HIGH);
 
  // convert the time into a distance
  inches = microsecondsToInches(duration);
  cm = microsecondsToCentimeters(duration);
  
  Serial.print(inches);
  Serial.print("in, ");
  Serial.print(cm);
  Serial.print("cm");
  Serial.println();
  
  delay(100);
}
 
long microsecondsToInches(long microseconds)
{
  // According to Parallax's datasheet for the PING))), there are
  // 73.746 microseconds per inch (i.e. sound travels at 1130 feet per
  // second).  This gives the distance travelled by the ping, outbound
  // and return, so we divide by 2 to get the distance of the obstacle.
  // See: http://www.parallax.com/dl/docs/prod/acc/28015-PING-v1.3.pdf
  return microseconds / 74 / 2;
}
 
long microsecondsToCentimeters(long microseconds)
{
  // The speed of sound is 340 m/s or 29 microseconds per centimeter.
  // The ping travels out and back, so to find the distance of the
  // object we take half of the distance travelled.
  return microseconds / 29 / 2;
}

That was taken from I site i can no longer find, and when using the serial monitor gives accurate readings.

I realise that this may be something extremely obvious, but as I am new hopefully you will take the time to type it.

Thanks

Additional: I am using the loopmidi program in the midi out slot of hairless which is connected as a controller for volume in FL studio via a Mod X knob in sytrus. I am only after a volume change as for my project the pitch is predetermined if that makes this any simpler.

You're not sending any MIDI data... and your baud rate is wrong for MIDI...
Your setup should look like this
If you're just starting I would use the MIDI library for arduino
http://playground.arduino.cc/Main/MIDILibrary
and then your setup should look like this:

void setup() {
  MIDI.begin();
  Serial.begin(115200);
}

then you would have to send a control change message for volume, you have to see for yourself which CC fruity responds to... in the general MIDI table volume is CC 7.
With the MIDI library you send it with

MIDI.sendControlChange(controlNumber, value, channel);

controlNumber would be the CC for volume, CC 7 or whatever fl wants. Value is the parameter you want to vary with your sensor, from 0 min to 127 max... and channel any from 0 to 15(or 1 to 16 I don't remember...)
You may have to map your sensor value, if it's bigger than 127 you should make it smaller, and viceversa

Supposing it's max is 20 you would have to map it with

cm = map(cm, 0, 20, 0, 127);

and then

cm = map(cm, 0, 20, 0, 127);
MIDI.sendControlChange(7, cm, 1);

And before sending MIDI delete or comment those lines with Serial.print, and also delete the delay

Thank you so much for your help.

I'm running into a bit of a problem though.

[1st problem fixed] For those inept like me who may read this later, you have to manually import the library in the program using sketch/import library/add library

I'm receiving the following error message from the program when I attempt to compile:
~~ ~~sketch_oct08a.ino: In function 'void setup()': sketch_oct08a:5: error: 'MIDI' was not declared in this scope sketch_oct08a.ino: In function 'void loop()': sketch_oct08a:41: error: 'MIDI' was not declared in this scope~~ ~~
I imagine this is because I have not installed the library properly. Currently the file path is Martin/My Documents/Arduino/Libraries which includes the unzipped midi folder and readme.txt
My code is as follows-hopefully there aren't too many now redundant things.
```
~~const int trigPin = 2;
const int echoPin = 4;

void setup() {
 MIDI.begin();
 Serial.begin(115200);
}

void loop()
{
 // establish variables for duration of the ping,
 // and the distance result in inches and centimeters:
 long duration, inches, cm;

// The sensor is triggered by a HIGH pulse of 10 or more microseconds.
 // Give a short LOW pulse beforehand to ensure a clean HIGH pulse:
 pinMode(trigPin, OUTPUT);
 digitalWrite(trigPin, LOW);
 delayMicroseconds(2);
 digitalWrite(trigPin, HIGH);
 delayMicroseconds(10);
 digitalWrite(trigPin, LOW);

// Read the signal from the sensor: a HIGH pulse whose
 // duration is the time (in microseconds) from the sending
 // of the ping to the reception of its echo off of an object.
 pinMode(echoPin, INPUT);
 duration = pulseIn(echoPin, HIGH);

// convert the time into a distance
 inches = microsecondsToInches(duration);
 cm = microsecondsToCentimeters(duration);
 
 //Serial.print(inches);
 //Serial.print("in, ");
 //Serial.print(cm);
 //Serial.print("cm");
 //Serial.println();
 
 cm = map(cm, 0, 250, 0, 127);
 MIDI.sendControlChange(7, cm, 1);

}

long microsecondsToInches(long microseconds)
{
 // According to Parallax's datasheet for the PING))), there are
 // 73.746 microseconds per inch (i.e. sound travels at 1130 feet per
 // second).  This gives the distance travelled by the ping, outbound
 // and return, so we divide by 2 to get the distance of the obstacle.
 // See: http://www.parallax.com/dl/docs/prod/acc/28015-PING-v1.3.pdf
 return microseconds / 74 / 2;
}

long microsecondsToCentimeters(long microseconds)
{
 // The speed of sound is 340 m/s or 29 microseconds per centimeter.
 // The ping travels out and back, so to find the distance of the
 // object we take half of the distance travelled.
 return microseconds / 29 / 2;
}~~

```
Thanks
EDIT:Have now also tried it under the file path Computer/OS (C:)/Program Files (x86)/Arduino/Hardware/Libraries
Where I have moved the libraries folder over from the previous place I had it as this folder had no premade library folder.

Testing now

New problem (yay). When I assign the modx to the controller, the modx does not change with movement closer and further from the sensor. Instead, when plying the pattern on the program a new pitch is introduced. Is that to do with the MIDI number from "MIDI.sendControlChange(7, cm, 1);"?

Also, the sensor itself has become quite loud and rather than clicking like I heard before using midi, it now makes a pitch itself. This pitch changes depending on how close the thing it senses is with closer being higher. I don't really mind this as I can amplify other parts of my project to hide it, but in the event this may help someone help me, I thought i should include it.

Additionally, there is an error message of "+220.545 - Warning: got a status byte when we were expecting 3 more data bytes, sending possibly incomplete MIDI message 0xff" within hairless which may also be the problem.

Any help is greatly appreciated.
Thanks

Apologies for double post-last one was getting full with all me edits.

Solved my last problem-had the wrong baud rate in the Hairless program making everything whacked out. This is now working as intended, thankyou very much capicoso.

I’m now 1/3 done with this project :smiley:

EDIT: I have made a thread over in programming for help with the next stage (using eight sensors rather than 1).
Any help is appreciated with that http://forum.arduino.cc/index.php?topic=192141.0

Thanks

I was just going to tell you to check that, but you got it :slight_smile:
It should come by default with the proper baud rate but you never know...
Cool, no problem