Browse Source

Formatted and Commented Code

master
James McKenzie 5 years ago
parent
commit
b76067d67a
1 changed files with 50 additions and 47 deletions
  1. +50
    -47
      Theremin/Theremin.ino

+ 50
- 47
Theremin/Theremin.ino View File

@@ -3,103 +3,106 @@
*@lang C89 Based
*@contact lunarised@outlook.com
*/
int mode;
int mode;
void setup() {
pinMode(6, INPUT_PULLUP);
pinMode (13, OUTPUT);
pinMode(8, OUTPUT);
pinMode (7, INPUT_PULLUP);

pinMode(5, INPUT_PULLUP);
Serial.begin (9600); //set up a serial connection with the computer

pinMode(11, OUTPUT); //the trigger pin
pinMode(12, INPUT); //echo pin
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

}

void loop() {
if (digitalRead(6) ==HIGH){
if (digitalRead(6) ==HIGH){
mode = 0;
}else{
}else{
mode = 1;
}
float note = 0;
if (digitalRead(7) == LOW || digitalRead(5) == LOW){
if (mode == 0){
note = tune(getNote());
}
else{
note = getNote();
}
if (digitalRead(5)==LOW){ //if the octave button is pressed
note *= 2;
}
if (mode == 0){
note = tune(getNote());
}
else{
note = getNote();
}
if (digitalRead(5)==LOW){ //if the octave button is pressed
note *= 2;
}
digitalWrite(13, HIGH);
Serial.print(note); //print frequency
Serial.println(" hz"); //print units after frequency
Serial.print(note); //print frequency
Serial.println(" hz"); //print units after frequency
tone(8, note);
}
else{
noTone(8);
}
}
/*
* Method that uses an UltraSonic sensor to calculate a frequency
* @return A Frequency generated by a sensor
*/
float getNote(){


float getNote()
{
float echoTime;
float calculatedDistance;
digitalWrite(11, HIGH);
delayMicroseconds(20);
digitalWrite(11, LOW);

echoTime = pulseIn(12, HIGH);
calculatedDistance = echoTime / 8;
calculatedDistance += 110;
if (calculatedDistance > 1760){ //git
calculatedDistance = 1760;
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
}

return calculatedDistance;
}

/*
* 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
*/
float tune(float toTune){
if (toTune> 523.75){ //C5
if (toTune> 523.75){ //C5
return 523.75;
}
if (toTune> 493.88){ //B4
if (toTune> 493.88){ //B4
return 493.88;
}
if (toTune> 440){ //A4
if (toTune> 440){ //A4
return 440;
}
if (toTune> 392){ //G4
if (toTune> 392){ //G4
return 392;
}
if (toTune> 349.23){ //F4
if (toTune> 349.23){ //F4
return 349.23;
}
if (toTune> 329.63){ //E5
if (toTune> 329.63){ //E5
return 329.63;
}
if (toTune> 293.66){ //D4
if (toTune> 293.66){ //D4
return 293.66;
}
if (toTune> 261.63){ //C4
if (toTune> 261.63){ //C4
return 261.63;
}
if (toTune> 246.94){ //B3
if (toTune> 246.94){ //B3
return 246.94;
}
if (toTune> 220){ //A3
if (toTune> 220){ //A3
return 220;
}
if (toTune> 196){ //G3
if (toTune> 196){ //G3
return 196;
}
if (toTune> 174.61){ //F3
if (toTune> 174.61){ //F3
return 174.61;
}
}

Loading…
Cancel
Save