A program to automatically tune a guitar. Written by Justin Reidhead and Steven Swenson

Dependencies:   FFT FrequencyFinder Motor NewTextLCD PinDetect mbed strings

Files at this revision

API Documentation at this revision

Comitter:
melangeaddict
Date:
Mon Apr 16 22:29:41 2012 +0000
Parent:
2:9c0a83c5ded5
Child:
4:e370f322a697
Commit message:
changed from global vector to global pointer array

Changed in this revision

main.cpp Show annotated file Show diff for this revision Revisions of this file
strings/strings.cpp Show annotated file Show diff for this revision Revisions of this file
strings/strings.h Show annotated file Show diff for this revision Revisions of this file
--- a/main.cpp	Sun Apr 15 00:37:37 2012 +0000
+++ b/main.cpp	Mon Apr 16 22:29:41 2012 +0000
@@ -4,35 +4,36 @@
 #include "NewTextLCD.h"
 #include "PinDetect.h"
 #include "strings.h"
-#include <iostream>
-#include <stdio.h>
-#include <vector>
+//#include "vector"
 
-using namespace std;
+//using namespace std;
 //***************************************************
 //***************Globals*****************************
 PinDetect string_but(p11);
-PinDetect pitch_but(p12);
+PinDetect pitch_but(p12);//These are the buttons the user will interface with
 PinDetect start_but(p13);
 PinDetect mode_but(p14);
 
-DigitalOut led1(LED1);
+DigitalOut led1(LED1);//For diagnostic purposes
 DigitalOut led2(LED2);
 
-TextLCD lcd(p23,p24,p25,p26,p27,p28);
-Motor motor(p20,p21,p22);//enable, direction, step
-FrequencyFinder guitar(p19);//input
+TextLCD lcd(p23,p24,p25,p26,p27,p28);//Our method of communication with LCD screen (rs e d4 d5 d6 d7)
+Motor motor(p20,p21,p22);//Setup for the motor (enable, direction, step)
+FrequencyFinder guitar(p19);//Interface to get data from guitar (input pin)
 
-int selected_string;
-bool current_mode;
-bool start_tuning;
-bool up, down;
+short selected_string;//Holds on to the selected string
+bool current_mode;//Tuning mode or winding mode
+bool start_tuning;//Bool to tell whether to start the tuning process or not
+bool wind_up;//Bool for winding mode - turn motor up
+bool wind_down;//Bool for winding mode - turn motor down
+bool up, down;//To move the motor up or down
 
-vector<strings> strings_array;
+//vector<strings> strings_array;//Holds strings objects - basically the frequency data for each string
+strings strings_array[6];
 
 //***************************************************
 //*****************constants*************************
-const bool tuning_mode=false;
+const bool tuning_mode=false;//For setting the mode to tuning or winding
 const bool winding_mode=true;
 
 //***************************************************
@@ -45,8 +46,10 @@
 void stop();
 void mode();
 void do_nothing();
-void wind_up();
-void wind_down();
+void wind_up_start();
+void wind_down_start();
+void wind_up_stop();
+void wind_down_stop();
 
 void button_init();
 void setup_buttons();
