Pages: [1]   Go Down
Author Topic: IR DISTANCE to MIDI - Help  (Read 826 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Newbie
*
Karma: 0
Posts: 7
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
//  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);
}
Logged

Manchester (England England)
Offline Offline
Brattain Member
*****
Karma: 634
Posts: 34519
Solder is electric glue
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
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
 
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 7
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Wow cheers for your quick reply.

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

Code:
//  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!
Logged

Manchester (England England)
Offline Offline
Brattain Member
*****
Karma: 634
Posts: 34519
Solder is electric glue
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

0
Offline Offline
Newbie
*
Karma: 0
Posts: 7
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thats brilliant thank you so much. Worked a treat!
Logged

Pages: [1]   Go Up
Jump to: