![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
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:
- 9:2a211133e79a
- Parent:
- 8:651fbf5ae98a
- Child:
- 11:bdad1acccdad
--- a/main.cpp Fri Apr 20 21:27:55 2012 +0000 +++ b/main.cpp Tue Apr 24 20:43:25 2012 +0000 @@ -37,6 +37,8 @@ //vector<strings> strings_array;//Holds strings objects - basically the frequency data for each string strings strings_array[6]; + +LocalFileSystem local("local"); //*************************************************** //*****************constants************************* const bool tuning_mode=false;//For setting the mode to tuning or winding @@ -46,37 +48,39 @@ //******************prototypes*********************** void device_init(); -void string_sel(); -void pitch_sel(); -void start(); -void stop(); -void mode(); -void do_nothing(); -void wind_up_start(); -void wind_down_start(); -void wind_up_stop(); -void wind_down_stop(); +void string_sel();//string select +void pitch_sel();//pitch select +void start();//start tuning +void stop();//stop tuning +void mode();//change mode +void do_nothing();//does nothing +void wind_up_start();//start motor winding up +void wind_down_start();//start motor winding down +void wind_up_stop();//stop motor winding up +void wind_down_stop();//stop motor winding down -void button_init(); -void setup_buttons(); -void output_menu(); -void motor_calibration(); -bool check_threshold(float); +void button_init();//Set sampling period etc... for buttons +void setup_buttons();//set function calls for buttons +void output_menu();//Output the main menu +void motor_calibration();//Calibrate the motor's direction +bool check_threshold(float);//check to make sure the frequency is valid (frequency to check) 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(); + // wait(.5); device_init();//Setup buttons and set global variables - LED_initialize(); + int state=0,next_state=0; - //float old_freq=0; + int num_steps=0; + float old_freq=0; float new_freq=0; float desired_freq=0; strings *temp=&strings_array[0]; @@ -99,19 +103,28 @@ break; //---------------------------------------- case 1://motor calibration state - motor_calibration();//determine which direction is up and down for the motor + //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();//Get the desired frequency for the string selected + old_freq=desired_freq;//We have to initalize it to something... next_state=3; break; //----------------------------------------- case 3://Do the dirty work of tuning new_freq=guitar.find_frequency();//Get the current frequency of the string + + //if (new_freq>(desired_freq+50) || new_freq<(desired_freq-50)) {//If the new frequency is wildly out of whack + //new_freq=guitar.find_frequency();//Get the current frequency of the string, again + // break; + //} + //num_steps=-5*abs(new_freq-old_freq); + // old_freq=new_freq; + 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(); @@ -128,12 +141,12 @@ } 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 @@ -158,11 +171,10 @@ next_state=0; setup_buttons(); output_menu(); - break; } //TODO:Determine number of steps per frequency change - //old_freq=new_freq; + break; //----------------------------------------- case 4://Winding mode @@ -435,9 +447,12 @@ } //********************************************** bool check_threshold(float freq) { - if (freq>500) { + strings *temp=&strings_array[selected_string]; + float desired_freq=temp->get_freq();//Get the desired frequency for the string selected + if (freq>(desired_freq+75) || freq<(desired_freq-75) || freq>500) {//new_freq>(desired_freq+50) || new_freq<(desired_freq-50) lcd.cls(); lcd.printf("Pluck string \nagain"); + wait(.5); return false; } else return true; @@ -447,33 +462,50 @@ void LED_initialize() { float wait_time=0.3; - ledBlue=1;//red - ledGreen=1; - ledRed=0; - wait(wait_time); - ledRed=1;//green - ledGreen=0; - wait(wait_time); - ledGreen=1; - ledBlue=0;//blue - wait(wait_time); - - for (int a=0; a<5; a++) { - ledRed=0;//all + /* ledBlue=1;//red + ledGreen=1; + ledRed=0; + wait(wait_time); + ledRed=1;//green ledGreen=0; - ledBlue=0; wait(wait_time); - ledRed=1; ledGreen=1; - ledBlue=1; + ledBlue=0;//blue wait(wait_time); - } - ledBlue=0; + for (int a=0; a<3; a++) { + ledRed=0;//all + ledGreen=0; + ledBlue=0; + wait(wait_time); + ledRed=1; + ledGreen=1; + ledBlue=1; + wait(wait_time); + + } + ledBlue=0; - ledRed=1;//blue - ledGreen=1; + ledRed=1;//blue + ledGreen=1; + */ + set_LED(1,1); + set_LED(2,1); + set_LED(3,1); + + for (int b=1; b<4; b++) { + for (int a=1; a<4; a++) { + set_LED(a, 0); + wait(wait_time); + set_LED(a,1); + wait(0.001); + + } + } + set_LED(1,0); + set_LED(2,1); + set_LED(3,1); return; @@ -485,7 +517,7 @@ //ledRed=2; //ledGreen=3; //value must be an int: 0(on) or 1(off) - + if (value==0 || value==1) switch (led) {