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,18 +3,16 @@
*@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
} }
@ -39,32 +37,37 @@ void loop() {
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;