hi,
i am very new to arduino. sort of experienced in builiding of analog curcuitry and own musicinstruments.
what i want to do is the following:
the arduino shall read a lightresistor and send pitchbend for a finegrained midicontroller. while searching i found a piece of code which does this.
//////////////////////////////////////////////
// a midi pitch bend implementation
// with noise smoothing
//////////////////////////////////////////////
#include "MIDI.h"
int sensorPin = 0; /i added this for my sensor
const byte MIDI_CH = 1;
// how many sensor samples are taken to compute the average
// the accelerometer is quite noisy
const byte OVERSAMPLE = 50;
const byte PITCH_BEND = 6;
// the max value you can expect from the sensor you are using
// it depends on many factors, which sensor, which tension applied,
// which reference configuration on Arduino, etc..
const int MAX_SENSOR_VALUE = 880;
// mobile average samples array
int values[OVERSAMPLE];
int last_average=0;
// the missing function in midi library
void midi_pitch_bend(int value, byte channel) {
byte lsb;
byte msb;
lsb = value & 0x7f;
msb = value >> 7;
MIDI.send(PITCH_BEND, lsb, msb, channel);
}
void setup()
{
const int n=41;
// If you wanted a slightly better resolution from the sensor, you might connect
// the 3.3v pin to aref pin and uncomment the following instruction.
// but read the warning here before hacking: arduino.cc/en/Reference/AnalogReference
// analogReference(EXTERNAL);
MIDI.begin();
// a wannabe..
MIDI.sendNoteOn(n, 127, MIDI_CH);
MIDI.sendNoteOn(n + 7, 127, MIDI_CH);
MIDI.sendNoteOn(n + 12, 127, MIDI_CH);
MIDI.sendNoteOn(n + 16, 127, MIDI_CH);
MIDI.sendNoteOn(n + 19, 127, MIDI_CH);
MIDI.sendNoteOn(n + 24, 127, MIDI_CH);
delay(2500);
MIDI.sendNoteOff(n, 0, MIDI_CH);
MIDI.sendNoteOff(n + 7, 0, MIDI_CH);
MIDI.sendNoteOff(n + 12, 0, MIDI_CH);
MIDI.sendNoteOff(n + 16, 0, MIDI_CH);
MIDI.sendNoteOff(n + 19, 0, MIDI_CH);
MIDI.sendNoteOff(n + 24, 0, MIDI_CH);
}
void loop()
{
int value;
int i;
long sum;
int average;
int delta;
sum=0;
// this array works as FIFO
// shift all values one position to the left, making room for the new reading,
// in the meanwhile it sums for the average
for (i=0; i < OVERSAMPLE-1; i++) {
values=values[i+1];
sum+=values;
}
values[OVERSAMPLE-1]=analogRead(0);
sum+=values[OVERSAMPLE-1];
average=sum / OVERSAMPLE;
// compute a trend compared to the previous
delta=average - last_average;
// save the current for the next iteration
last_average=average;
// try to limit flooding the midi channel
if (abs(delta) > 0) {
// note: you have to scale to 16384 because pitch bend messages
// have 14 bits of data instead of 7 like most midi messages
value=map(average, 0, MAX_SENSOR_VALUE, 0, 8192);
midi_pitch_bend(value, MIDI_CH);
// too much messages worsen things,
// it depends also on the midi implementation on the synth
delay(1);
}
}
however this code reads only one sensor and is not yet full understandable for me. by trial and error i managed to add the information about which input and learned about the right mapping for my device.
it is from the bottom of this page
arduino.cc/cgi-bin/yabb2/YaBB.pl?action=print;num=1265084001
i need now to learn how to integrate more sensors/functions.
the second one shall be read another fotoresistor that is next to the tempo led of a digital audiodelay. calculate the tempo and send midiclock. maybe that is going to be a very complicated thing so an alternaternive would just add another input which sends a given controller besides pitchbend.
what would be the best way to get on the right track?
i tried to analyse the code but at this point my knowledge is not enough.
so i would be thankful for some help.