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

Dependencies:   FFT FrequencyFinder Motor NewTextLCD PinDetect mbed strings

Committer:
melangeaddict
Date:
Mon Apr 16 22:29:41 2012 +0000
Revision:
3:c672e782f19b
Parent:
2:9c0a83c5ded5
Child:
4:e370f322a697
changed from global vector to global pointer array

Who changed what in which revision?

UserRevisionLine numberNew contents of line
melangeaddict 2:9c0a83c5ded5 1 #include "mbed.h"
melangeaddict 2:9c0a83c5ded5 2 #include "Motor.h"
melangeaddict 2:9c0a83c5ded5 3 #include "FrequencyFinder.h"
melangeaddict 2:9c0a83c5ded5 4 #include "NewTextLCD.h"
melangeaddict 2:9c0a83c5ded5 5 #include "PinDetect.h"
melangeaddict 2:9c0a83c5ded5 6 #include "strings.h"
melangeaddict 3:c672e782f19b 7 //#include "vector"
melangeaddict 2:9c0a83c5ded5 8
melangeaddict 3:c672e782f19b 9 //using namespace std;
melangeaddict 2:9c0a83c5ded5 10 //***************************************************
melangeaddict 2:9c0a83c5ded5 11 //***************Globals*****************************
melangeaddict 2:9c0a83c5ded5 12 PinDetect string_but(p11);
melangeaddict 3:c672e782f19b 13 PinDetect pitch_but(p12);//These are the buttons the user will interface with
melangeaddict 2:9c0a83c5ded5 14 PinDetect start_but(p13);
melangeaddict 2:9c0a83c5ded5 15 PinDetect mode_but(p14);
melangeaddict 2:9c0a83c5ded5 16
melangeaddict 3:c672e782f19b 17 DigitalOut led1(LED1);//For diagnostic purposes
melangeaddict 2:9c0a83c5ded5 18 DigitalOut led2(LED2);
melangeaddict 2:9c0a83c5ded5 19
melangeaddict 3:c672e782f19b 20 TextLCD lcd(p23,p24,p25,p26,p27,p28);//Our method of communication with LCD screen (rs e d4 d5 d6 d7)
melangeaddict 3:c672e782f19b 21 Motor motor(p20,p21,p22);//Setup for the motor (enable, direction, step)
melangeaddict 3:c672e782f19b 22 FrequencyFinder guitar(p19);//Interface to get data from guitar (input pin)
melangeaddict 2:9c0a83c5ded5 23
melangeaddict 3:c672e782f19b 24 short selected_string;//Holds on to the selected string
melangeaddict 3:c672e782f19b 25 bool current_mode;//Tuning mode or winding mode
melangeaddict 3:c672e782f19b 26 bool start_tuning;//Bool to tell whether to start the tuning process or not
melangeaddict 3:c672e782f19b 27 bool wind_up;//Bool for winding mode - turn motor up
melangeaddict 3:c672e782f19b 28 bool wind_down;//Bool for winding mode - turn motor down
melangeaddict 3:c672e782f19b 29 bool up, down;//To move the motor up or down
melangeaddict 2:9c0a83c5ded5 30
melangeaddict 3:c672e782f19b 31 //vector<strings> strings_array;//Holds strings objects - basically the frequency data for each string
melangeaddict 3:c672e782f19b 32 strings strings_array[6];
melangeaddict 2:9c0a83c5ded5 33
melangeaddict 2:9c0a83c5ded5 34 //***************************************************
melangeaddict 2:9c0a83c5ded5 35 //*****************constants*************************
melangeaddict 3:c672e782f19b 36 const bool tuning_mode=false;//For setting the mode to tuning or winding
melangeaddict 2:9c0a83c5ded5 37 const bool winding_mode=true;
melangeaddict 2:9c0a83c5ded5 38
melangeaddict 2:9c0a83c5ded5 39 //***************************************************
melangeaddict 2:9c0a83c5ded5 40 //******************prototypes***********************
melangeaddict 2:9c0a83c5ded5 41 void device_init();
melangeaddict 2:9c0a83c5ded5 42
melangeaddict 2:9c0a83c5ded5 43 void string_sel();
melangeaddict 2:9c0a83c5ded5 44 void pitch_sel();
melangeaddict 2:9c0a83c5ded5 45 void start();
melangeaddict 2:9c0a83c5ded5 46 void stop();
melangeaddict 2:9c0a83c5ded5 47 void mode();
melangeaddict 2:9c0a83c5ded5 48 void do_nothing();
melangeaddict 3:c672e782f19b 49 void wind_up_start();
melangeaddict 3:c672e782f19b 50 void wind_down_start();
melangeaddict 3:c672e782f19b 51 void wind_up_stop();
melangeaddict 3:c672e782f19b 52 void wind_down_stop();
melangeaddict 2:9c0a83c5ded5 53
melangeaddict 2:9c0a83c5ded5 54 void button_init();
melangeaddict 2:9c0a83c5ded5 55 void setup_buttons();
melangeaddict 2:9c0a83c5ded5 56 void output_menu();
melangeaddict 2:9c0a83c5ded5 57 void motor_calibration();
melangeaddict 2:9c0a83c5ded5 58 bool check_threshold(float);
melangeaddict 2:9c0a83c5ded5 59 //*************************************************
melangeaddict 2:9c0a83c5ded5 60 //*********************main************************
melangeaddict 2:9c0a83c5ded5 61 int main() {
melangeaddict 3:c672e782f19b 62 lcd.cls();//Clear the LCD
melangeaddict 3:c672e782f19b 63 lcd.printf("Perfect\n Pitch");//Modify to whatever we want to name this thing
melangeaddict 2:9c0a83c5ded5 64
melangeaddict 2:9c0a83c5ded5 65 wait(.5);
melangeaddict 2:9c0a83c5ded5 66
melangeaddict 3:c672e782f19b 67 device_init();//Setup buttons and set global variables
melangeaddict 3:c672e782f19b 68
melangeaddict 3:c672e782f19b 69 lcd.cls();
melangeaddict 3:c672e782f19b 70 lcd.printf("finish device_init");
melangeaddict 3:c672e782f19b 71 wait(.5);
melangeaddict 2:9c0a83c5ded5 72
melangeaddict 2:9c0a83c5ded5 73 int state=0,next_state=0;
melangeaddict 2:9c0a83c5ded5 74 //float old_freq=0;
melangeaddict 2:9c0a83c5ded5 75 float new_freq=0;
melangeaddict 2:9c0a83c5ded5 76 float desired_freq=0;
melangeaddict 2:9c0a83c5ded5 77 strings *temp=&strings_array[0];
melangeaddict 3:c672e782f19b 78
melangeaddict 3:c672e782f19b 79 output_menu();
melangeaddict 3:c672e782f19b 80
melangeaddict 2:9c0a83c5ded5 81 while (1) {
melangeaddict 2:9c0a83c5ded5 82
melangeaddict 2:9c0a83c5ded5 83 state=next_state;
melangeaddict 2:9c0a83c5ded5 84
melangeaddict 2:9c0a83c5ded5 85 switch (state) {
melangeaddict 2:9c0a83c5ded5 86 //------------------------------------
melangeaddict 2:9c0a83c5ded5 87 case 0://Stay here till the user selects the string and pitch
melangeaddict 2:9c0a83c5ded5 88 if (start_tuning==true) {
melangeaddict 2:9c0a83c5ded5 89 next_state=1;
melangeaddict 2:9c0a83c5ded5 90 } else {
melangeaddict 2:9c0a83c5ded5 91 next_state=0;
melangeaddict 3:c672e782f19b 92 //output_menu();
melangeaddict 2:9c0a83c5ded5 93 }
melangeaddict 2:9c0a83c5ded5 94 break;
melangeaddict 2:9c0a83c5ded5 95 //----------------------------------------
melangeaddict 2:9c0a83c5ded5 96 case 1://motor calibration state
melangeaddict 3:c672e782f19b 97 motor_calibration();//determine which direction is up and down for the motor
melangeaddict 2:9c0a83c5ded5 98 next_state=2;
melangeaddict 2:9c0a83c5ded5 99 break;
melangeaddict 2:9c0a83c5ded5 100 //-----------------------------------------
melangeaddict 2:9c0a83c5ded5 101 case 2://begin the actual tuning
melangeaddict 2:9c0a83c5ded5 102 temp=&strings_array[selected_string];
melangeaddict 3:c672e782f19b 103 desired_freq=temp->get_freq();//Get the desired frequency for the string selected
melangeaddict 2:9c0a83c5ded5 104
melangeaddict 2:9c0a83c5ded5 105 next_state=3;
melangeaddict 2:9c0a83c5ded5 106 break;
melangeaddict 2:9c0a83c5ded5 107 //-----------------------------------------
melangeaddict 3:c672e782f19b 108 case 3://Do the dirty work of tuning
melangeaddict 3:c672e782f19b 109 new_freq=guitar.find_frequency();//Get the current frequency of the string
melangeaddict 3:c672e782f19b 110 if (check_threshold(new_freq)) {//The check_threshold function makes sure the frequency is valid (less than 500 Hz)
melangeaddict 2:9c0a83c5ded5 111 if ((desired_freq-.5)<new_freq && (desired_freq+.5)>new_freq) {//We are within .5Hz of the desired frequency
melangeaddict 2:9c0a83c5ded5 112 lcd.cls();
melangeaddict 2:9c0a83c5ded5 113 lcd.printf("String %d\ntuned",selected_string);
melangeaddict 2:9c0a83c5ded5 114 wait(.5);
melangeaddict 2:9c0a83c5ded5 115
melangeaddict 2:9c0a83c5ded5 116 start_tuning=false;
melangeaddict 2:9c0a83c5ded5 117 next_state=0;
melangeaddict 3:c672e782f19b 118 } else if ((desired_freq-.5)>new_freq) {//We are too low, and need to turn the string tigher
melangeaddict 2:9c0a83c5ded5 119 //motor(up,# of steps);
melangeaddict 2:9c0a83c5ded5 120 next_state=3;
melangeaddict 3:c672e782f19b 121 } else {//We are too high, and need to loosen the string
melangeaddict 2:9c0a83c5ded5 122 //motor(down,# of steps);
melangeaddict 2:9c0a83c5ded5 123 next_state=3;
melangeaddict 2:9c0a83c5ded5 124 }
melangeaddict 2:9c0a83c5ded5 125 } else {
melangeaddict 2:9c0a83c5ded5 126 next_state=3;
melangeaddict 2:9c0a83c5ded5 127 }
melangeaddict 2:9c0a83c5ded5 128
melangeaddict 2:9c0a83c5ded5 129 //TODO:Determine number of steps per frequency change
melangeaddict 2:9c0a83c5ded5 130 //old_freq=new_freq;
melangeaddict 2:9c0a83c5ded5 131 break;
melangeaddict 2:9c0a83c5ded5 132 //-----------------------------------------
melangeaddict 3:c672e782f19b 133 case 4://Winding mode
melangeaddict 3:c672e782f19b 134 if (current_mode==winding_mode) {
melangeaddict 3:c672e782f19b 135 lcd.cls();
melangeaddict 3:c672e782f19b 136 lcd.printf("Winding Mode");
melangeaddict 3:c672e782f19b 137
melangeaddict 3:c672e782f19b 138 if (wind_up) {
melangeaddict 3:c672e782f19b 139 motor.motor_turn(up,5);//TODO:Adjust number of turns
melangeaddict 3:c672e782f19b 140 }
melangeaddict 3:c672e782f19b 141
melangeaddict 3:c672e782f19b 142 if (wind_down) {
melangeaddict 3:c672e782f19b 143 motor.motor_turn(down,5);//TODO:Adjust number of turns
melangeaddict 3:c672e782f19b 144 }
melangeaddict 3:c672e782f19b 145 next_state=4;
melangeaddict 3:c672e782f19b 146 } else {
melangeaddict 3:c672e782f19b 147 next_state=0;
melangeaddict 3:c672e782f19b 148 }
melangeaddict 3:c672e782f19b 149 break;
melangeaddict 2:9c0a83c5ded5 150 default:
melangeaddict 2:9c0a83c5ded5 151 break;
melangeaddict 3:c672e782f19b 152 }//end switch
melangeaddict 2:9c0a83c5ded5 153 wait_ms(5);
melangeaddict 2:9c0a83c5ded5 154
melangeaddict 2:9c0a83c5ded5 155 if (start_tuning==false) {//If the stop button is pressed, the state machine returns to user input
melangeaddict 2:9c0a83c5ded5 156 next_state=0;
melangeaddict 3:c672e782f19b 157
melangeaddict 2:9c0a83c5ded5 158 }
melangeaddict 3:c672e782f19b 159
melangeaddict 3:c672e782f19b 160 if (current_mode==winding_mode) {//TODO:Debug this
melangeaddict 3:c672e782f19b 161 next_state=4;
melangeaddict 3:c672e782f19b 162 }
melangeaddict 3:c672e782f19b 163
melangeaddict 3:c672e782f19b 164 if (start_tuning) {
melangeaddict 3:c672e782f19b 165 lcd.cls();
melangeaddict 3:c672e782f19b 166 lcd.printf("Detected %f\nDesired %f",new_freq,desired_freq);
melangeaddict 3:c672e782f19b 167 }
melangeaddict 3:c672e782f19b 168
melangeaddict 3:c672e782f19b 169 }//end while
melangeaddict 2:9c0a83c5ded5 170
melangeaddict 2:9c0a83c5ded5 171 // return 0;
melangeaddict 2:9c0a83c5ded5 172 }
melangeaddict 2:9c0a83c5ded5 173
melangeaddict 2:9c0a83c5ded5 174 //***************************************************
melangeaddict 2:9c0a83c5ded5 175 //******************functions************************
melangeaddict 3:c672e782f19b 176 //Display the string and pitch selection menu
melangeaddict 2:9c0a83c5ded5 177 void output_menu() {
melangeaddict 2:9c0a83c5ded5 178 lcd.cls();
melangeaddict 3:c672e782f19b 179 lcd.printf("Select String: %d",selected_string);
melangeaddict 3:c672e782f19b 180
melangeaddict 3:c672e782f19b 181 lcd.locate(0,1);
melangeaddict 3:c672e782f19b 182 strings *temp=&strings_array[selected_string];
melangeaddict 3:c672e782f19b 183 lcd.printf("Select Pitch: ");
melangeaddict 3:c672e782f19b 184 lcd.printf(temp->get_note().c_str());
melangeaddict 3:c672e782f19b 185 wait(.1);
melangeaddict 2:9c0a83c5ded5 186 }
melangeaddict 2:9c0a83c5ded5 187
melangeaddict 2:9c0a83c5ded5 188 //***************************************************
melangeaddict 3:c672e782f19b 189 //Initialize the buttons
melangeaddict 2:9c0a83c5ded5 190 void button_init() {
melangeaddict 2:9c0a83c5ded5 191 string_but.mode( PullDown );
melangeaddict 2:9c0a83c5ded5 192 string_but.setSampleFrequency();
melangeaddict 2:9c0a83c5ded5 193
melangeaddict 2:9c0a83c5ded5 194 pitch_but.mode( PullDown );
melangeaddict 2:9c0a83c5ded5 195 pitch_but.setSampleFrequency();
melangeaddict 2:9c0a83c5ded5 196
melangeaddict 2:9c0a83c5ded5 197 start_but.mode( PullDown );
melangeaddict 2:9c0a83c5ded5 198 start_but.setSampleFrequency();
melangeaddict 2:9c0a83c5ded5 199
melangeaddict 2:9c0a83c5ded5 200 mode_but.mode( PullDown );
melangeaddict 2:9c0a83c5ded5 201 mode_but.setSampleFrequency();
melangeaddict 2:9c0a83c5ded5 202 }
melangeaddict 2:9c0a83c5ded5 203
melangeaddict 2:9c0a83c5ded5 204 //***************************************************
melangeaddict 3:c672e782f19b 205 //Depending on the current mode, the buttons do different things
melangeaddict 2:9c0a83c5ded5 206 void setup_buttons() {
melangeaddict 3:c672e782f19b 207 if (current_mode==tuning_mode) {//Tuning mode
melangeaddict 2:9c0a83c5ded5 208 string_but.attach_asserted(&string_sel);
melangeaddict 3:c672e782f19b 209 string_but.attach_deasserted(&do_nothing);
melangeaddict 2:9c0a83c5ded5 210 pitch_but.attach_asserted(&pitch_sel);
melangeaddict 3:c672e782f19b 211 pitch_but.attach_deasserted(&do_nothing);
melangeaddict 2:9c0a83c5ded5 212 start_but.attach_asserted(&start);
melangeaddict 2:9c0a83c5ded5 213 mode_but.attach_asserted(&mode);
melangeaddict 3:c672e782f19b 214 } else {//Winding mode
melangeaddict 3:c672e782f19b 215 string_but.attach_asserted(&wind_up_start);
melangeaddict 3:c672e782f19b 216 string_but.attach_deasserted(&wind_up_stop);
melangeaddict 3:c672e782f19b 217 pitch_but.attach_asserted(&wind_down_start);
melangeaddict 3:c672e782f19b 218 pitch_but.attach_deasserted(&wind_down_stop);
melangeaddict 2:9c0a83c5ded5 219 start_but.attach_asserted(&do_nothing);
melangeaddict 2:9c0a83c5ded5 220 mode_but.attach_asserted(&mode);
melangeaddict 2:9c0a83c5ded5 221 }
melangeaddict 2:9c0a83c5ded5 222 }
melangeaddict 2:9c0a83c5ded5 223 //***************************************************
melangeaddict 3:c672e782f19b 224 //Change the selected string - there are only six strings
melangeaddict 2:9c0a83c5ded5 225 void string_sel() {
melangeaddict 2:9c0a83c5ded5 226 selected_string++;
melangeaddict 2:9c0a83c5ded5 227 if (selected_string>6)
melangeaddict 2:9c0a83c5ded5 228 selected_string=1;
melangeaddict 2:9c0a83c5ded5 229
melangeaddict 2:9c0a83c5ded5 230 output_menu();
melangeaddict 2:9c0a83c5ded5 231 }
melangeaddict 2:9c0a83c5ded5 232 //***************************************************
melangeaddict 3:c672e782f19b 233 //Change the pitch of the selected string
melangeaddict 2:9c0a83c5ded5 234 void pitch_sel() {
melangeaddict 2:9c0a83c5ded5 235 strings *temp=&strings_array[selected_string];
melangeaddict 2:9c0a83c5ded5 236 temp->inc_index();
melangeaddict 2:9c0a83c5ded5 237
melangeaddict 2:9c0a83c5ded5 238 output_menu();
melangeaddict 2:9c0a83c5ded5 239 }
melangeaddict 2:9c0a83c5ded5 240 //***************************************************
melangeaddict 3:c672e782f19b 241 //Start the tuning process
melangeaddict 2:9c0a83c5ded5 242 void start() {
melangeaddict 2:9c0a83c5ded5 243 start_tuning=true;
melangeaddict 3:c672e782f19b 244
melangeaddict 3:c672e782f19b 245 string_but.attach_asserted(&do_nothing);//Disable the other buttons
melangeaddict 3:c672e782f19b 246 pitch_but.attach_asserted(&do_nothing);
melangeaddict 3:c672e782f19b 247 start_but.attach_asserted(&stop); //except for the start/stop button
melangeaddict 3:c672e782f19b 248 mode_but.attach_asserted(&do_nothing);
melangeaddict 2:9c0a83c5ded5 249 }
melangeaddict 2:9c0a83c5ded5 250 //***************************************************
melangeaddict 2:9c0a83c5ded5 251 void mode() {
melangeaddict 2:9c0a83c5ded5 252 if (current_mode==tuning_mode) {
melangeaddict 2:9c0a83c5ded5 253 current_mode=winding_mode;
melangeaddict 2:9c0a83c5ded5 254 lcd.cls();
melangeaddict 2:9c0a83c5ded5 255 lcd.printf("Winding Mode");
melangeaddict 3:c672e782f19b 256 wait(.5);
melangeaddict 2:9c0a83c5ded5 257 } else {
melangeaddict 2:9c0a83c5ded5 258 current_mode=tuning_mode;
melangeaddict 2:9c0a83c5ded5 259 lcd.cls();
melangeaddict 2:9c0a83c5ded5 260 lcd.printf("Tuning Mode");
melangeaddict 3:c672e782f19b 261 wait(.5);
melangeaddict 2:9c0a83c5ded5 262 }
melangeaddict 3:c672e782f19b 263 setup_buttons();//Change the functions the buttons connect to
melangeaddict 2:9c0a83c5ded5 264 }
melangeaddict 2:9c0a83c5ded5 265 //***************************************************
melangeaddict 2:9c0a83c5ded5 266 void stop() {
melangeaddict 2:9c0a83c5ded5 267 start_tuning=false;
melangeaddict 2:9c0a83c5ded5 268 }
melangeaddict 2:9c0a83c5ded5 269 //***************************************************
melangeaddict 2:9c0a83c5ded5 270 void do_nothing() {
melangeaddict 2:9c0a83c5ded5 271 return;
melangeaddict 2:9c0a83c5ded5 272 }
melangeaddict 2:9c0a83c5ded5 273 //***************************************************
melangeaddict 3:c672e782f19b 274 void wind_up_start() {
melangeaddict 3:c672e782f19b 275 wind_up=true;
melangeaddict 2:9c0a83c5ded5 276 }
melangeaddict 2:9c0a83c5ded5 277 //***************************************************
melangeaddict 3:c672e782f19b 278 void wind_up_stop() {
melangeaddict 3:c672e782f19b 279 wind_up=false;
melangeaddict 3:c672e782f19b 280 }
melangeaddict 3:c672e782f19b 281 //***************************************************
melangeaddict 3:c672e782f19b 282 void wind_down_start() {
melangeaddict 3:c672e782f19b 283 wind_down=true;
melangeaddict 3:c672e782f19b 284 }
melangeaddict 3:c672e782f19b 285 //***************************************************
melangeaddict 3:c672e782f19b 286 void wind_down_stop() {
melangeaddict 3:c672e782f19b 287 wind_down=false;
melangeaddict 2:9c0a83c5ded5 288 }
melangeaddict 2:9c0a83c5ded5 289 //***************************************************
melangeaddict 2:9c0a83c5ded5 290 void device_init() {
melangeaddict 3:c672e782f19b 291
melangeaddict 2:9c0a83c5ded5 292 strings string1(1);
melangeaddict 2:9c0a83c5ded5 293 strings string2(2);
melangeaddict 2:9c0a83c5ded5 294 strings string3(3);
melangeaddict 2:9c0a83c5ded5 295 strings string4(4);
melangeaddict 2:9c0a83c5ded5 296 strings string5(5);
melangeaddict 2:9c0a83c5ded5 297 strings string6(6);
melangeaddict 3:c672e782f19b 298 /* strings_array.push_back(string1);
melangeaddict 3:c672e782f19b 299 strings_array.push_back(string2);
melangeaddict 3:c672e782f19b 300 strings_array.push_back(string3);
melangeaddict 3:c672e782f19b 301 strings_array.push_back(string4);
melangeaddict 3:c672e782f19b 302 strings_array.push_back(string5);
melangeaddict 3:c672e782f19b 303 strings_array.push_back(string6);
melangeaddict 3:c672e782f19b 304 */
melangeaddict 2:9c0a83c5ded5 305
melangeaddict 3:c672e782f19b 306 strings_array[0]=string1;
melangeaddict 3:c672e782f19b 307 strings_array[1]=string2;
melangeaddict 3:c672e782f19b 308 strings_array[2]=string3;
melangeaddict 3:c672e782f19b 309 strings_array[3]=string4;
melangeaddict 3:c672e782f19b 310 strings_array[4]=string5;
melangeaddict 3:c672e782f19b 311 strings_array[5]=string6;
melangeaddict 3:c672e782f19b 312
melangeaddict 3:c672e782f19b 313 lcd.cls();
melangeaddict 3:c672e782f19b 314 lcd.printf("device init");
melangeaddict 3:c672e782f19b 315 wait(.5);
melangeaddict 3:c672e782f19b 316
melangeaddict 3:c672e782f19b 317 selected_string=6;
melangeaddict 2:9c0a83c5ded5 318 current_mode=tuning_mode;
melangeaddict 2:9c0a83c5ded5 319 start_tuning=false;
melangeaddict 2:9c0a83c5ded5 320 up=true;
melangeaddict 2:9c0a83c5ded5 321 down=false;
melangeaddict 3:c672e782f19b 322 wind_up=true;
melangeaddict 2:9c0a83c5ded5 323
melangeaddict 2:9c0a83c5ded5 324 button_init();
melangeaddict 2:9c0a83c5ded5 325
melangeaddict 2:9c0a83c5ded5 326 output_menu();
melangeaddict 3:c672e782f19b 327
melangeaddict 2:9c0a83c5ded5 328 }
melangeaddict 2:9c0a83c5ded5 329 //***************************************************
melangeaddict 2:9c0a83c5ded5 330 void motor_calibration() {
melangeaddict 2:9c0a83c5ded5 331 lcd.cls();
melangeaddict 2:9c0a83c5ded5 332 lcd.printf("Calibrate Motor");
melangeaddict 3:c672e782f19b 333 wait(.5);
melangeaddict 2:9c0a83c5ded5 334
melangeaddict 2:9c0a83c5ded5 335 float freq=0, freq_up=0, freq_down=0;
melangeaddict 2:9c0a83c5ded5 336 bool done=false;
melangeaddict 2:9c0a83c5ded5 337 lcd.cls();
melangeaddict 2:9c0a83c5ded5 338 lcd.printf("Please pluck\nstring");
melangeaddict 2:9c0a83c5ded5 339
melangeaddict 2:9c0a83c5ded5 340 //motor.motor_turn(up,25)//TODO: Adjust the number of steps here
melangeaddict 2:9c0a83c5ded5 341 //On second thought, we don't need to tune up and down for this, we can find the current frequency
melangeaddict 2:9c0a83c5ded5 342 //and then turn the peg for the two frequencies!
melangeaddict 2:9c0a83c5ded5 343 while (!done) {
melangeaddict 2:9c0a83c5ded5 344 freq=guitar.find_frequency();
melangeaddict 2:9c0a83c5ded5 345
melangeaddict 2:9c0a83c5ded5 346 if (check_threshold(freq)) {
melangeaddict 3:c672e782f19b 347 lcd.cls();
melangeaddict 2:9c0a83c5ded5 348 freq_up=freq;
melangeaddict 2:9c0a83c5ded5 349 done=true;
melangeaddict 2:9c0a83c5ded5 350 }
melangeaddict 2:9c0a83c5ded5 351 }
melangeaddict 2:9c0a83c5ded5 352 motor.motor_turn(down,25);//TODO: Adjust the number of steps here
melangeaddict 2:9c0a83c5ded5 353 done=false;
melangeaddict 2:9c0a83c5ded5 354 while (!done) {
melangeaddict 2:9c0a83c5ded5 355 freq=guitar.find_frequency();
melangeaddict 2:9c0a83c5ded5 356
melangeaddict 2:9c0a83c5ded5 357 if (check_threshold(freq)) {
melangeaddict 3:c672e782f19b 358 lcd.cls();
melangeaddict 2:9c0a83c5ded5 359 freq_down=freq;
melangeaddict 2:9c0a83c5ded5 360 done=true;
melangeaddict 2:9c0a83c5ded5 361 }
melangeaddict 2:9c0a83c5ded5 362 }
melangeaddict 2:9c0a83c5ded5 363
melangeaddict 2:9c0a83c5ded5 364 if (freq_up<freq_down) {
melangeaddict 2:9c0a83c5ded5 365 down=true;
melangeaddict 2:9c0a83c5ded5 366 up=false;
melangeaddict 2:9c0a83c5ded5 367 }
melangeaddict 2:9c0a83c5ded5 368
melangeaddict 2:9c0a83c5ded5 369 lcd.cls();
melangeaddict 2:9c0a83c5ded5 370 lcd.printf("Calibration Done");
melangeaddict 2:9c0a83c5ded5 371 }
melangeaddict 2:9c0a83c5ded5 372 //**********************************************
melangeaddict 2:9c0a83c5ded5 373 bool check_threshold(float freq) {
melangeaddict 2:9c0a83c5ded5 374 if (freq>500) {
melangeaddict 2:9c0a83c5ded5 375 lcd.cls();
melangeaddict 2:9c0a83c5ded5 376 lcd.printf("Pluck string \nagain");
melangeaddict 2:9c0a83c5ded5 377 return false;
melangeaddict 2:9c0a83c5ded5 378 } else
melangeaddict 2:9c0a83c5ded5 379 return true;
melangeaddict 2:9c0a83c5ded5 380 }