The results were erratic without smoothing, so this was applied taking an average from 5 readings (10 was too unresponsive).
At present the IR sensor only gives readings of approximately 20 - 80 (once converted to MIDI friendly numbers) when 0 - 127 is expected and wanted. I assume that the IR sensor needs to be calibrated, but I'm not sure how to do this yet.
The sensor was connected to analog input 0.
Arduino code is below. It makes use of elements taken from the smoothing example sketch, and an IR sketch found at http://luckylarry.co.uk/arduino-projects/arduino-using-a-sharp-ir-sensor-for-distance-calculation/ as well as some original elements and the Arduino MIDI library.
#include <MIDI.h>
const int numReadings = 5; //the readings from the analog input
int readings[numReadings]; //the index of the current reading
int index = 0; //the index of the current reading
int total = 0; //the running total
int average = 0; //the average
int midiout = 0; //midi out
int inputPin = 0; // analog pin for reading the IR sensor
void setup() {
MIDI.begin(); // Launch MIDI with default options
for (int thisReading = 0; thisReading < numReadings; thisReading++)
readings [thisReading] = 0;
}
void loop() {
float volts = analogRead(inputPin)*0.0048828125; // value from sensor * (5/1024) - if running 3.3.volts then change 5 to 3.3
float distance = 65*pow(volts, -1.10); // worked out from graph 65 = theretical distance / (1/Volts)S - luckylarry.co.uk
delay(100); // arbitary wait time.
total= total - readings[index]; //subtract the last reading
readings[index] = analogRead(inputPin); //read from the sensor
total= total + readings[index]; //advance to the next position in the array
index = index +1;
if (index >= numReadings) //if we're at the end of the array...
index = 0; //...wrap around to the beginning.
average = total / numReadings; //calculate the average
MIDI.sendControlChange(16, average /8, 1);
delay (1);
}
This code was used to control the distance parameter of a helicopter PD patch taken from Designing Sound by Andy Farnell:
Blue highlighted elements were newly added and consist of a simple ctlin object listening for CC messages which then control the distance slider. As it stands the distance increases as the object is moved closer to the sensor which is obviously counterintuitive, simply switching the slider settings from 0-1 to 1-0 did not rectify this.
Ultimately I get the impression that an IR sensor may not be of too much value to this project due to its inaccuracy. Perhaps an ultrasonic distance sensor would be of more use.
No comments:
Post a Comment