Go Down

Topic: IR DISTANCE to MIDI - Help (Read 892 times) previous topic - next topic

jimpendo

Hello Everyone. I am using Noah Stahls code to attempt to take data from a long range ir distance and spit it out as a midi cc. I have searched forums, interwebs, etc, and nothing. I have tried various approaches, but figured I would just post the below code as its the basic beginnings so I can see where I am going wrong even at the start....

Noah's code (in its original rendition) is spitting out data perfectly in inches (which come close enough to 0-127)
And I have successfully run the midi example to make sure im outputting properly out of tx-1 and through the midi interface.

Any help would be greatly appreciated, but please beware you are dealing with an absolute beginner!

Thanks team

James

Code: [Select]
//  Noah Stahl
//  5/25/2011
//  http://arduinomega.blogspot.com
//  Arduino Mega 2560
//This sketch is used to test the Sharp Long Range Infrared Sensor.
//The sensor output is attached to analog pin 15. Once the distance
//is calculated, it is printed out to the serial monitor.

#define sensorIR 1               //Must be an analog pin
float sensorValue, inches, cm;    //Must be of type float for pow()

void setup() {
  Serial.begin(31250);
 
}

void loop() {
  sensorValue = analogRead(sensorIR);
  inches = 4192.936 * pow(sensorValue,-0.935) - 3.937;
  //cm = 10650.08 * pow(sensorValue,-0.935) - 10;
  delay(100);
 
  Serial.print(0xb0);
  Serial.print("inches: ");
  Serial.print(127);
}

Grumpy_Mike

Quote
Serial.print(0xb0);
  Serial.print("inches: ");
  Serial.print(127);

Printing strings to a MIDI output is going to screw you up.
Using Serial.print is going to screw you up.
Use
Code: [Select]
Serial.write(0xb0); // CC message for channel 1
  Serial.write(0x5); // or what ever CC number you want to write to
  Serial.write(inches & 0x7f); // keep the inches from going over 127


jimpendo

Wow cheers for your quick reply.

I tried this, but it has compiling errors, does this look right to you?

Code: [Select]
//  Noah Stahl
//  5/25/2011
//  http://arduinomega.blogspot.com
//  Arduino Mega 2560
//This sketch is used to test the Sharp Long Range Infrared Sensor.
//The sensor output is attached to analog pin 15. Once the distance
//is calculated, it is printed out to the serial monitor.

#define sensorIR 1               //Must be an analog pin
float sensorValue, inches, cm;    //Must be of type float for pow()

void setup() {
  Serial.begin(31250);
 
}

void loop() {
  sensorValue = analogRead(sensorIR);
  inches = 4192.936 * pow(sensorValue,-0.935) - 3.937;
  //cm = 10650.08 * pow(sensorValue,-0.935) - 10;
  delay(100);
 
Serial.write(0xb0); // CC message for channel 1
  Serial.write(0x5); // or what ever CC number you want to write to
  Serial.write(inches & 0x7f); // keep the inches from going over 127
}


Errors:

sketch_may01a.cpp: In function 'void loop()':
sketch_may01a:24: error: invalid operands of types 'float' and 'int' to binary 'operator&'

Cheers!

Grumpy_Mike

Change that last line to:-
Code: [Select]
Serial.write(int(inches) & 0x7f); // keep the inches from going over 127

jimpendo

Thats brilliant thank you so much. Worked a treat!

Go Up
 


Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

Arduino
via Egeo 16
Torino, 10131
Italy