// AutoTona // Jesse Leal (jl2722) // MAE 3780 // 10/28/2021 /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// // Frequency AUDIO IMPORT // Reference: Clyde Lettsome #include "arduinoFFT.h" #define SAMPLES 128 //SAMPLES-pt FFT. Must be a base 2 number. Max 128 for Arduino Uno. #define SAMPLING_FREQUENCY 700 //Ts = Based on Nyquist, must be 2 times the highest expected frequency. arduinoFFT FFT = arduinoFFT(); unsigned int samplingPeriod; unsigned long microSeconds; double vReal[SAMPLES]; //create vector of size SAMPLES to hold real values double vImag[SAMPLES]; //create vector of size SAMPLES to hold imaginary values // AUDIO END // SERVO IMPORT #include Servo myservo; // SERVO END // 7SEG Display Defining #define segA 10// connecting segment A to PIN10 #define segB 9// connecting segment B to PIN9 #define segC 8// connecting segment C to PIN8 #define segD 7// connecting segment D to PIN7 #define segE 6// connecting segment E to PIN6 #define segG 5// connecting segment G to PIN5 #define segF 4// connecting segment F to PIN4 // 7SEG END // BUTTON SETUP int button = 11;// button set to pin 11 double COUNT = 0; //button count integer for 0-5 increment //BUTTON END // GUITAR STRING STRING FREQUENCY SETUP double STol = 40; // String Tolerance Frequency to prevent tuning wrong string double E_string = 82; // HZ double A_string = 110; // HZ double D_string = 149; // HZ double G_string = 198; // HZ double B_string = 249; // HZ double e_string = 335; // HZ double Tolerance = 4; // Nominal Frequency Tolerance bool stop = false; // GUITAR STRING END /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// void setup() { // AUDIO SETUP // Reference: Clyde Lettsome Serial.begin(9600); samplingPeriod = round(1000000 * (1.0 / SAMPLING_FREQUENCY)); //Period in microseconds // AUDIO END // 7 SEGMENT DISPLAY SETUP // Setting Ardiuno pins as outputs pinMode(4, OUTPUT); pinMode(5, OUTPUT); pinMode(6, OUTPUT); pinMode(7, OUTPUT); pinMode(8, OUTPUT); pinMode(9, OUTPUT); pinMode(10, OUTPUT); // 7 SEG END // LED SETUP pinMode(A3, OUTPUT); //RED-SHARP pinMode(A4, OUTPUT); //GREEN-NOMINAL pinMode(A5, OUTPUT); //RED-FLAT //LED END // BUTTON SETUP pinMode(button, INPUT); } /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// void loop() { // SERVO myservo.attach(A1); //myservo.write(120); // SERVO TEST // SERVO END // BUTTON CLICK if (digitalRead(button) == HIGH) { COUNT++; Serial.println("count:"); Serial.println(COUNT); Serial.println("Sampling Frequency:"); Serial.println(SAMPLING_FREQUENCY); delay(300); // BUTTON END } // AUDIO CHECK // Reference: Clyde Lettsome while (digitalRead(button) == LOW && stop == 0) { // AUDIO CHECK BEGIN /*Sample SAMPLES times*/ for (int i = 0; i < SAMPLES; i++) { microSeconds = micros(); //Returns the number of microseconds since the Arduino board began running the current script. vReal[i] = analogRead(0); //Reads the value from analog pin 0 (A0), quantize it and save it as a real term. vImag[i] = 0; //Makes imaginary term 0 always /*remaining wait time between samples if necessary*/ while (micros() < (microSeconds + samplingPeriod)) { //do nothing } } /*Perform FFT on samples*/ FFT.Windowing(vReal, SAMPLES, FFT_WIN_TYP_HAMMING, FFT_FORWARD); FFT.Compute(vReal, vImag, SAMPLES, FFT_FORWARD); FFT.ComplexToMagnitude(vReal, vImag, SAMPLES); /*Find peak frequency and print peak*/ double peak = FFT.MajorPeak(vReal, SAMPLES, SAMPLING_FREQUENCY); Serial.println(peak); //Print out the most dominant frequency. // AUDIO CHECK END // 7 SEGMENT DISPLAY CHOICE if (COUNT == 5) { // GUITAR STRING: E //7 SEG: E digitalWrite(segA, LOW); digitalWrite(segB, HIGH); digitalWrite(segC, HIGH); digitalWrite(segD, LOW); digitalWrite(segE, LOW); digitalWrite(segF, LOW); digitalWrite(segG, LOW); // E_String AUTO TUNING if (peak > E_string - STol && peak <= E_string - Tolerance && stop == 0) {// Flat Note myservo.write(0); // Servo CW to tune higher digitalWrite(A3, LOW); // turn OFF FLAT LED digitalWrite(A4, LOW); // turn OFF NOMINAL LED digitalWrite(A5, HIGH); // turn OFF SHARP LED } else if (peak < E_string + STol && peak >= E_string + Tolerance && stop == 0) { // Sharp Note myservo.write(180); // Servo CCW to tune lower digitalWrite(A3, HIGH); // turn ON FLAT LED digitalWrite(A4, LOW); // turn OFF NOMINAL LED digitalWrite(A5, LOW); // turn OFF SHARP LED } else if (peak < E_string + Tolerance || peak > E_string - Tolerance) {// Tuned Note myservo.write(90); // STOP servo digitalWrite(A3, LOW); // turn OFF FLAT LED digitalWrite(A4, HIGH); // turn ON NOMINAL LED digitalWrite(A5, LOW); // turn OFF SHARP LED stop == 1; } } else if (COUNT == 4) { // GUITAR STRING: A //7 SEG: A digitalWrite(segA, LOW); digitalWrite(segB, LOW); digitalWrite(segC, LOW); digitalWrite(segD, HIGH); digitalWrite(segE, LOW); digitalWrite(segF, LOW); digitalWrite(segG, LOW); // A_String AUTO TUNING if (peak > A_string - STol && peak <= A_string - Tolerance && stop == 0) {// Flat Note myservo.write(0); // Servo CW to tune higher digitalWrite(A3, LOW); // turn OFF FLAT LED digitalWrite(A4, LOW); // turn OFF NOMINAL LED digitalWrite(A5, HIGH); // turn OFF SHARP LED } else if (peak < A_string + STol && peak >= A_string + Tolerance && stop == 0) { // Sharp Note myservo.write(180); // Servo CCW to tune lower digitalWrite(A3, HIGH); // turn ON FLAT LED digitalWrite(A4, LOW); // turn OFF NOMINAL LED digitalWrite(A5, LOW); // turn OFF SHARP LED } else if (peak < A_string + Tolerance || peak > A_string - Tolerance) {// Tuned Note myservo.write(90); // STOP servo digitalWrite(A3, LOW); // turn OFF FLAT LED digitalWrite(A4, HIGH); // turn ON NOMINAL LED digitalWrite(A5, LOW); // turn OFF SHARP LED stop == 1; } } else if (COUNT == 3) {// GUITAR STRING: D //7 SEG: D digitalWrite(segA, LOW); digitalWrite(segB, LOW); digitalWrite(segC, LOW); digitalWrite(segD, LOW); digitalWrite(segE, LOW); digitalWrite(segF, LOW); digitalWrite(segG, HIGH); // D_String AUTO TUNING if (peak > D_string - STol && peak <= D_string - Tolerance && stop == 0) {// Flat Note myservo.write(0); // Servo CW to tune higher digitalWrite(A3, LOW); // turn OFF FLAT LED digitalWrite(A4, LOW); // turn OFF NOMINAL LED digitalWrite(A5, HIGH); // turn OFF SHARP LED } else if (peak < D_string + STol && peak >= D_string + Tolerance && stop == 0) { // Sharp Note myservo.write(180); // Servo CCW to tune lower digitalWrite(A3, HIGH); // turn ON FLAT LED digitalWrite(A4, LOW); // turn OFF NOMINAL LED digitalWrite(A5, LOW); // turn OFF SHARP LED } else if (peak < D_string + Tolerance || peak > D_string - Tolerance) {// Tuned Note myservo.write(90); // STOP servo digitalWrite(A3, LOW); // turn OFF FLAT LED digitalWrite(A4, HIGH); // turn ON NOMINAL LED digitalWrite(A5, LOW); // turn OFF SHARP LED stop == 1; } } else if (COUNT == 2) {// GUITAR STRING: G //7 SEG: G digitalWrite(segA, LOW); digitalWrite(segB, HIGH); digitalWrite(segC, LOW); digitalWrite(segD, LOW); digitalWrite(segE, LOW); digitalWrite(segF, LOW); digitalWrite(segG, LOW); // G_String AUTO TUNING if (peak > G_string - STol && peak <= G_string - Tolerance && stop == 0) {// Flat Note myservo.write(0); // Servo CW to tune higher digitalWrite(A3, LOW); // turn OFF FLAT LED digitalWrite(A4, LOW); // turn OFF NOMINAL LED digitalWrite(A5, HIGH); // turn OFF SHARP LED } else if (peak < G_string + STol && peak >= G_string + Tolerance && stop == 0) { // Sharp Note myservo.write(180); // Servo CCW to tune lower digitalWrite(A3, HIGH); // turn ON FLAT LED digitalWrite(A4, LOW); // turn OFF NOMINAL LED digitalWrite(A5, LOW); // turn OFF SHARP LED } else if (peak < G_string + Tolerance || peak > G_string - Tolerance) { // Tuned Note myservo.write(90); // STOP servo digitalWrite(A3, LOW); // turn OFF FLAT LED digitalWrite(A4, HIGH); // turn ON NOMINAL LED digitalWrite(A5, LOW); // turn OFF SHARP LED stop == 1; } } else if (COUNT == 1) {// GUITAR STRING: B //7 SEG: B digitalWrite(segA, HIGH); digitalWrite(segB, HIGH); digitalWrite(segC, LOW); digitalWrite(segD, LOW); digitalWrite(segE, LOW); digitalWrite(segF, LOW); digitalWrite(segG, LOW); // B_String AUTO TUNING if (peak > B_string - STol && peak <= B_string - Tolerance && stop == 0) {// Flat Note myservo.write(0); // Servo CW to tune higher digitalWrite(A3, LOW); // turn OFF FLAT LED digitalWrite(A4, LOW); // turn OFF NOMINAL LED digitalWrite(A5, HIGH); // turn OFF SHARP LED } else if (peak < B_string + STol && peak >= B_string + Tolerance && stop == 0) {// Sharp Note myservo.write(180); // Servo CCW to tune lower digitalWrite(A3, HIGH); // turn ON FLAT LED digitalWrite(A4, LOW); // turn OFF NOMINAL LED digitalWrite(A5, LOW); // turn OFF SHARP LED } else if (peak < B_string + Tolerance || peak > B_string - Tolerance) {// Tuned Note myservo.write(90); // STOP servo digitalWrite(A3, LOW); // turn OFF FLAT LED digitalWrite(A4, HIGH); // turn ON NOMINAL LED digitalWrite(A5, LOW); // turn OFF SHARP LED stop == 1; } } else if (COUNT == 0) {// GUITAR STRING: e //7 SEG: e digitalWrite(segA, LOW); digitalWrite(segB, LOW); digitalWrite(segC, LOW); digitalWrite(segD, LOW); digitalWrite(segE, HIGH); digitalWrite(segF, HIGH); digitalWrite(segG, LOW); // e_String AUTO TUNING if (peak > e_string - STol && peak <= e_string - Tolerance && stop == 0) {// Flat Note myservo.write(0); // Servo CW to tune higher digitalWrite(A3, LOW); // turn OFF FLAT LED digitalWrite(A4, LOW); // turn OFF NOMINAL LED digitalWrite(A5, HIGH); // turn OFF SHARP LED } else if (peak >= e_string + Tolerance && stop == 0) { // Sharp Note myservo.write(180); // Servo CCW to tune lower digitalWrite(A3, HIGH); // turn ON FLAT LED digitalWrite(A4, LOW); // turn OFF NOMINAL LED digitalWrite(A5, LOW); // turn OFF SHARP LED } else if (peak < e_string + Tolerance || peak > e_string - Tolerance) {// Tuned Note myservo.write(90); // STOP servo digitalWrite(A3, LOW); // turn OFF FLAT LED digitalWrite(A4, HIGH); // turn ON NOMINAL LED digitalWrite(A5, LOW); // turn OFF SHARP LED stop == 1; } } else if (COUNT > 5) { // Restart Button Cycle COUNT = 0; } } }