Here's a very lame schematic of how I hooked it up:
and here's the code (hacked from the arduino Smoothing example):
#include <math.h>
// Define the number of samples to keep track of. The higher the number,
// the more the readings will be smoothed, but the slower the output will
// respond to the input. Using a #define rather than a normal variable lets
// use this value to determine the size of the readings array.
#define NUMREADINGS 3
int readings[NUMREADINGS]; // the readings from the analog input
int index = 0; // the index of the current reading
int total = 0; // the running total
int average = 0; // the average
int val = 0;
int inputPin = 0;
byte vol = 0;
byte status_byte = 176; // control change on channel 1
byte cc_number = 7; // value for control number = volume
int to_log[128]; // log LUT
void setup()
{
Serial.begin(31250); // set MIDI baud rate
// builds a lookup table - using log sounds smoother to me...
for (int i=0; i < 128; i++) {
to_log[i] = (int) floor(pow((float)i/127.0, .4545) * 127);
}
for (int i = 0; i < NUMREADINGS; i++)
readings[i] = 0; // initialize all the readings to 0
}
void sendVolume(byte data) {
Serial.print(status_byte); // send status byte
Serial.print(cc_number); // send CC number
Serial.print(data); // send data value
}
void loop()
{
total -= readings[index]; // subtract the last reading
readings[index] = analogRead(inputPin); // read from the sensor
total += readings[index]; // add the reading to the total
index = (index + 1); // advance to the next index
if (index >= NUMREADINGS) { // if we're at the end of the array...
index = 0;
if ( val != average ) {
// ...wrap around to the beginning
if ( average > 127 ) { average = 127; }
sendVolume(to_log[average]);
}
val = average;
}
average = total / NUMREADINGS; // calculate the average
}