inoProjects/Theremin/Theremin.ino

109 lines
2.4 KiB
Arduino
Raw Normal View History

2019-04-29 03:14:05 -04:00
/*
*@author James McKenzie
*@lang C89 Based
*@contact lunarised@outlook.com
*/
2019-04-29 06:22:16 -04:00
int mode;
2019-04-29 03:14:05 -04:00
void setup() {
2019-04-29 06:22:16 -04:00
pinMode(6, INPUT_PULLUP); //Mode Select Pin
pinMode (13, OUTPUT); //LED Indicator Pin
pinMode(8, OUTPUT); //Speaker Pin
pinMode (7, INPUT_PULLUP); //Play Pin
pinMode(5, INPUT_PULLUP); //+Octave Play Pin
Serial.begin (9600); //Set up a Serial Connection
pinMode(11, OUTPUT); //Trigger pin
pinMode(12, INPUT); //Echo pin
2019-04-29 05:46:33 -04:00
2019-04-29 03:14:05 -04:00
}
void loop() {
2019-04-29 06:22:16 -04:00
if (digitalRead(6) ==HIGH){
2019-04-29 05:46:33 -04:00
mode = 0;
2019-04-29 06:22:16 -04:00
}else{
2019-04-29 05:46:33 -04:00
mode = 1;
}
float note = 0;
2019-04-29 05:55:06 -04:00
if (digitalRead(7) == LOW || digitalRead(5) == LOW){
2019-04-29 06:22:16 -04:00
if (mode == 0){
note = tune(getNote());
}
else{
note = getNote();
}
if (digitalRead(5)==LOW){ //if the octave button is pressed
note *= 2;
}
2019-04-29 05:46:33 -04:00
digitalWrite(13, HIGH);
2019-04-29 06:22:16 -04:00
Serial.print(note); //print frequency
Serial.println(" hz"); //print units after frequency
2019-04-29 05:46:33 -04:00
tone(8, note);
}
else{
noTone(8);
}
2019-04-29 04:05:49 -04:00
}
2019-04-29 06:22:16 -04:00
/*
* Method that uses an UltraSonic sensor to calculate a frequency
* @return A Frequency generated by a sensor
*/
float getNote(){
2019-04-29 04:05:49 -04:00
float echoTime;
float calculatedDistance;
digitalWrite(11, HIGH);
delayMicroseconds(20);
digitalWrite(11, LOW);
echoTime = pulseIn(12, HIGH);
2019-04-29 06:22:16 -04:00
calculatedDistance = echoTime / 8; //Change this value to decrease
//the playing physical range
calculatedDistance += 110; //Minimum value
if (calculatedDistance > 1760){ //Prevent high pitched sounds
calculatedDistance = 1760; //Maximum Value
2019-04-29 03:14:05 -04:00
}
2019-04-29 04:05:49 -04:00
return calculatedDistance;
2019-04-29 03:14:05 -04:00
}
2019-04-29 06:22:16 -04:00
/*
* Method which takes a note, and brings the note down to a correct pitch
* @param toTune The Note in which you are autotuning
* @return The Note that has now been autotuned
*/
2019-04-29 05:46:33 -04:00
float tune(float toTune){
2019-04-29 06:22:16 -04:00
if (toTune> 523.75){ //C5
2019-04-29 05:46:33 -04:00
return 523.75;
}
2019-04-29 06:22:16 -04:00
if (toTune> 493.88){ //B4
2019-04-29 05:46:33 -04:00
return 493.88;
}
2019-04-29 06:22:16 -04:00
if (toTune> 440){ //A4
2019-04-29 05:46:33 -04:00
return 440;
}
2019-04-29 06:22:16 -04:00
if (toTune> 392){ //G4
2019-04-29 05:46:33 -04:00
return 392;
}
2019-04-29 06:22:16 -04:00
if (toTune> 349.23){ //F4
2019-04-29 05:46:33 -04:00
return 349.23;
}
2019-04-29 06:22:16 -04:00
if (toTune> 329.63){ //E5
2019-04-29 05:46:33 -04:00
return 329.63;
}
2019-04-29 06:22:16 -04:00
if (toTune> 293.66){ //D4
2019-04-29 05:46:33 -04:00
return 293.66;
}
2019-04-29 06:22:16 -04:00
if (toTune> 261.63){ //C4
2019-04-29 05:46:33 -04:00
return 261.63;
}
2019-04-29 06:22:16 -04:00
if (toTune> 246.94){ //B3
2019-04-29 05:46:33 -04:00
return 246.94;
}
2019-04-29 06:22:16 -04:00
if (toTune> 220){ //A3
2019-04-29 05:46:33 -04:00
return 220;
}
2019-04-29 06:22:16 -04:00
if (toTune> 196){ //G3
2019-04-29 05:46:33 -04:00
return 196;
}
2019-04-29 06:22:16 -04:00
if (toTune> 174.61){ //F3
2019-04-29 05:46:33 -04:00
return 174.61;
}
}