A program to automatically tune a guitar. Written by Justin Reidhead and Steven Swenson
Dependencies: FFT FrequencyFinder Motor NewTextLCD PinDetect mbed strings
Diff: main.cpp
- Revision:
- 3:c672e782f19b
- Parent:
- 2:9c0a83c5ded5
- Child:
- 4:e370f322a697
--- 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; }