Formatted and Commented Code

This commit is contained in:
James McKenzie 2019-04-29 22:22:16 +12:00
parent deb63bfb07
commit b76067d67a

View File

@ -3,103 +3,106 @@
*@lang C89 Based *@lang C89 Based
*@contact lunarised@outlook.com *@contact lunarised@outlook.com
*/ */
int mode; int mode;
void setup() { void setup() {
pinMode(6, INPUT_PULLUP); pinMode(6, INPUT_PULLUP); //Mode Select Pin
pinMode (13, OUTPUT); pinMode (13, OUTPUT); //LED Indicator Pin
pinMode(8, OUTPUT); pinMode(8, OUTPUT); //Speaker Pin
pinMode (7, INPUT_PULLUP); pinMode (7, INPUT_PULLUP); //Play Pin
pinMode(5, INPUT_PULLUP); //+Octave Play Pin
pinMode(5, INPUT_PULLUP); Serial.begin (9600); //Set up a Serial Connection
Serial.begin (9600); //set up a serial connection with the computer pinMode(11, OUTPUT); //Trigger pin
pinMode(12, INPUT); //Echo pin
pinMode(11, OUTPUT); //the trigger pin
pinMode(12, INPUT); //echo pin
} }
void loop() { void loop() {
if (digitalRead(6) ==HIGH){ if (digitalRead(6) ==HIGH){
mode = 0; mode = 0;
}else{ }else{
mode = 1; mode = 1;
} }
float note = 0; float note = 0;
if (digitalRead(7) == LOW || digitalRead(5) == LOW){ if (digitalRead(7) == LOW || digitalRead(5) == LOW){
if (mode == 0){ if (mode == 0){
note = tune(getNote()); note = tune(getNote());
} }
else{ else{
note = getNote(); note = getNote();
} }
if (digitalRead(5)==LOW){ //if the octave button is pressed if (digitalRead(5)==LOW){ //if the octave button is pressed
note *= 2; note *= 2;
} }
digitalWrite(13, HIGH); digitalWrite(13, HIGH);
Serial.print(note); //print frequency Serial.print(note); //print frequency
Serial.println(" hz"); //print units after frequency Serial.println(" hz"); //print units after frequency
tone(8, note); tone(8, note);
} }
else{ else{
noTone(8); 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 echoTime;
float calculatedDistance; float calculatedDistance;
digitalWrite(11, HIGH); digitalWrite(11, HIGH);
delayMicroseconds(20); delayMicroseconds(20);
digitalWrite(11, LOW); digitalWrite(11, LOW);
echoTime = pulseIn(12, HIGH); echoTime = pulseIn(12, HIGH);
calculatedDistance = echoTime / 8; calculatedDistance = echoTime / 8; //Change this value to decrease
calculatedDistance += 110; //the playing physical range
if (calculatedDistance > 1760){ //git calculatedDistance += 110; //Minimum value
if (calculatedDistance > 1760){ //Prevent high pitched sounds
calculatedDistance = 1760; calculatedDistance = 1760; //Maximum Value
} }
return calculatedDistance; 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){ float tune(float toTune){
if (toTune> 523.75){ //C5 if (toTune> 523.75){ //C5
return 523.75; return 523.75;
} }
if (toTune> 493.88){ //B4 if (toTune> 493.88){ //B4
return 493.88; return 493.88;
} }
if (toTune> 440){ //A4 if (toTune> 440){ //A4
return 440; return 440;
} }
if (toTune> 392){ //G4 if (toTune> 392){ //G4
return 392; return 392;
} }
if (toTune> 349.23){ //F4 if (toTune> 349.23){ //F4
return 349.23; return 349.23;
} }
if (toTune> 329.63){ //E5 if (toTune> 329.63){ //E5
return 329.63; return 329.63;
} }
if (toTune> 293.66){ //D4 if (toTune> 293.66){ //D4
return 293.66; return 293.66;
} }
if (toTune> 261.63){ //C4 if (toTune> 261.63){ //C4
return 261.63; return 261.63;
} }
if (toTune> 246.94){ //B3 if (toTune> 246.94){ //B3
return 246.94; return 246.94;
} }
if (toTune> 220){ //A3 if (toTune> 220){ //A3
return 220; return 220;
} }
if (toTune> 196){ //G3 if (toTune> 196){ //G3
return 196; return 196;
} }
if (toTune> 174.61){ //F3 if (toTune> 174.61){ //F3
return 174.61; return 174.61;
} }
} }