ultrasonic sensor midi problem

Hello , i have a problem with hairless-midiserial to convert the sensor signals to midi, in hairless , apears :
-Error: got unexpected data byte 0x0.
This is the code i use:

/*
 HC-SR04 Ping distance sensor:
 VCC to arduino 5v 
 GND to arduino GND
 Echo to Arduino pin 7 
 Trig to Arduino pin 8
 
 This sketch originates from Virtualmix: http://goo.gl/kJ8Gl
 Has been modified by Winkle ink here: http://winkleink.blogspot.com.au/2012/05/arduino-hc-sr04-ultrasonic-distance.html
 And modified further by ScottC here: http://arduinobasics.blogspot.com/
 on 10 Nov 2012.
 */


#define echoPin 7 // Echo Pin
#define trigPin 8 // Trigger Pin
#define LEDPin 13 // Onboard LED

int maximumRange = 200; // Maximum range needed
int minimumRange = 0; // Minimum range needed
long duration, distance; // Duration used to calculate distance

void setup() {
 Serial.begin (9600);
 pinMode(trigPin, OUTPUT);
 pinMode(echoPin, INPUT);
 pinMode(LEDPin, OUTPUT); // Use LED indicator (if required)
}

void loop() {
/* The following trigPin/echoPin cycle is used to determine the
 distance of the nearest object by bouncing soundwaves off of it. */ 
 digitalWrite(trigPin, LOW); 
 delayMicroseconds(2); 

 digitalWrite(trigPin, HIGH);
 delayMicroseconds(10); 
 
 digitalWrite(trigPin, LOW);
 duration = pulseIn(echoPin, HIGH);
 
 //Calculate the distance (in cm) based on the speed of sound.
 distance = duration/58.2;
 
 if (distance >= maximumRange || distance <= minimumRange){
 /* Send a negative number to computer and Turn LED ON 
 to indicate "out of range" */
 Serial.println("-1");
 digitalWrite(LEDPin, HIGH); 
 }
 else {
 /* Send the distance to the computer using Serial protocol, and
 turn LED OFF to indicate successful reading. */
 Serial.println(distance);
 digitalWrite(LEDPin, LOW); 
 }
 
 //Delay 50ms before next reading.
 delay(50);
}

i hope someone help me or tell me another option to make a ultrasonic midi sensor! txx
:grin: :grin: :grin:

Try using the serial monitor to actually see the value of the distance variable. Do you always get values of zero or does it work sometimes ?

If the value is always zero then the wiring or the sensor is probably faulty. If it mostly works then the zeroes are probably an artefact. You could try using one of the ping libraries, especially NewPing, to eliminate the problem or check for zero in the code and not send it by changing the declaration of minimumRange to 1 so that zero is never sent.

Tanx now i use the New Ping Library with one example i found but now apear this error in Hairles Midi:
8.496 - Error: got unexpected data byte 0x63.
8.496 - Error: got unexpected data byte 0x6d.
8.496 - Error: got unexpected data byte 0xd.
8.496 - Error: got unexpected data byte 0xa.
This is the example code:

#include <NewPing.h>

#define TRIGGER_PIN 12 // Arduino pin tied to trigger pin on the ultrasonic sensor.
#define ECHO_PIN 11 // Arduino pin tied to echo pin on the ultrasonic sensor.
#define MAX_DISTANCE 200 // Maximum distance we want to ping for (in centimeters). Maximum sensor distance is rated at 400-500cm.

NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE); // NewPing setup of pins and maximum distance.

void setup() {
Serial.begin(115200); // Open serial monitor at 115200 baud to see ping results.
}

void loop() {
delay(50); // Wait 50ms between pings (about 20 pings/sec). 29ms should be the shortest delay between pings.
unsigned int uS = sonar.ping(); // Send ping, get ping time in microseconds (uS).
Serial.print("Ping: ");
Serial.print(uS / US_ROUNDTRIP_CM); // Convert ping time to distance and print result (0 = outside set distance range, no ping echo)
Serial.println("cm");
}

8.496 - Error: got unexpected data byte 0x63.
8.496 - Error: got unexpected data byte 0x6d.
8.496 - Error: got unexpected data byte 0xd.
8.496 - Error: got unexpected data byte 0xa.

What exactly is the midi interface on the PC expecting ? What format should it be in ?

What exactly is the midi interface on the PC expecting ? What format should it be in ?

One that doesn't involve this Serial.println("cm"); it would seem

You need to know what the midi interface is expecting to receive and to describe what is supposed to happen when the value returned by the distance sensor changes. Presumably the note produced depends on the distance but you have not said that. If so, then how does it vary ? Does the frequency increase or decrease with increasing distance ? Does the rate of change of distance have any effect ? Does the volume change with distance ?

None of the questions require you to be an Arduino expert.

Can you start by describing what the program is supposed to do ?

Can I suggest that you read up on the format of midi messages and decide what you want to send. Then you can try converting the distance returned by the ultrasonic sensor into a message that a midi device will understand. The Hairless serial to midi converter will not magically change the input into something that a midi device can deal with, merely convert it from one communication method to another and pass it on.