@@ -56,18 +59,25 @@
 //*************************************************
 //*********************main************************
 int main() {
-    lcd.cls();
-    lcd.printf("Perfect\n      Pitch");
+    lcd.cls();//Clear the LCD
+    lcd.printf("Perfect\n      Pitch");//Modify to whatever we want to name this thing
 
     wait(.5);
 
-    device_init();
+    device_init();//Setup buttons and set global variables
+
+    lcd.cls();
+    lcd.printf("finish device_init");
+    wait(.5);
 
     int state=0,next_state=0;
     //float old_freq=0;
     float new_freq=0;
     float desired_freq=0;
     strings *temp=&strings_array[0];
+
+    output_menu();
+
     while (1) {
 
         state=next_state;
@@ -79,25 +89,25 @@
                     next_state=1;
                 } else {
                     next_state=0;
+                    //output_menu();
                 }
-
                 break;
                 //----------------------------------------
             case 1://motor calibration state
-                motor_calibration();
+                motor_calibration();//determine which direction is up and down for the motor
                 next_state=2;
                 break;
                 //-----------------------------------------
             case 2://begin the actual tuning
                 temp=&strings_array[selected_string];
-                desired_freq=temp->get_freq();
+                desired_freq=temp->get_freq();//Get the desired frequency for the string selected
 
                 next_state=3;
                 break;
                 //-----------------------------------------
-            case 3:
-                new_freq=guitar.find_frequency();
-                if (check_threshold(new_freq)) {
+            case 3://Do the dirty work of tuning
+                new_freq=guitar.find_frequency();//Get the current frequency of the string
+                if (check_threshold(new_freq)) {//The check_threshold function makes sure the frequency is valid (less than 500 Hz)
                     if ((desired_freq-.5)<new_freq && (desired_freq+.5)>new_freq) {//We are within .5Hz of the desired frequency
                         lcd.cls();
                         lcd.printf("String %d\ntuned",selected_string);
@@ -105,10 +115,10 @@
 
                         start_tuning=false;
                         next_state=0;
-                    } else if ((desired_freq-.5)>new_freq) {
+                    } else if ((desired_freq-.5)>new_freq) {//We are too low, and need to turn the string tigher
                         //motor(up,# of steps);
                         next_state=3;
-                    } else {
+                    } else {//We are too high, and need to loosen the string
                         //motor(down,# of steps);
                         next_state=3;
                     }
@@ -120,28 +130,63 @@
                 //old_freq=new_freq;
                 break;
                 //-----------------------------------------
+            case 4://Winding mode
+                if (current_mode==winding_mode) {
+                    lcd.cls();
+                    lcd.printf("Winding Mode");
+
+                    if (wind_up) {
+                        motor.motor_turn(up,5);//TODO:Adjust number of turns
+                    }
+
+                    if (wind_down) {
+                        motor.motor_turn(down,5);//TODO:Adjust number of turns
+                    }
+                    next_state=4;
+                } else {
+                    next_state=0;
+                }
+                break;
             default:
                 break;
-        }
+        }//end switch
         wait_ms(5);
 
         if (start_tuning==false) {//If the stop button is pressed, the state machine returns to user input
             next_state=0;
+
         }
-    }
+
+        if (current_mode==winding_mode) {//TODO:Debug this
+            next_state=4;
+        }
+
+        if (start_tuning) {
+            lcd.cls();
+            lcd.printf("Detected %f\nDesired %f",new_freq,desired_freq);
+        }
+
+    }//end while
 
     //   return 0;
 }
 
 //***************************************************
 //******************functions************************
+//Display the string and pitch selection menu
 void output_menu() {
     lcd.cls();
-    strings temp=strings_array[selected_string];
-    lcd.printf("Select String: %d\nSelect Pitch: %s",selected_string,temp.get_note().c_str());
+    lcd.printf("Select String: %d",selected_string);
+    
+    lcd.locate(0,1);
+    strings *temp=&strings_array[selected_string];
+    lcd.printf("Select Pitch: ");
+    lcd.printf(temp->get_note().c_str());
+    wait(.1);
 }
 
 //***************************************************
+//Initialize the buttons
 void button_init() {
     string_but.mode( PullDown );
     string_but.setSampleFrequency();
@@ -157,20 +202,26 @@
 }
 
 //***************************************************
