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:
- 13:948c7d19acb9
- Parent:
- 12:03c189de6e2e
- Child:
- 14:fd59e7acf2e5
--- a/main.cpp Wed Apr 25 04:09:25 2012 +0000 +++ b/main.cpp Thu Apr 26 21:22:56 2012 +0000 @@ -78,7 +78,7 @@ device_init();//Setup buttons and set global variables - + float freq_acc[6] = {.7,.7,.6,.6,.5,.5};//Values to be between when tuning i.e., the first string is +-.7 to desired int state=0,next_state=0; int num_steps=0; //float old_freq=0; @@ -129,15 +129,14 @@ num_steps=get_steps(desired_freq, new_freq); - if ((desired_freq-.5)<new_freq && (desired_freq+.5)>new_freq) {//We are within .5Hz of the desired frequency + if ((desired_freq-freq_acc[selected_string])<new_freq && (desired_freq+freq_acc[selected_string])>new_freq) {//We are within .5Hz of the desired frequency new_freq=guitar.find_frequency(); - if ((desired_freq-.5)>new_freq || (desired_freq+.5)<new_freq) {//This checks the frequency again to make sure we are close + if ((desired_freq-freq_acc[selected_string])>new_freq || (desired_freq+freq_acc[selected_string])<new_freq) {//This checks the frequency again to make sure we are close next_state=3; break; } - lcd.cls(); lcd.printf("String %d\ntuned",selected_string+1); set_LED(1,0);//blue on @@ -148,15 +147,16 @@ start_tuning=false; output_menu(); setup_buttons(); + new_freq=0;//Just to reset next_state=0; } else if ((desired_freq-.5)>new_freq) {//We are too low, and need to turn the string tigher if (current_direction==down) { - num_steps+=18;//For deadband + num_steps+=10;//For deadband current_direction=up; } - lcd.cls(); - lcd.printf("Tuning up"); + //lcd.cls(); + //lcd.printf("Tuning up"); set_LED(1,1);//blue off set_LED(2,0);//red on; @@ -171,12 +171,12 @@ } else {//We are too high, and need to loosen the string if (current_direction==up) { - num_steps+=18;//For deadband + num_steps+=10;//For deadband current_direction=down; } - lcd.cls(); - lcd.printf("Tuning down"); + //lcd.cls(); + //lcd.printf("Tuning down"); set_LED(1,1);//blue off set_LED(2,1);//red off; set_LED(3,0);//green on @@ -203,6 +203,7 @@ break; //----------------------------------------- case 4://Winding mode + if (current_mode==winding_mode) { if (wind_up) { motor.motor_turn(up,5);//TODO:Adjust number of turns @@ -345,6 +346,10 @@ //*************************************************** void mode() { if (current_mode==tuning_mode) { + set_LED(2,0);//red off + set_LED(1,0);//blue on + set_LED(3,1);//green on + current_mode=winding_mode; lcd.cls(); lcd.printf("Winding Mode"); @@ -352,6 +357,10 @@ lcd.cls(); lcd.printf("String for up\nPitch for down"); } else { + set_LED(2,1);//red off + set_LED(1,0);//blue on + set_LED(3,1);//green off + current_mode=tuning_mode; lcd.cls(); lcd.printf("Tuning Mode"); @@ -374,18 +383,30 @@ //*************************************************** void wind_up_start() { wind_up=true; + set_LED(2,0);//red on + set_LED(1,1);//blue off + set_LED(3,1);//green off } //*************************************************** void wind_up_stop() { wind_up=false; + set_LED(2,0);//red off + set_LED(1,0);//blue on + set_LED(3,1);//green on } //*************************************************** void wind_down_start() { wind_down=true; + set_LED(3,0);//green on + set_LED(1,1);//blue off + set_LED(2,1);// off } //*************************************************** void wind_down_stop() { wind_down=false; + set_LED(2,0);//red off + set_LED(1,0);//blue on + set_LED(3,1);//green on } //*************************************************** void device_init() { @@ -530,7 +551,7 @@ void LED_initialize() { - float wait_time=0.3; + float wait_time=0.1; /* ledBlue=1;//red ledGreen=1; ledRed=0; @@ -622,65 +643,76 @@ return 60; } else if (difference>3) { return 35; + } else if (difference>2) { + return 20; } else if (difference>1) { return 15; } else { return 10; } case 4: - if (difference>10) { + if (difference>5) { return 60; } else if (difference>3) { return 40; + } else if (difference>2) { + return 30; } else if (difference>1) { return 23; } else { return 10; } case 3: - if (difference>10) { + if (difference>5) { return 40; } else if (difference>3) { return 25; + } else if (difference>2) { + return 20; } else if (difference>1) { return 15; } else { return 10; } case 2: - if (difference>10) { + if (difference>5) { return 33; } else if (difference>3) { - return 20; + return 23; + } else if (difference>2) { + return 17; } else if (difference>1) { - return 15; + return 12; } else { - return 10; + return 8; } case 1: - if (difference>10) { - return 30; + if (difference>5) { + return 35; } else if (difference>3) { + return 25; + } else if (difference>2) { return 20; } else if (difference>1) { return 10; } else { - return 7; + return 5; } case 0: - if (difference>10) { + if (difference>8) { + return 30; + } else if (difference>6) { return 20; - } else if (difference>3) { + } else if (difference>4) { return 15; } else if (difference>1) { - return 8; + return 10; } else { - return 4; + return 5; } default: return 0; - break; }//end switch } \ No newline at end of file