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:
Thu Apr 19 20:31:42 2012 +0000
Revision:
6:46fb12f6ace1
Parent:
5:c0fd99f07536
Child:
7:057bcd99a3f8
Working with hardware

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