+//Depending on the current mode, the buttons do different things
 void setup_buttons() {
-    if (current_mode==tuning_mode) {
+    if (current_mode==tuning_mode) {//Tuning mode
         string_but.attach_asserted(&string_sel);
+        string_but.attach_deasserted(&do_nothing);
         pitch_but.attach_asserted(&pitch_sel);
+        pitch_but.attach_deasserted(&do_nothing);
         start_but.attach_asserted(&start);
         mode_but.attach_asserted(&mode);
-    } else {
-        string_but.attach_asserted(&wind_up);
-        pitch_but.attach_asserted(&wind_down);
+    } else {//Winding mode
+        string_but.attach_asserted(&wind_up_start);
+        string_but.attach_deasserted(&wind_up_stop);
+        pitch_but.attach_asserted(&wind_down_start);
+        pitch_but.attach_deasserted(&wind_down_stop);
         start_but.attach_asserted(&do_nothing);
         mode_but.attach_asserted(&mode);
     }
 }
 //***************************************************
+//Change the selected string - there are only six strings
 void string_sel() {
     selected_string++;
     if (selected_string>6)
@@ -179,6 +230,7 @@
     output_menu();
 }
 //***************************************************
+//Change the pitch of the selected string
 void pitch_sel() {
     strings *temp=&strings_array[selected_string];
     temp->inc_index();
@@ -186,8 +238,14 @@
     output_menu();
 }
 //***************************************************
+//Start the tuning process
 void start() {
     start_tuning=true;
+
+    string_but.attach_asserted(&do_nothing);//Disable the other buttons
+    pitch_but.attach_asserted(&do_nothing);
+    start_but.attach_asserted(&stop);       //except for the start/stop button
+    mode_but.attach_asserted(&do_nothing);
 }
 //***************************************************
 void mode() {
@@ -195,14 +253,14 @@
         current_mode=winding_mode;
         lcd.cls();
         lcd.printf("Winding Mode");
-        wait(1);
+        wait(.5);
     } else {
         current_mode=tuning_mode;
         lcd.cls();
         lcd.printf("Tuning Mode");
-        wait(1);
+        wait(.5);
     }
-    setup_buttons();
+    setup_buttons();//Change the functions the buttons connect to
 }
 //***************************************************
 void stop() {
@@ -213,42 +271,66 @@
     return;
 }
 //***************************************************
