Below is the program i have written to convert voltage readings into MIDI data.
The brief and general nature of the program is as follows:
The Arduino checks the current state of each flex sensor when initialised
The Adruino then checks the state of the flex sensors on a contunious loop
If the state of the flex sensors has changed (becomes bent) then the Arduino will send the intended MIDI note
There is a short delay before the Note-off command is sent
The duration of the note (or length of delay) is dependant on the Y axis of the Accelerometer
The pitch of the note is dependant on the X axis of the Accelerometer
(it is worth noting that the very last lines of the program are an essential part of the program however i’m not entirely sure what it does but the program does not work without it!)
Arduino Program
// Program to read analog inputs and convert this to MIDI data before sending to DAW via USB
// Name PINS
int F1 = A0;
int F2 = A1;
int F3 = A2;
int F4 = A3;
int AX = A4;
int AY = A5;
// Names for Actions
int flex1;
int flex2;
int flex3;
int flex4;
int accelx;
int accely;
int flexState1;
int flexState2;
int flexState3;
int flexState4;
int accelStatex;
int accelStatey;
// Runs once when initialised
void setup() {
Serial.begin(31250); // MIDI runs at this rate (31250)
pinMode(A0, INPUT); // Designating PINS as INPUTS
pinMode(A1, INPUT);
pinMode(A2, INPUT);
pinMode(A3, INPUT);
pinMode(A4, INPUT);
pinMode(A5, INPUT);
flexState1 = analogRead(F1); // read the initial state
flexState2 = analogRead(F2);
flexState3 = analogRead(F3);
flexState4 = analogRead(F4);
accelStatex = analogRead(AX);
accelStatey = analogRead(AY);
}
void loop(){
flex1 = analogRead(F1); // read input value and store it
accelStatex = analogRead(AX);// read input from x axis
accelStatey = analogRead(AY); // read input from y axis
int mapped = map(accelStatex,600,200,60,127); // map x reading to between 60 – 127
int Ymapped = map(accelStatey,600,200,250,1000); // map y reading to between 250 – 1000 (1/4 sec – 1 sec)
if (flex1 < flexState1) { // the flex state has changed?
if (flex1 < 395) { // check if the flex sensor is bent
SendMidi (0x90, mapped, 120);// then send Note-on (Note-on, Note, Velocity)
}
delay(Ymapped); //hold note duration by y mapped (250-1000)
{
SendMidi (0x80, mapped, 120); // send Note-off
}
}
// Repeat will all flex sensors…..
flex2 = analogRead(F2);
accelStatex = analogRead(AX);
accelStatey = analogRead(AY);
int mappedF2 = map(accelStatex,600,200,60,127);
int XmappedF2 = map(accelStatey,600,200,60,127);
if (flex2 < flexState2) {
if (flex2 < 395) {
SendMidi (0x90, mapped +1, 120); // +1 to give a range of notes
}
delay(Ymapped);
{
SendMidi (0x80, mapped +1, 120);
}
}
flex3 = analogRead(F3);
accelStatex = analogRead(AX);
accelStatey = analogRead(AY);
int mappedF3 = map(accelStatex,600,200,60,127);
int XmappedF3 = map(accelStatey,600,200,60,127);
if (flex3 < flexState3) {
if (flex3 < 395) {
SendMidi (0x90, mappedF3 +2, 120);
}
delay(Ymapped);
{
SendMidi (0x80, mappedF3 +2, 120);
}
}
flex4 = analogRead(F4);
accelStatex = analogRead(AX);
accelStatey = analogRead(AY);
int mappedF4 = map(accelStatex,600,200,60,127);
int XmappedF4 = map(accelStatey,600,200,60,127);
if (flex4 < flexState4) {
if (flex4 < 395) {
SendMidi (0x90, mappedF4 +3, 120);
}
delay(Ymapped);
{
SendMidi (0x80, mappedF4 +3, 120);
}
}
// delay(5);
flexState1 = flex1;
flexState2 = flex2;
flexState3 = flex3;
flexState4 = flex4;
accelStatex = accelx;
accelStatey = accely;
}
void SendMidi (int cmd, int value, int vel) {
Serial.write (cmd);
Serial.write (value);
Serial.write (vel);
}