A program to automatically tune a guitar. Written by Justin Reidhead and Steven Swenson
Dependencies: FFT FrequencyFinder Motor NewTextLCD PinDetect mbed strings
main.cpp@5:c0fd99f07536, 2012-04-17 (annotated)
- Committer:
- melangeaddict
- Date:
- Tue Apr 17 19:46:33 2012 +0000
- Revision:
- 5:c0fd99f07536
- Parent:
- 4:e370f322a697
- Child:
- 6:46fb12f6ace1
It's a trap!
Who changed what in which revision?
User | Revision | Line number | New 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 | 5:c0fd99f07536 | 20 | TextLCD lcd(p10,p9,p8,p7,p6,p5);//Our method of communication with LCD screen (rs e d4 d5 d6 d7) |
melangeaddict | 5:c0fd99f07536 | 21 | Motor motor(p19,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 | 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 | 2:9c0a83c5ded5 | 69 | int state=0,next_state=0; |
melangeaddict | 2:9c0a83c5ded5 | 70 | //float old_freq=0; |
melangeaddict | 2:9c0a83c5ded5 | 71 | float new_freq=0; |
melangeaddict | 2:9c0a83c5ded5 | 72 | float desired_freq=0; |
melangeaddict | 2:9c0a83c5ded5 | 73 | strings *temp=&strings_array[0]; |
melangeaddict | 3:c672e782f19b | 74 | |
melangeaddict | 3:c672e782f19b | 75 | output_menu(); |
melangeaddict | 3:c672e782f19b | 76 | |
melangeaddict | 2:9c0a83c5ded5 | 77 | while (1) { |
melangeaddict | 2:9c0a83c5ded5 | 78 | state=next_state; |
melangeaddict | 2:9c0a83c5ded5 | 79 | |
melangeaddict | 2:9c0a83c5ded5 | 80 | switch (state) { |
melangeaddict | 2:9c0a83c5ded5 | 81 | //------------------------------------ |
melangeaddict | 2:9c0a83c5ded5 | 82 | case 0://Stay here till the user selects the string and pitch |
melangeaddict | 2:9c0a83c5ded5 | 83 | if (start_tuning==true) { |
melangeaddict | 2:9c0a83c5ded5 | 84 | next_state=1; |
melangeaddict | 2:9c0a83c5ded5 | 85 | } else { |
melangeaddict | 2:9c0a83c5ded5 | 86 | next_state=0; |
melangeaddict | 3:c672e782f19b | 87 | //output_menu(); |
melangeaddict | 2:9c0a83c5ded5 | 88 | } |
melangeaddict | 2:9c0a83c5ded5 | 89 | break; |
melangeaddict | 2:9c0a83c5ded5 | 90 | //---------------------------------------- |
melangeaddict | 2:9c0a83c5ded5 | 91 | case 1://motor calibration state |
melangeaddict | 3:c672e782f19b | 92 | motor_calibration();//determine which direction is up and down for the motor |
melangeaddict | 2:9c0a83c5ded5 | 93 | next_state=2; |
melangeaddict | 2:9c0a83c5ded5 | 94 | break; |
melangeaddict | 2:9c0a83c5ded5 | 95 | //----------------------------------------- |
melangeaddict | 2:9c0a83c5ded5 | 96 | case 2://begin the actual tuning |
melangeaddict | 2:9c0a83c5ded5 | 97 | temp=&strings_array[selected_string]; |
melangeaddict | 3:c672e782f19b | 98 | desired_freq=temp->get_freq();//Get the desired frequency for the string selected |
melangeaddict | 2:9c0a83c5ded5 | 99 | |
melangeaddict | 2:9c0a83c5ded5 | 100 | next_state=3; |
melangeaddict | 2:9c0a83c5ded5 | 101 | break; |
melangeaddict | 2:9c0a83c5ded5 | 102 | //----------------------------------------- |
melangeaddict | 3:c672e782f19b | 103 | case 3://Do the dirty work of tuning |
melangeaddict | 3:c672e782f19b | 104 | new_freq=guitar.find_frequency();//Get the current frequency of the string |
melangeaddict | 3:c672e782f19b | 105 | if (check_threshold(new_freq)) {//The check_threshold function makes sure the frequency is valid (less than 500 Hz) |
melangeaddict | 2:9c0a83c5ded5 | 106 | if ((desired_freq-.5)<new_freq && (desired_freq+.5)>new_freq) {//We are within .5Hz of the desired frequency |
melangeaddict | 2:9c0a83c5ded5 | 107 | lcd.cls(); |
melangeaddict | 5:c0fd99f07536 | 108 | lcd.printf("String %d\ntuned",selected_string+1); |
melangeaddict | 2:9c0a83c5ded5 | 109 | wait(.5); |
melangeaddict | 2:9c0a83c5ded5 | 110 | |
melangeaddict | 2:9c0a83c5ded5 | 111 | start_tuning=false; |
melangeaddict | 5:c0fd99f07536 | 112 | output_menu(); |
melangeaddict | 5:c0fd99f07536 | 113 | setup_buttons(); |
melangeaddict | 2:9c0a83c5ded5 | 114 | next_state=0; |
melangeaddict | 3:c672e782f19b | 115 | } else if ((desired_freq-.5)>new_freq) {//We are too low, and need to turn the string tigher |
melangeaddict | 5:c0fd99f07536 | 116 | motor.motor_turn(up,10);//TODO:Adjust # of steps |
melangeaddict | 2:9c0a83c5ded5 | 117 | next_state=3; |
melangeaddict | 3:c672e782f19b | 118 | } else {//We are too high, and need to loosen the string |
melangeaddict | 5:c0fd99f07536 | 119 | motor.motor_turn(down,10); |
melangeaddict | 2:9c0a83c5ded5 | 120 | next_state=3; |
melangeaddict | 2:9c0a83c5ded5 | 121 | } |
melangeaddict | 2:9c0a83c5ded5 | 122 | } else { |
melangeaddict | 2:9c0a83c5ded5 | 123 | next_state=3; |
melangeaddict | 2:9c0a83c5ded5 | 124 | } |
melangeaddict | 2:9c0a83c5ded5 | 125 | |
melangeaddict | 2:9c0a83c5ded5 | 126 | //TODO:Determine number of steps per frequency change |
melangeaddict | 2:9c0a83c5ded5 | 127 | //old_freq=new_freq; |
melangeaddict | 2:9c0a83c5ded5 | 128 | break; |
melangeaddict | 2:9c0a83c5ded5 | 129 | //----------------------------------------- |
melangeaddict | 3:c672e782f19b | 130 | case 4://Winding mode |
melangeaddict | 3:c672e782f19b | 131 | if (current_mode==winding_mode) { |
melangeaddict | 3:c672e782f19b | 132 | if (wind_up) { |
melangeaddict | 3:c672e782f19b | 133 | motor.motor_turn(up,5);//TODO:Adjust number of turns |
melangeaddict | 3:c672e782f19b | 134 | } |
melangeaddict | 3:c672e782f19b | 135 | |
melangeaddict | 3:c672e782f19b | 136 | if (wind_down) { |
melangeaddict | 3:c672e782f19b | 137 | motor.motor_turn(down,5);//TODO:Adjust number of turns |
melangeaddict | 3:c672e782f19b | 138 | } |
melangeaddict | 3:c672e782f19b | 139 | next_state=4; |
melangeaddict | 3:c672e782f19b | 140 | } else { |
melangeaddict | 3:c672e782f19b | 141 | next_state=0; |
melangeaddict | 3:c672e782f19b | 142 | } |
melangeaddict | 3:c672e782f19b | 143 | break; |
melangeaddict | 2:9c0a83c5ded5 | 144 | default: |
melangeaddict | 2:9c0a83c5ded5 | 145 | break; |
melangeaddict | 3:c672e782f19b | 146 | }//end switch |
melangeaddict | 2:9c0a83c5ded5 | 147 | wait_ms(5); |
melangeaddict | 2:9c0a83c5ded5 | 148 | |
melangeaddict | 2:9c0a83c5ded5 | 149 | if (start_tuning==false) {//If the stop button is pressed, the state machine returns to user input |
melangeaddict | 2:9c0a83c5ded5 | 150 | next_state=0; |
melangeaddict | 4:e370f322a697 | 151 | //setup_buttons(); |
melangeaddict | 4:e370f322a697 | 152 | //output_menu(); |
melangeaddict | 2:9c0a83c5ded5 | 153 | } |
melangeaddict | 3:c672e782f19b | 154 | |
melangeaddict | 3:c672e782f19b | 155 | if (current_mode==winding_mode) {//TODO:Debug this |
melangeaddict | 3:c672e782f19b | 156 | next_state=4; |
melangeaddict | 3:c672e782f19b | 157 | } |
melangeaddict | 3:c672e782f19b | 158 | |
melangeaddict | 3:c672e782f19b | 159 | if (start_tuning) { |
melangeaddict | 3:c672e782f19b | 160 | lcd.cls(); |
melangeaddict | 5:c0fd99f07536 | 161 | lcd.printf("Detected %f ",new_freq); |
melangeaddict | 5:c0fd99f07536 | 162 | lcd.locate(0,1); |
melangeaddict | 5:c0fd99f07536 | 163 | lcd.printf("Desired %f",desired_freq); |
melangeaddict | 3:c672e782f19b | 164 | } |
melangeaddict | 3:c672e782f19b | 165 | |
melangeaddict | 3:c672e782f19b | 166 | }//end while |
melangeaddict | 2:9c0a83c5ded5 | 167 | |
melangeaddict | 2:9c0a83c5ded5 | 168 | // return 0; |
melangeaddict | 2:9c0a83c5ded5 | 169 | } |
melangeaddict | 2:9c0a83c5ded5 | 170 | |
melangeaddict | 2:9c0a83c5ded5 | 171 | //*************************************************** |
melangeaddict | 2:9c0a83c5ded5 | 172 | //******************functions************************ |
melangeaddict | 3:c672e782f19b | 173 | //Display the string and pitch selection menu |
melangeaddict | 2:9c0a83c5ded5 | 174 | void output_menu() { |
melangeaddict | 2:9c0a83c5ded5 | 175 | lcd.cls(); |
melangeaddict | 4:e370f322a697 | 176 | lcd.printf("Select String: %d",selected_string+1); |
melangeaddict | 4:e370f322a697 | 177 | |
melangeaddict | 3:c672e782f19b | 178 | lcd.locate(0,1); |
melangeaddict | 3:c672e782f19b | 179 | strings *temp=&strings_array[selected_string]; |
melangeaddict | 3:c672e782f19b | 180 | lcd.printf("Select Pitch: "); |
melangeaddict | 5:c0fd99f07536 | 181 | lcd.printf("%s",temp->get_note()); |
melangeaddict | 5:c0fd99f07536 | 182 | //wait(.5); |
melangeaddict | 2:9c0a83c5ded5 | 183 | } |
melangeaddict | 2:9c0a83c5ded5 | 184 | |
melangeaddict | 2:9c0a83c5ded5 | 185 | //*************************************************** |
melangeaddict | 3:c672e782f19b | 186 | //Initialize the buttons |
melangeaddict | 2:9c0a83c5ded5 | 187 | void button_init() { |
melangeaddict | 2:9c0a83c5ded5 | 188 | string_but.mode( PullDown ); |
melangeaddict | 2:9c0a83c5ded5 | 189 | string_but.setSampleFrequency(); |
melangeaddict | 2:9c0a83c5ded5 | 190 | |
melangeaddict | 2:9c0a83c5ded5 | 191 | pitch_but.mode( PullDown ); |
melangeaddict | 2:9c0a83c5ded5 | 192 | pitch_but.setSampleFrequency(); |
melangeaddict | 2:9c0a83c5ded5 | 193 | |
melangeaddict | 2:9c0a83c5ded5 | 194 | start_but.mode( PullDown ); |
melangeaddict | 2:9c0a83c5ded5 | 195 | start_but.setSampleFrequency(); |
melangeaddict | 2:9c0a83c5ded5 | 196 | |
melangeaddict | 2:9c0a83c5ded5 | 197 | mode_but.mode( PullDown ); |
melangeaddict | 2:9c0a83c5ded5 | 198 | mode_but.setSampleFrequency(); |
melangeaddict | 4:e370f322a697 | 199 | |
melangeaddict | 4:e370f322a697 | 200 | setup_buttons(); |
melangeaddict | 2:9c0a83c5ded5 | 201 | } |
melangeaddict | 2:9c0a83c5ded5 | 202 | |
melangeaddict | 2:9c0a83c5ded5 | 203 | //*************************************************** |
melangeaddict | 3:c672e782f19b | 204 | //Depending on the current mode, the buttons do different things |
melangeaddict | 2:9c0a83c5ded5 | 205 | void setup_buttons() { |
melangeaddict | 3:c672e782f19b | 206 | if (current_mode==tuning_mode) {//Tuning mode |
melangeaddict | 2:9c0a83c5ded5 | 207 | string_but.attach_asserted(&string_sel); |
melangeaddict | 3:c672e782f19b | 208 | string_but.attach_deasserted(&do_nothing); |
melangeaddict | 2:9c0a83c5ded5 | 209 | pitch_but.attach_asserted(&pitch_sel); |
melangeaddict | 3:c672e782f19b | 210 | pitch_but.attach_deasserted(&do_nothing); |
melangeaddict | 2:9c0a83c5ded5 | 211 | start_but.attach_asserted(&start); |
melangeaddict | 2:9c0a83c5ded5 | 212 | mode_but.attach_asserted(&mode); |
melangeaddict | 3:c672e782f19b | 213 | } else {//Winding mode |
melangeaddict | 3:c672e782f19b | 214 | string_but.attach_asserted(&wind_up_start); |
melangeaddict | 3:c672e782f19b | 215 | string_but.attach_deasserted(&wind_up_stop); |
melangeaddict | 3:c672e782f19b | 216 | pitch_but.attach_asserted(&wind_down_start); |
melangeaddict | 3:c672e782f19b | 217 | pitch_but.attach_deasserted(&wind_down_stop); |
melangeaddict | 2:9c0a83c5ded5 | 218 | start_but.attach_asserted(&do_nothing); |
melangeaddict | 2:9c0a83c5ded5 | 219 | mode_but.attach_asserted(&mode); |
melangeaddict | 2:9c0a83c5ded5 | 220 | } |
melangeaddict | 2:9c0a83c5ded5 | 221 | } |
melangeaddict | 2:9c0a83c5ded5 | 222 | //*************************************************** |
melangeaddict | 3:c672e782f19b | 223 | //Change the selected string - there are only six strings |
melangeaddict | 2:9c0a83c5ded5 | 224 | void string_sel() { |
melangeaddict | 2:9c0a83c5ded5 | 225 | selected_string++; |
melangeaddict | 4:e370f322a697 | 226 | if (selected_string>5) |
melangeaddict | 4:e370f322a697 | 227 | selected_string=0; |
melangeaddict | 2:9c0a83c5ded5 | 228 | |
melangeaddict | 5:c0fd99f07536 | 229 | strings *temp=&strings_array[selected_string]; |
melangeaddict | 5:c0fd99f07536 | 230 | temp->reset_index(); |
melangeaddict | 5:c0fd99f07536 | 231 | |
melangeaddict | 2:9c0a83c5ded5 | 232 | output_menu(); |
melangeaddict | 2:9c0a83c5ded5 | 233 | } |
melangeaddict | 2:9c0a83c5ded5 | 234 | //*************************************************** |
melangeaddict | 3:c672e782f19b | 235 | //Change the pitch of the selected string |
melangeaddict | 2:9c0a83c5ded5 | 236 | void pitch_sel() { |
melangeaddict | 2:9c0a83c5ded5 | 237 | strings *temp=&strings_array[selected_string]; |
melangeaddict | 2:9c0a83c5ded5 | 238 | temp->inc_index(); |
melangeaddict | 2:9c0a83c5ded5 | 239 | |
melangeaddict | 2:9c0a83c5ded5 | 240 | output_menu(); |
melangeaddict | 2:9c0a83c5ded5 | 241 | } |
melangeaddict | 2:9c0a83c5ded5 | 242 | //*************************************************** |
melangeaddict | 3:c672e782f19b | 243 | //Start the tuning process |
melangeaddict | 2:9c0a83c5ded5 | 244 | void start() { |
melangeaddict | 2:9c0a83c5ded5 | 245 | start_tuning=true; |
melangeaddict | 3:c672e782f19b | 246 | |
melangeaddict | 3:c672e782f19b | 247 | string_but.attach_asserted(&do_nothing);//Disable the other buttons |
melangeaddict | 5:c0fd99f07536 | 248 | string_but.attach_deasserted(&do_nothing); |
melangeaddict | 3:c672e782f19b | 249 | pitch_but.attach_asserted(&do_nothing); |
melangeaddict | 5:c0fd99f07536 | 250 | pitch_but.attach_deasserted(&do_nothing); |
melangeaddict | 3:c672e782f19b | 251 | start_but.attach_asserted(&stop); //except for the start/stop button |
melangeaddict | 3:c672e782f19b | 252 | mode_but.attach_asserted(&do_nothing); |
melangeaddict | 2:9c0a83c5ded5 | 253 | } |
melangeaddict | 2:9c0a83c5ded5 | 254 | //*************************************************** |
melangeaddict | 2:9c0a83c5ded5 | 255 | void mode() { |
melangeaddict | 2:9c0a83c5ded5 | 256 | if (current_mode==tuning_mode) { |
melangeaddict | 2:9c0a83c5ded5 | 257 | current_mode=winding_mode; |
melangeaddict | 2:9c0a83c5ded5 | 258 | lcd.cls(); |
melangeaddict | 2:9c0a83c5ded5 | 259 | lcd.printf("Winding Mode"); |
melangeaddict | 4:e370f322a697 | 260 | wait(1); |
melangeaddict | 4:e370f322a697 | 261 | lcd.cls(); |
melangeaddict | 5:c0fd99f07536 | 262 | lcd.printf("String for up\nPitch for down"); |
melangeaddict | 2:9c0a83c5ded5 | 263 | } else { |
melangeaddict | 2:9c0a83c5ded5 | 264 | current_mode=tuning_mode; |
melangeaddict | 2:9c0a83c5ded5 | 265 | lcd.cls(); |
melangeaddict | 2:9c0a83c5ded5 | 266 | lcd.printf("Tuning Mode"); |
melangeaddict | 3:c672e782f19b | 267 | wait(.5); |
melangeaddict | 4:e370f322a697 | 268 | output_menu(); |
melangeaddict | 2:9c0a83c5ded5 | 269 | } |
melangeaddict | 3:c672e782f19b | 270 | setup_buttons();//Change the functions the buttons connect to |
melangeaddict | 2:9c0a83c5ded5 | 271 | } |
melangeaddict | 2:9c0a83c5ded5 | 272 | //*************************************************** |
melangeaddict | 2:9c0a83c5ded5 | 273 | void stop() { |
melangeaddict | 2:9c0a83c5ded5 | 274 | start_tuning=false; |
melangeaddict | 2:9c0a83c5ded5 | 275 | } |
melangeaddict | 2:9c0a83c5ded5 | 276 | //*************************************************** |
melangeaddict | 2:9c0a83c5ded5 | 277 | void do_nothing() { |
melangeaddict | 2:9c0a83c5ded5 | 278 | return; |
melangeaddict | 2:9c0a83c5ded5 | 279 | } |
melangeaddict | 2:9c0a83c5ded5 | 280 | //*************************************************** |
melangeaddict | 3:c672e782f19b | 281 | void wind_up_start() { |
melangeaddict | 3:c672e782f19b | 282 | wind_up=true; |
melangeaddict | 2:9c0a83c5ded5 | 283 | } |
melangeaddict | 2:9c0a83c5ded5 | 284 | //*************************************************** |
melangeaddict | 3:c672e782f19b | 285 | void wind_up_stop() { |
melangeaddict | 3:c672e782f19b | 286 | wind_up=false; |
melangeaddict | 3:c672e782f19b | 287 | } |
melangeaddict | 3:c672e782f19b | 288 | //*************************************************** |
melangeaddict | 3:c672e782f19b | 289 | void wind_down_start() { |
melangeaddict | 3:c672e782f19b | 290 | wind_down=true; |
melangeaddict | 3:c672e782f19b | 291 | } |
melangeaddict | 3:c672e782f19b | 292 | //*************************************************** |
melangeaddict | 3:c672e782f19b | 293 | void wind_down_stop() { |
melangeaddict | 3:c672e782f19b | 294 | wind_down=false; |
melangeaddict | 2:9c0a83c5ded5 | 295 | } |
melangeaddict | 2:9c0a83c5ded5 | 296 | //*************************************************** |
melangeaddict | 2:9c0a83c5ded5 | 297 | void device_init() { |
melangeaddict | 3:c672e782f19b | 298 | |
melangeaddict | 2:9c0a83c5ded5 | 299 | strings string1(1); |
melangeaddict | 2:9c0a83c5ded5 | 300 | strings string2(2); |
melangeaddict | 2:9c0a83c5ded5 | 301 | strings string3(3); |
melangeaddict | 2:9c0a83c5ded5 | 302 | strings string4(4); |
melangeaddict | 2:9c0a83c5ded5 | 303 | strings string5(5); |
melangeaddict | 2:9c0a83c5ded5 | 304 | strings string6(6); |
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 | 4:e370f322a697 | 313 | selected_string=5; |
melangeaddict | 2:9c0a83c5ded5 | 314 | current_mode=tuning_mode; |
melangeaddict | 2:9c0a83c5ded5 | 315 | start_tuning=false; |
melangeaddict | 2:9c0a83c5ded5 | 316 | up=true; |
melangeaddict | 2:9c0a83c5ded5 | 317 | down=false; |
melangeaddict | 3:c672e782f19b | 318 | wind_up=true; |
melangeaddict | 2:9c0a83c5ded5 | 319 | |
melangeaddict | 2:9c0a83c5ded5 | 320 | button_init(); |
melangeaddict | 2:9c0a83c5ded5 | 321 | |
melangeaddict | 2:9c0a83c5ded5 | 322 | output_menu(); |
melangeaddict | 3:c672e782f19b | 323 | |
melangeaddict | 2:9c0a83c5ded5 | 324 | } |
melangeaddict | 2:9c0a83c5ded5 | 325 | //*************************************************** |
melangeaddict | 2:9c0a83c5ded5 | 326 | void motor_calibration() { |
melangeaddict | 2:9c0a83c5ded5 | 327 | lcd.cls(); |
melangeaddict | 2:9c0a83c5ded5 | 328 | lcd.printf("Calibrate Motor"); |
melangeaddict | 3:c672e782f19b | 329 | wait(.5); |
melangeaddict | 2:9c0a83c5ded5 | 330 | |
melangeaddict | 2:9c0a83c5ded5 | 331 | float freq=0, freq_up=0, freq_down=0; |
melangeaddict | 2:9c0a83c5ded5 | 332 | bool done=false; |
melangeaddict | 2:9c0a83c5ded5 | 333 | lcd.cls(); |
melangeaddict | 2:9c0a83c5ded5 | 334 | lcd.printf("Please pluck\nstring"); |
melangeaddict | 4:e370f322a697 | 335 | wait(1); |
melangeaddict | 2:9c0a83c5ded5 | 336 | |
melangeaddict | 2:9c0a83c5ded5 | 337 | //motor.motor_turn(up,25)//TODO: Adjust the number of steps here |
melangeaddict | 2:9c0a83c5ded5 | 338 | //On second thought, we don't need to tune up and down for this, we can find the current frequency |
melangeaddict | 2:9c0a83c5ded5 | 339 | //and then turn the peg for the two frequencies! |
melangeaddict | 2:9c0a83c5ded5 | 340 | while (!done) { |
melangeaddict | 2:9c0a83c5ded5 | 341 | freq=guitar.find_frequency(); |
melangeaddict | 2:9c0a83c5ded5 | 342 | |
melangeaddict | 2:9c0a83c5ded5 | 343 | if (check_threshold(freq)) { |
melangeaddict | 3:c672e782f19b | 344 | lcd.cls(); |
melangeaddict | 2:9c0a83c5ded5 | 345 | freq_up=freq; |
melangeaddict | 2:9c0a83c5ded5 | 346 | done=true; |
melangeaddict | 2:9c0a83c5ded5 | 347 | } |
melangeaddict | 4:e370f322a697 | 348 | |
melangeaddict | 4:e370f322a697 | 349 | if (start_tuning==false) |
melangeaddict | 4:e370f322a697 | 350 | break; |
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 | 4:e370f322a697 | 362 | if (start_tuning==false) |
melangeaddict | 4:e370f322a697 | 363 | break; |
melangeaddict | 2:9c0a83c5ded5 | 364 | } |
melangeaddict | 2:9c0a83c5ded5 | 365 | |
melangeaddict | 2:9c0a83c5ded5 | 366 | if (freq_up<freq_down) { |
melangeaddict | 2:9c0a83c5ded5 | 367 | down=true; |
melangeaddict | 2:9c0a83c5ded5 | 368 | up=false; |
melangeaddict | 2:9c0a83c5ded5 | 369 | } |
melangeaddict | 2:9c0a83c5ded5 | 370 | |
melangeaddict | 2:9c0a83c5ded5 | 371 | lcd.cls(); |
melangeaddict | 2:9c0a83c5ded5 | 372 | lcd.printf("Calibration Done"); |
melangeaddict | 4:e370f322a697 | 373 | |
melangeaddict | 4:e370f322a697 | 374 | if (start_tuning==false) { |
melangeaddict | 4:e370f322a697 | 375 | output_menu(); |
melangeaddict | 4:e370f322a697 | 376 | setup_buttons(); |
melangeaddict | 4:e370f322a697 | 377 | } |
melangeaddict | 2:9c0a83c5ded5 | 378 | } |
melangeaddict | 2:9c0a83c5ded5 | 379 | //********************************************** |
melangeaddict | 2:9c0a83c5ded5 | 380 | bool check_threshold(float freq) { |
melangeaddict | 2:9c0a83c5ded5 | 381 | if (freq>500) { |
melangeaddict | 2:9c0a83c5ded5 | 382 | lcd.cls(); |
melangeaddict | 2:9c0a83c5ded5 | 383 | lcd.printf("Pluck string \nagain"); |
melangeaddict | 2:9c0a83c5ded5 | 384 | return false; |
melangeaddict | 2:9c0a83c5ded5 | 385 | } else |
melangeaddict | 2:9c0a83c5ded5 | 386 | return true; |
melangeaddict | 2:9c0a83c5ded5 | 387 | } |