-void wind_up() {
-    motor.motor_turn(1,10);
+void wind_up_start() {
+    wind_up=true;
 }
 //***************************************************
-void wind_down() {
-    motor.motor_turn(0,10);
+void wind_up_stop() {
+    wind_up=false;
+}
+//***************************************************
+void wind_down_start() {
+    wind_down=true;
+}
+//***************************************************
+void wind_down_stop() {
+    wind_down=false;
 }
 //***************************************************
 void device_init() {
+
     strings string1(1);
     strings string2(2);
     strings string3(3);
     strings string4(4);
     strings string5(5);
     strings string6(6);
-    strings_array.push_back(string1);
-    strings_array.push_back(string2);
-    strings_array.push_back(string3);
-    strings_array.push_back(string4);
-    strings_array.push_back(string5);
-    strings_array.push_back(string6);
+    /*    strings_array.push_back(string1);
+        strings_array.push_back(string2);
+        strings_array.push_back(string3);
+        strings_array.push_back(string4);
+        strings_array.push_back(string5);
+        strings_array.push_back(string6);
+        */
 
-    selected_string=1;
+    strings_array[0]=string1;
+    strings_array[1]=string2;
+    strings_array[2]=string3;
+    strings_array[3]=string4;
+    strings_array[4]=string5;
+    strings_array[5]=string6;
+
+    lcd.cls();
+    lcd.printf("device init");
+    wait(.5);
+
+    selected_string=6;
     current_mode=tuning_mode;
     start_tuning=false;
     up=true;
     down=false;
+    wind_up=true;
 
     button_init();
 
     output_menu();
+
 }
 //***************************************************
 void motor_calibration() {
     lcd.cls();
     lcd.printf("Calibrate Motor");
+    wait(.5);
 
     float freq=0, freq_up=0, freq_down=0;
     bool done=false;
@@ -262,6 +344,7 @@
         freq=guitar.find_frequency();
 
         if (check_threshold(freq)) {
+            lcd.cls();
             freq_up=freq;
             done=true;
         }
@@ -272,6 +355,7 @@
         freq=guitar.find_frequency();
 
         if (check_threshold(freq)) {
+            lcd.cls();
             freq_down=freq;
             done=true;
         }
--- a/strings/strings.cpp	Sun Apr 15 00:37:37 2012 +0000
+++ b/strings/strings.cpp	Mon Apr 16 22:29:41 2012 +0000
@@ -1,156 +1,161 @@
 #include "strings.h"
 
+strings::strings() {
 
-strings::strings(int string_num)
-{
+}
+
+strings::strings(int string_num) {
+    set_values(string_num);
+}
+
+strings::~strings(void) {
+
+}
+
+void strings::set_values(int string_num) {
     index=4;
-    switch(string_num){
-    case 6:
-        frequencies[0]=65.4;
-        frequencies[1]=69.3;
-        frequencies[2]=73.4;
-        frequencies[3]=77.7;
-        frequencies[4]=82.4;
-        frequencies[5]=87.3;
-        frequencies[6]=92.5;
-        frequencies[7]=98;
-        frequencies[8]=103.8;
-        notes[0]="C";
-        notes[1]="C#";
-        notes[2]="D";
-        notes[3]="D#";
-        notes[4]="E";
-        notes[5]="F";
-        notes[6]="F#";
-        notes[7]="G";
-        notes[8]="G#";
-        break;
+    switch (string_num) {
+        case 6:
+            frequencies[0]=65.4;
+            frequencies[1]=69.3;
+            frequencies[2]=73.4;
+            frequencies[3]=77.7;
+            frequencies[4]=82.4;
+            frequencies[5]=87.3;
+            frequencies[6]=92.5;
+            frequencies[7]=98;
+            frequencies[8]=103.8;
+            notes[0]="C";
+            notes[1]="C#";
+            notes[2]="D";
+            notes[3]="D#";
+            notes[4]="E";
+            notes[5]="F";
+            notes[6]="F#";
+            notes[7]="G";
+            notes[8]="G#";
+            break;
 
-    case 5:
-        frequencies[0]=87.3;
-        frequencies[1]=92.5;
-        frequencies[2]=98;
-        frequencies[3]=103.8;
-        frequencies[4]=110;
-        frequencies[5]=116.5;
-        frequencies[6]=123.4;
-        frequencies[7]=130.8;
-        frequencies[8]=138.6;
-        notes[0]="F";
-        notes[1]="F#";
-        notes[2]="G";
-        notes[3]="G#";
-        notes[4]="A";
-        notes[5]="A#";
-        notes[6]="B";
-        notes[7]="C";
-        notes[8]="C#";
-        break;
+        case 5:
+            frequencies[0]=87.3;
+            frequencies[1]=92.5;
+            frequencies[2]=98;
+            frequencies[3]=103.8;
+            frequencies[4]=110;
+            frequencies[5]=116.5;
+            frequencies[6]=123.4;
+            frequencies[7]=130.8;
+            frequencies[8]=138.6;
+            notes[0]="F";
+            notes[1]="F#";
+            notes[2]="G";
+            notes[3]="G#";
+            notes[4]="A";
+            notes[5]="A#";
+            notes[6]="B";
+            notes[7]="C";
+            notes[8]="C#";
+            break;
 
-    case 4:
-        frequencies[0]=116.5;
-        frequencies[1]=123.4;
-        frequencies[2]=130.8;
-        frequencies[3]=138.6;
-        frequencies[4]=146.8;
-        frequencies[5]=155.56;
-        frequencies[6]=164.8;
-        frequencies[7]=174.6;
-        frequencies[8]=184.9;
-        notes[0]="A#";
-        notes[1]="B";
-        notes[2]="C";
-        notes[3]="C#";
-        notes[4]="D";
-        notes[5]="D#";
-        notes[6]="E";
-        notes[7]="F";
-        notes[8]="F#";
-        break;
+        case 4:
+            frequencies[0]=116.5;
+            frequencies[1]=123.4;
+            frequencies[2]=130.8;
+            frequencies[3]=138.6;
+            frequencies[4]=146.8;
+            frequencies[5]=155.56;
+            frequencies[6]=164.8;
+            frequencies[7]=174.6;
+            frequencies[8]=184.9;
+            notes[0]="A#";
+            notes[1]="B";
+            notes[2]="C";
+            notes[3]="C#";
+            notes[4]="D";
+            notes[5]="D#";
+            notes[6]="E";
+            notes[7]="F";
+            notes[8]="F#";
+            break;
 
-    case 3:
-        frequencies[0]=155.6;
-        frequencies[1]=164.8;
-        frequencies[2]=174.6;
-        frequencies[3]=184.9;
-        frequencies[4]=195.9;
-        frequencies[5]=207.6;
-        frequencies[6]=220;
-        frequencies[7]=233.1;
-        frequencies[8]=246.9;
-        notes[0]="D#";
-        notes[1]="E";
-        notes[2]="F";
-        notes[3]="F#";
-        notes[4]="G";
-        notes[5]="G#";
-        notes[6]="A";
-        notes[7]="A#";
-        notes[8]="B";
-        break;
+        case 3:
+            frequencies[0]=155.6;
+            frequencies[1]=164.8;
+            frequencies[2]=174.6;
+            frequencies[3]=184.9;
+            frequencies[4]=195.9;
+            frequencies[5]=207.6;
+            frequencies[6]=220;
+            frequencies[7]=233.1;
+            frequencies[8]=246.9;
+            notes[0]="D#";
+            notes[1]="E";
+            notes[2]="F";
+            notes[3]="F#";
+            notes[4]="G";
+            notes[5]="G#";
+            notes[6]="A";
+            notes[7]="A#";
+            notes[8]="B";
+            break;
 
-    case 2:
-        frequencies[0]=195.9;
-        frequencies[1]=207.6;
-        frequencies[2]=220;
-        frequencies[3]=233.1;
-        frequencies[4]=246.9;
-        frequencies[5]=261.6;
-        frequencies[6]=277.1;
-        frequencies[7]=293.6;
-        frequencies[8]=311.1;
-        notes[0]="G";
-        notes[1]="G#";
-        notes[2]="A";
-        notes[3]="A#";
-        notes[4]="B";
-        notes[5]="C";
-        notes[6]="C#";
-        notes[7]="D";
-        notes[8]="D#";
-        break;
+        case 2:
+            frequencies[0]=195.9;
+            frequencies[1]=207.6;
+            frequencies[2]=220;
+            frequencies[3]=233.1;
+            frequencies[4]=246.9;
+            frequencies[5]=261.6;
+            frequencies[6]=277.1;
+            frequencies[7]=293.6;
+            frequencies[8]=311.1;
+            notes[0]="G";
+            notes[1]="G#";
+            notes[2]="A";
+            notes[3]="A#";
+            notes[4]="B";
+            notes[5]="C";
+            notes[6]="C#";
+            notes[7]="D";
+            notes[8]="D#";
+            break;
 
-    case 1:
-        frequencies[0]=261.6;
-        frequencies[1]=277.1;
-        frequencies[2]=293.6;
-        frequencies[3]=311.1;
-        frequencies[4]=329.6;
-        frequencies[5]=349.2;
-        frequencies[6]=369.9;
-        frequencies[7]=391.9;
-        frequencies[8]=415.3;
-        notes[0]="C";
-        notes[1]="C#";
-        notes[2]="D";
-        notes[3]="D#";
-        notes[4]="E";
-        notes[5]="F";
-        notes[6]="F#";
-        notes[7]="G";
-        notes[8]="G#";
-        break;
+        case 1:
+            frequencies[0]=261.6;
+            frequencies[1]=277.1;
+            frequencies[2]=293.6;
+            frequencies[3]=311.1;
+            frequencies[4]=329.6;
+            frequencies[5]=349.2;
+            frequencies[6]=369.9;
+            frequencies[7]=391.9;
+            frequencies[8]=415.3;
+            notes[0]="C";
+            notes[1]="C#";
+            notes[2]="D";
+            notes[3]="D#";
+            notes[4]="E";
+            notes[5]="F";
+            notes[6]="F#";
+            notes[7]="G";
+            notes[8]="G#";
+            break;
 
-    default:
+        default:
             break;
     }
 }
 
-
-strings::~strings(void)
-{
-}
-
-void strings::inc_index(){
+void strings::inc_index() {
     index++;
-    if(index>9)
+    if (index>9)
         index=0;
 }
 
-std::string strings::get_note(){
+std::string strings::get_note() {
     return notes[index];
 }
 
-float strings::get_freq(){
+float strings::get_freq() {
     return frequencies[index];
 }
--- a/strings/strings.h	Sun Apr 15 00:37:37 2012 +0000
+++ b/strings/strings.h	Mon Apr 16 22:29:41 2012 +0000
@@ -1,15 +1,19 @@
 #pragma once
-#include <string>
+#include "string"
 
 class strings
 {
 public:
     strings(int string_num);
+    strings();
     ~strings(void);
 
     void inc_index();
     std::string get_note();
     float get_freq();
+    
+    protected:
+    void set_values(int string_num);
 
 private:
     float frequencies[9];