Go Down

Topic: Please help me with midi output from ir distance sensor! (Read 1 time) previous topic - next topic


Hi Everyone.

I'm are doing an lighting installation for an event in Sydney this coming friday, and kinda freakin because a component of the work is using an arduino, and naturally I jumped right in with the "ive seen others do it, so Im sure I can" attitude... always a positive attitude to take imo.

We have 5 prox sensors running into an arduino uno, which is spitting out midi over tx and through a midi-usb converter and into the computer, which is controlling madrix for the lights.

So basically... you can see from the code below what I am starting with (full credit to Noah Stahl whos code this essentially is). The next thing I need to is add in 4 more sensors.. can i do that just by defining them at the start and then mirroring the code below?

Secondly, the sensors are very jumpy, so I need to do some mad smoothing, I have looked around on how to do that but i'm at a bit of a loss...

And thirdly, we just need one more control (just a note change or anything like that, not continuous controll) that fires once every two minutes, how would I do that do you think?

Heres the code i'm working with:

//  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() {

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

And here is our installation website http://digitalwattle.co.nz/
There is a sweet test video if you want to see what we are doing!

Thanks to anyone who replies, I have to say I'm loving this community, will chuck the arduino logo on our sponsors list on our installation lightbox!




May 24, 2012, 08:10 am Last Edit: May 24, 2012, 08:17 am by AWOL Reason: 1
The simplest way is not to replicate code, but to use a for loop.
Make sensorIR an array, and in "loop" have a for loop that addresses each in out pin in turn.
Smoothing could be any number of algorithms, and the best will depend on the sensor.
Try a rolling average over, say 10 readings.

Another technique, which uses less memory, is to keep a ruuning value, and for each new reading, multiply the running total by, say 9/10 and add 1/10th of the new reading.

Please do not cross post - I have deleted the other thread on this subject.
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.
I speak for myself, not Arduino.


Are you using SHARP IR sensors ? if so did you put a bypass capacitor as suggested on the datasheet ?


the third question should look something like i hope this helps

Code: [Select]

#define delay 120 //(in seconds)
#define duration 2 //(duration of tone in seconds)
unsigned long time;
unsigned long prevtime;
unsigned long starttone;
boolean tone = False;

time = millis()
if (time-prevtime>=(delay*1000))
Serial.write(0x90); //note on on channel 0
Serial.write(0x3C); // middle C (not 100% sure)
Serial.write(0x45); // velocity
Prevtime = time;
starttone = time;
tone = TRUE;

if (time-tonetime>=(duration*1000)&&tone==TRUE)
Serial.write(0x80) ;// note off
Serial.write(0x3C); // middle C
Serial.write(0x45); // velocity (does't matter what you fill in here)

Go Up