Hello Everyone,
That is my first post here, however I've been working with Arduino for a couple of years.
This project isn't a new one. I did it some time ago. I hope it could be interesting for you.
Zheremino it's a Optical Midi Theremin. In this project I wanted to control synthesizers from a sensor with a Theremin-like method. That is, playing the instrument without touching it. Despite the Theremin, this project don't play continous notes, without space between B3 and C4, for instance. I rather play with the natural keyboard transitions between notes. Due that, playing a scale sounds easy and you don't have to worry too much about the pitch, if the synth is rigth tunned, of course.
Here you will find a couple of videos about how it works:
And here comes the code,as I wrote then:
#define Nmedidas 38 //Define how many measures will count for averaging.
#define notaBaja 59 //Lowest note
#define notaAlta 72 //Highest note
int value;
int avg;
int Navg;
int sum;
int i;
int nota;
void setup(){
Serial.begin(31250); // MIDI baud rate
}
void loop(){
// measure and averaging the sensor
sum=0;
for (i=1; i<=Nmedidas; i++){
value=analogRead(0);
sum = sum + value;
}
avg =map( sum / Nmedidas,40,400,notaBaja,notaAlta);
//Case 1: If there are no note changing
if((avg==Navg)||((avg<notaBaja)&&(Navg<notaBaja)))
{
goto fin;
}
//Case 2: There were note playing and now must be off “note off”
if((avg<=notaBaja)&&(Navg>=notaBaja)){
Navg=avg;
noteOn(0×80,nota,0);
delay(30);
goto fin;
}
//Case 3: A new note must be played
//Send “note Off” and play the new onel
if((avg>=notaBaja)&&(avg<=notaAlta)){
Navg=avg;
noteOn(0×80,nota,00);
nota=avg;
noteOn(0×90,nota,90);
delay(100);
}
//Just an end label
fin:
;
}
// plays a MIDI note.
void noteOn(char cmd, char data1, char data2) {
Serial.print(cmd,BYTE);
Serial.print(data1,BYTE);
Serial.print(data2,BYTE);
}
Be happy!