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

Dependencies:   FFT FrequencyFinder Motor NewTextLCD PinDetect mbed strings

Revision:
8:651fbf5ae98a
Parent:
7:057bcd99a3f8
Child:
9:2a211133e79a
--- a/main.cpp	Fri Apr 20 17:28:27 2012 +0000
+++ b/main.cpp	Fri Apr 20 21:27:55 2012 +0000
@@ -22,9 +22,9 @@
 FrequencyFinder guitar(p20);//Interface to get data from guitar (input pin)
 AnalogOut bias(p18);//1.6v DC offset
 
-DigitalOut ledBlue(p25);
-DigitalOut ledGreen(p23);
-DigitalOut ledRed(p24);
+DigitalOut ledBlue(p27);
+DigitalOut ledGreen(p28);
+DigitalOut ledRed(p29);
 
 
 short selected_string;//Holds on to the selected string
@@ -62,19 +62,19 @@
 void output_menu();
 void motor_calibration();
 bool check_threshold(float);
-void LED_initialize();
-void set_LED(int led, int value);
+void LED_initialize();//Initialization sequence for LEDs, called at start-up
+void set_LED(int led, int value);//sets or clears an LED: Blue=1(in tune), Red=2(flat), Green=3(sharp). Value=1(off), 0(on);
 //*************************************************
 //*********************main************************
 int main() {
 
     lcd.cls();//Clear the LCD
     lcd.printf("Perfect\n      Pitch");//Modify to whatever we want to name this thing
-    LED_initialize();
-
 
     device_init();//Setup buttons and set global variables
 
+    LED_initialize();
+
     int state=0,next_state=0;
     //float old_freq=0;
     float new_freq=0;
@@ -116,6 +116,9 @@
                     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+1);
+                        set_LED(1,0);//blue on
+                        set_LED(2,1);//red off;
+                        set_LED(3,1);//green off
                         wait(.5);
 
                         start_tuning=false;
@@ -123,9 +126,27 @@
                         setup_buttons();
                         next_state=0;
                     } else if ((desired_freq-.5)>new_freq) {//We are too low, and need to turn the string tigher
+                        lcd.cls();
+                        lcd.printf("Tuning up");
+                        
+                        set_LED(1,1);//blue off
+                        set_LED(2,0);//red on;
+                        set_LED(3,1);//green off
+                        
+                        
+                        wait(.5);
+
                         motor.motor_turn(up,10);//TODO:Adjust # of steps
                         next_state=3;
+
                     } else {//We are too high, and need to loosen the string
+                        lcd.cls();
+                        lcd.printf("Tuning down");
+                        set_LED(1,1);//blue off
+                        set_LED(2,1);//red off;
+                        set_LED(3,0);//green on
+                        wait(.5);
+
                         motor.motor_turn(down,10);
                         next_state=3;
                     }
@@ -133,6 +154,13 @@
                     next_state=3;
                 }
 
+                if (start_tuning==false) {//If the stop button is pressed, the state machine returns to user input
+                    next_state=0;
+                    setup_buttons();
+                    output_menu();
+                    break;
+                }
+
                 //TODO:Determine number of steps per frequency change
                 //old_freq=new_freq;
                 break;
@@ -243,9 +271,9 @@
 //***************************************************
 //Change the selected string - there are only six strings
 void string_sel() {
-    selected_string++;
-    if (selected_string>5)
-        selected_string=0;
+    selected_string--;
+    if (selected_string<0)
+        selected_string=5;
 
     strings *temp=&strings_array[selected_string];
     temp->reset_index();
@@ -390,8 +418,11 @@
     }
 
     if (freq_up<freq_down) {
-        down=1;
-        up=0;
+        //down=0;
+        //up=1;
+    } else {
+        //down=0;
+        //up=1;
     }
 
     lcd.cls();
@@ -453,8 +484,9 @@
     //ledBlue=1;
     //ledRed=2;
     //ledGreen=3;
-    //value must be an int: 0 or 1
-    if (value!=0)
+    //value must be an int: 0(on) or 1(off)
+   
+    if (value==0 || value==1)
 
         switch (led) {
             case(1):