A program to automatically tune a guitar. Written by Justin Reidhead and Steven Swenson
Dependencies: FFT FrequencyFinder Motor NewTextLCD PinDetect mbed strings
main.cpp@4:e370f322a697, 2012-04-16 (annotated)
- Committer:
- melangeaddict
- Date:
- Mon Apr 16 23:17:49 2012 +0000
- Revision:
- 4:e370f322a697
- Parent:
- 3:c672e782f19b
- Child:
- 5:c0fd99f07536
Menus are working
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 | 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 | 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 | 2:9c0a83c5ded5 | 108 | lcd.printf("String %d\ntuned",selected_string); |
melangeaddict | 2:9c0a83c5ded5 | 109 | wait(.5); |
melangeaddict | 2:9c0a83c5ded5 | 110 | |
melangeaddict | 2:9c0a83c5ded5 | 111 | start_tuning=false; |
melangeaddict | 2:9c0a83c5ded5 | 112 | next_state=0; |
melangeaddict | 3:c672e782f19b | 113 | } else if ((desired_freq-.5)>new_freq) {//We are too low, and need to turn the string tigher |
melangeaddict | 2:9c0a83c5ded5 | 114 | //motor(up,# of steps); |
melangeaddict | 2:9c0a83c5ded5 | 115 | next_state=3; |
melangeaddict | 3:c672e782f19b | 116 | } else {//We are too high, and need to loosen the string |
melangeaddict | 2:9c0a83c5ded5 | 117 | //motor(down,# of steps); |
melangeaddict | 2:9c0a83c5ded5 | 118 | next_state=3; |
melangeaddict | 2:9c0a83c5ded5 | 119 | } |
melangeaddict | 2:9c0a83c5ded5 | 120 | } else { |
melangeaddict | 2:9c0a83c5ded5 | 121 | next_state=3; |
melangeaddict | 2:9c0a83c5ded5 | 122 | } |
melangeaddict | 2:9c0a83c5ded5 | 123 | |
melangeaddict | 2:9c0a83c5ded5 | 124 | //TODO:Determine number of steps per frequency change |
melangeaddict | 2:9c0a83c5ded5 | 125 | //old_freq=new_freq; |
melangeaddict | 2:9c0a83c5ded5 | 126 | break; |
melangeaddict | 2:9c0a83c5ded5 | 127 | //----------------------------------------- |
melangeaddict | 3:c672e782f19b | 128 | case 4://Winding mode |
melangeaddict | 3:c672e782f19b | 129 | if (current_mode==winding_mode) { |
melangeaddict | 3:c672e782f19b | 130 | if (wind_up) { |
melangeaddict | 3:c672e782f19b | 131 | motor.motor_turn(up,5);//TODO:Adjust number of turns |
melangeaddict | 3:c672e782f19b | 132 | } |
melangeaddict | 3:c672e782f19b | 133 | |
melangeaddict | 3:c672e782f19b | 134 | if (wind_down) { |
melangeaddict | 3:c672e782f19b | 135 | motor.motor_turn(down,5);//TODO:Adjust number of turns |
melangeaddict | 3:c672e782f19b | 136 | } |
melangeaddict | 3:c672e782f19b | 137 | next_state=4; |
melangeaddict | 3:c672e782f19b | 138 | } else { |
melangeaddict | 3:c672e782f19b | 139 | next_state=0; |
melangeaddict | 3:c672e782f19b | 140 | } |
melangeaddict | 3:c672e782f19b | 141 | break; |
melangeaddict | 2:9c0a83c5ded5 | 142 | default: |
melangeaddict | 2:9c0a83c5ded5 | 143 | break; |
melangeaddict | 3:c672e782f19b | 144 | }//end switch |
melangeaddict | 2:9c0a83c5ded5 | 145 | wait_ms(5); |
melangeaddict | 2:9c0a83c5ded5 | 146 | |
melangeaddict | 2:9c0a83c5ded5 | 147 | if (start_tuning==false) {//If the stop button is pressed, the state machine returns to user input |
melangeaddict | 2:9c0a83c5ded5 | 148 | next_state=0; |
melangeaddict | 4:e370f322a697 | 149 | //setup_buttons(); |
melangeaddict | 4:e370f322a697 | 150 | //output_menu(); |
melangeaddict | 2:9c0a83c5ded5 | 151 | } |
melangeaddict | 3:c672e782f19b | 152 | |
melangeaddict | 3:c672e782f19b | 153 | if (current_mode==winding_mode) {//TODO:Debug this |
melangeaddict | 3:c672e782f19b | 154 | next_state=4; |
melangeaddict | 3:c672e782f19b | 155 | } |
melangeaddict | 3:c672e782f19b | 156 | |
melangeaddict | 3:c672e782f19b | 157 | if (start_tuning) { |
melangeaddict | 3:c672e782f19b | 158 | lcd.cls(); |
melangeaddict | 3:c672e782f19b | 159 | lcd.printf("Detected %f\nDesired %f",new_freq,desired_freq); |
melangeaddict | 3:c672e782f19b | 160 | } |
melangeaddict | 3:c672e782f19b | 161 | |
melangeaddict | 3:c672e782f19b | 162 | }//end while |
melangeaddict | 2:9c0a83c5ded5 | 163 | |
melangeaddict | 2:9c0a83c5ded5 | 164 | // return 0; |
melangeaddict | 2:9c0a83c5ded5 | 165 | } |
melangeaddict | 2:9c0a83c5ded5 | 166 | |
melangeaddict | 2:9c0a83c5ded5 | 167 | //*************************************************** |
melangeaddict | 2:9c0a83c5ded5 | 168 | //******************functions************************ |
melangeaddict | 3:c672e782f19b | 169 | //Display the string and pitch selection menu |
melangeaddict | 2:9c0a83c5ded5 | 170 | void output_menu() { |
melangeaddict | 2:9c0a83c5ded5 | 171 | lcd.cls(); |
melangeaddict | 4:e370f322a697 | 172 | lcd.printf("Select String: %d",selected_string+1); |
melangeaddict | 4:e370f322a697 | 173 | |
melangeaddict | 3:c672e782f19b | 174 | lcd.locate(0,1); |
melangeaddict | 3:c672e782f19b | 175 | strings *temp=&strings_array[selected_string]; |
melangeaddict | 3:c672e782f19b | 176 | lcd.printf("Select Pitch: "); |
melangeaddict | 4:e370f322a697 | 177 | lcd.printf("%s",temp->get_note());//temp->get_note().c_str());TODO:Fix!!!! |
melangeaddict | 4:e370f322a697 | 178 | wait(.5); |
melangeaddict | 2:9c0a83c5ded5 | 179 | } |
melangeaddict | 2:9c0a83c5ded5 | 180 | |
melangeaddict | 2:9c0a83c5ded5 | 181 | //*************************************************** |
melangeaddict | 3:c672e782f19b | 182 | //Initialize the buttons |
melangeaddict | 2:9c0a83c5ded5 | 183 | void button_init() { |
melangeaddict | 2:9c0a83c5ded5 | 184 | string_but.mode( PullDown ); |
melangeaddict | 2:9c0a83c5ded5 | 185 | string_but.setSampleFrequency(); |
melangeaddict | 2:9c0a83c5ded5 | 186 | |
melangeaddict | 2:9c0a83c5ded5 | 187 | pitch_but.mode( PullDown ); |
melangeaddict | 2:9c0a83c5ded5 | 188 | pitch_but.setSampleFrequency(); |
melangeaddict | 2:9c0a83c5ded5 | 189 | |
melangeaddict | 2:9c0a83c5ded5 | 190 | start_but.mode( PullDown ); |
melangeaddict | 2:9c0a83c5ded5 | 191 | start_but.setSampleFrequency(); |
melangeaddict | 2:9c0a83c5ded5 | 192 | |
melangeaddict | 2:9c0a83c5ded5 | 193 | mode_but.mode( PullDown ); |
melangeaddict | 2:9c0a83c5ded5 | 194 | mode_but.setSampleFrequency(); |
melangeaddict | 4:e370f322a697 | 195 | |
melangeaddict | 4:e370f322a697 | 196 | setup_buttons(); |
melangeaddict | 2:9c0a83c5ded5 | 197 | } |
melangeaddict | 2:9c0a83c5ded5 | 198 | |
melangeaddict | 2:9c0a83c5ded5 | 199 | //*************************************************** |
melangeaddict | 3:c672e782f19b | 200 | //Depending on the current mode, the buttons do different things |
melangeaddict | 2:9c0a83c5ded5 | 201 | void setup_buttons() { |
melangeaddict | 3:c672e782f19b | 202 | if (current_mode==tuning_mode) {//Tuning mode |
melangeaddict | 2:9c0a83c5ded5 | 203 | string_but.attach_asserted(&string_sel); |
melangeaddict | 3:c672e782f19b | 204 | string_but.attach_deasserted(&do_nothing); |
melangeaddict | 2:9c0a83c5ded5 | 205 | pitch_but.attach_asserted(&pitch_sel); |
melangeaddict | 3:c672e782f19b | 206 | pitch_but.attach_deasserted(&do_nothing); |
melangeaddict | 2:9c0a83c5ded5 | 207 | start_but.attach_asserted(&start); |
melangeaddict | 2:9c0a83c5ded5 | 208 | mode_but.attach_asserted(&mode); |
melangeaddict | 3:c672e782f19b | 209 | } else {//Winding mode |
melangeaddict | 3:c672e782f19b | 210 | string_but.attach_asserted(&wind_up_start); |
melangeaddict | 3:c672e782f19b | 211 | string_but.attach_deasserted(&wind_up_stop); |
melangeaddict | 3:c672e782f19b | 212 | pitch_but.attach_asserted(&wind_down_start); |
melangeaddict | 3:c672e782f19b | 213 | pitch_but.attach_deasserted(&wind_down_stop); |
melangeaddict | 2:9c0a83c5ded5 | 214 | start_but.attach_asserted(&do_nothing); |
melangeaddict | 2:9c0a83c5ded5 | 215 | mode_but.attach_asserted(&mode); |
melangeaddict | 2:9c0a83c5ded5 | 216 | } |
melangeaddict | 2:9c0a83c5ded5 | 217 | } |
melangeaddict | 2:9c0a83c5ded5 | 218 | //*************************************************** |
melangeaddict | 3:c672e782f19b | 219 | //Change the selected string - there are only six strings |
melangeaddict | 2:9c0a83c5ded5 | 220 | void string_sel() { |
melangeaddict | 2:9c0a83c5ded5 | 221 | selected_string++; |
melangeaddict | 4:e370f322a697 | 222 | if (selected_string>5) |
melangeaddict | 4:e370f322a697 | 223 | selected_string=0; |
melangeaddict | 2:9c0a83c5ded5 | 224 | |
melangeaddict | 2:9c0a83c5ded5 | 225 | output_menu(); |
melangeaddict | 2:9c0a83c5ded5 | 226 | } |
melangeaddict | 2:9c0a83c5ded5 | 227 | //*************************************************** |
melangeaddict | 3:c672e782f19b | 228 | //Change the pitch of the selected string |
melangeaddict | 2:9c0a83c5ded5 | 229 | void pitch_sel() { |
melangeaddict | 2:9c0a83c5ded5 | 230 | strings *temp=&strings_array[selected_string]; |
melangeaddict | 2:9c0a83c5ded5 | 231 | temp->inc_index(); |
melangeaddict | 2:9c0a83c5ded5 | 232 | |
melangeaddict | 2:9c0a83c5ded5 | 233 | output_menu(); |
melangeaddict | 2:9c0a83c5ded5 | 234 | } |
melangeaddict | 2:9c0a83c5ded5 | 235 | //*************************************************** |
melangeaddict | 3:c672e782f19b | 236 | //Start the tuning process |
melangeaddict | 2:9c0a83c5ded5 | 237 | void start() { |
melangeaddict | 2:9c0a83c5ded5 | 238 | start_tuning=true; |
melangeaddict | 3:c672e782f19b | 239 | |
melangeaddict | 3:c672e782f19b | 240 | string_but.attach_asserted(&do_nothing);//Disable the other buttons |
melangeaddict | 3:c672e782f19b | 241 | pitch_but.attach_asserted(&do_nothing); |
melangeaddict | 3:c672e782f19b | 242 | start_but.attach_asserted(&stop); //except for the start/stop button |
melangeaddict | 3:c672e782f19b | 243 | mode_but.attach_asserted(&do_nothing); |
melangeaddict | 2:9c0a83c5ded5 | 244 | } |
melangeaddict | 2:9c0a83c5ded5 | 245 | //*************************************************** |
melangeaddict | 2:9c0a83c5ded5 | 246 | void mode() { |
melangeaddict | 2:9c0a83c5ded5 | 247 | if (current_mode==tuning_mode) { |
melangeaddict | 2:9c0a83c5ded5 | 248 | current_mode=winding_mode; |
melangeaddict | 2:9c0a83c5ded5 | 249 | lcd.cls(); |
melangeaddict | 2:9c0a83c5ded5 | 250 | lcd.printf("Winding Mode"); |
melangeaddict | 4:e370f322a697 | 251 | wait(1); |
melangeaddict | 4:e370f322a697 | 252 | lcd.cls(); |
melangeaddict | 4:e370f322a697 | 253 | lcd.printf("Start for up\nStop for down"); |
melangeaddict | 2:9c0a83c5ded5 | 254 | } else { |
melangeaddict | 2:9c0a83c5ded5 | 255 | current_mode=tuning_mode; |
melangeaddict | 2:9c0a83c5ded5 | 256 | lcd.cls(); |
melangeaddict | 2:9c0a83c5ded5 | 257 | lcd.printf("Tuning Mode"); |
melangeaddict | 3:c672e782f19b | 258 | wait(.5); |
melangeaddict | 4:e370f322a697 | 259 | output_menu(); |
melangeaddict | 2:9c0a83c5ded5 | 260 | } |
melangeaddict | 3:c672e782f19b | 261 | setup_buttons();//Change the functions the buttons connect to |
melangeaddict | 2:9c0a83c5ded5 | 262 | } |
melangeaddict | 2:9c0a83c5ded5 | 263 | //*************************************************** |
melangeaddict | 2:9c0a83c5ded5 | 264 | void stop() { |
melangeaddict | 2:9c0a83c5ded5 | 265 | start_tuning=false; |
melangeaddict | 2:9c0a83c5ded5 | 266 | } |
melangeaddict | 2:9c0a83c5ded5 | 267 | //*************************************************** |
melangeaddict | 2:9c0a83c5ded5 | 268 | void do_nothing() { |
melangeaddict | 2:9c0a83c5ded5 | 269 | return; |
melangeaddict | 2:9c0a83c5ded5 | 270 | } |
melangeaddict | 2:9c0a83c5ded5 | 271 | //*************************************************** |
melangeaddict | 3:c672e782f19b | 272 | void wind_up_start() { |
melangeaddict | 3:c672e782f19b | 273 | wind_up=true; |
melangeaddict | 2:9c0a83c5ded5 | 274 | } |
melangeaddict | 2:9c0a83c5ded5 | 275 | //*************************************************** |
melangeaddict | 3:c672e782f19b | 276 | void wind_up_stop() { |
melangeaddict | 3:c672e782f19b | 277 | wind_up=false; |
melangeaddict | 3:c672e782f19b | 278 | } |
melangeaddict | 3:c672e782f19b | 279 | //*************************************************** |
melangeaddict | 3:c672e782f19b | 280 | void wind_down_start() { |
melangeaddict | 3:c672e782f19b | 281 | wind_down=true; |
melangeaddict | 3:c672e782f19b | 282 | } |
melangeaddict | 3:c672e782f19b | 283 | //*************************************************** |
melangeaddict | 3:c672e782f19b | 284 | void wind_down_stop() { |
melangeaddict | 3:c672e782f19b | 285 | wind_down=false; |
melangeaddict | 2:9c0a83c5ded5 | 286 | } |
melangeaddict | 2:9c0a83c5ded5 | 287 | //*************************************************** |
melangeaddict | 2:9c0a83c5ded5 | 288 | void device_init() { |
melangeaddict | 3:c672e782f19b | 289 | |
melangeaddict | 2:9c0a83c5ded5 | 290 | strings string1(1); |
melangeaddict | 2:9c0a83c5ded5 | 291 | strings string2(2); |
melangeaddict | 2:9c0a83c5ded5 | 292 | strings string3(3); |
melangeaddict | 2:9c0a83c5ded5 | 293 | strings string4(4); |
melangeaddict | 2:9c0a83c5ded5 | 294 | strings string5(5); |
melangeaddict | 2:9c0a83c5ded5 | 295 | strings string6(6); |
melangeaddict | 2:9c0a83c5ded5 | 296 | |
melangeaddict | 3:c672e782f19b | 297 | strings_array[0]=string1; |
melangeaddict | 3:c672e782f19b | 298 | strings_array[1]=string2; |
melangeaddict | 3:c672e782f19b | 299 | strings_array[2]=string3; |
melangeaddict | 3:c672e782f19b | 300 | strings_array[3]=string4; |
melangeaddict | 3:c672e782f19b | 301 | strings_array[4]=string5; |
melangeaddict | 3:c672e782f19b | 302 | strings_array[5]=string6; |
melangeaddict | 3:c672e782f19b | 303 | |
melangeaddict | 4:e370f322a697 | 304 | selected_string=5; |
melangeaddict | 2:9c0a83c5ded5 | 305 | current_mode=tuning_mode; |
melangeaddict | 2:9c0a83c5ded5 | 306 | start_tuning=false; |
melangeaddict | 2:9c0a83c5ded5 | 307 | up=true; |
melangeaddict | 2:9c0a83c5ded5 | 308 | down=false; |
melangeaddict | 3:c672e782f19b | 309 | wind_up=true; |
melangeaddict | 2:9c0a83c5ded5 | 310 | |
melangeaddict | 2:9c0a83c5ded5 | 311 | button_init(); |
melangeaddict | 2:9c0a83c5ded5 | 312 | |
melangeaddict | 2:9c0a83c5ded5 | 313 | output_menu(); |
melangeaddict | 3:c672e782f19b | 314 | |
melangeaddict | 2:9c0a83c5ded5 | 315 | } |
melangeaddict | 2:9c0a83c5ded5 | 316 | //*************************************************** |
melangeaddict | 2:9c0a83c5ded5 | 317 | void motor_calibration() { |
melangeaddict | 2:9c0a83c5ded5 | 318 | lcd.cls(); |
melangeaddict | 2:9c0a83c5ded5 | 319 | lcd.printf("Calibrate Motor"); |
melangeaddict | 3:c672e782f19b | 320 | wait(.5); |
melangeaddict | 2:9c0a83c5ded5 | 321 | |
melangeaddict | 2:9c0a83c5ded5 | 322 | float freq=0, freq_up=0, freq_down=0; |
melangeaddict | 2:9c0a83c5ded5 | 323 | bool done=false; |
melangeaddict | 2:9c0a83c5ded5 | 324 | lcd.cls(); |
melangeaddict | 2:9c0a83c5ded5 | 325 | lcd.printf("Please pluck\nstring"); |
melangeaddict | 4:e370f322a697 | 326 | wait(1); |
melangeaddict | 2:9c0a83c5ded5 | 327 | |
melangeaddict | 2:9c0a83c5ded5 | 328 | //motor.motor_turn(up,25)//TODO: Adjust the number of steps here |
melangeaddict | 2:9c0a83c5ded5 | 329 | //On second thought, we don't need to tune up and down for this, we can find the current frequency |
melangeaddict | 2:9c0a83c5ded5 | 330 | //and then turn the peg for the two frequencies! |
melangeaddict | 2:9c0a83c5ded5 | 331 | while (!done) { |
melangeaddict | 2:9c0a83c5ded5 | 332 | freq=guitar.find_frequency(); |
melangeaddict | 2:9c0a83c5ded5 | 333 | |
melangeaddict | 2:9c0a83c5ded5 | 334 | if (check_threshold(freq)) { |
melangeaddict | 3:c672e782f19b | 335 | lcd.cls(); |
melangeaddict | 2:9c0a83c5ded5 | 336 | freq_up=freq; |
melangeaddict | 2:9c0a83c5ded5 | 337 | done=true; |
melangeaddict | 2:9c0a83c5ded5 | 338 | } |
melangeaddict | 4:e370f322a697 | 339 | |
melangeaddict | 4:e370f322a697 | 340 | if (start_tuning==false) |
melangeaddict | 4:e370f322a697 | 341 | break; |
melangeaddict | 2:9c0a83c5ded5 | 342 | } |
melangeaddict | 2:9c0a83c5ded5 | 343 | motor.motor_turn(down,25);//TODO: Adjust the number of steps here |
melangeaddict | 2:9c0a83c5ded5 | 344 | done=false; |
melangeaddict | 2:9c0a83c5ded5 | 345 | while (!done) { |
melangeaddict | 2:9c0a83c5ded5 | 346 | freq=guitar.find_frequency(); |
melangeaddict | 2:9c0a83c5ded5 | 347 | |
melangeaddict | 2:9c0a83c5ded5 | 348 | if (check_threshold(freq)) { |
melangeaddict | 3:c672e782f19b | 349 | lcd.cls(); |
melangeaddict | 2:9c0a83c5ded5 | 350 | freq_down=freq; |
melangeaddict | 2:9c0a83c5ded5 | 351 | done=true; |
melangeaddict | 2:9c0a83c5ded5 | 352 | } |
melangeaddict | 4:e370f322a697 | 353 | if (start_tuning==false) |
melangeaddict | 4:e370f322a697 | 354 | break; |
melangeaddict | 2:9c0a83c5ded5 | 355 | } |
melangeaddict | 2:9c0a83c5ded5 | 356 | |
melangeaddict | 2:9c0a83c5ded5 | 357 | if (freq_up<freq_down) { |
melangeaddict | 2:9c0a83c5ded5 | 358 | down=true; |
melangeaddict | 2:9c0a83c5ded5 | 359 | up=false; |
melangeaddict | 2:9c0a83c5ded5 | 360 | } |
melangeaddict | 2:9c0a83c5ded5 | 361 | |
melangeaddict | 2:9c0a83c5ded5 | 362 | lcd.cls(); |
melangeaddict | 2:9c0a83c5ded5 | 363 | lcd.printf("Calibration Done"); |
melangeaddict | 4:e370f322a697 | 364 | |
melangeaddict | 4:e370f322a697 | 365 | if (start_tuning==false) { |
melangeaddict | 4:e370f322a697 | 366 | output_menu(); |
melangeaddict | 4:e370f322a697 | 367 | setup_buttons(); |
melangeaddict | 4:e370f322a697 | 368 | } |
melangeaddict | 2:9c0a83c5ded5 | 369 | } |
melangeaddict | 2:9c0a83c5ded5 | 370 | //********************************************** |
melangeaddict | 2:9c0a83c5ded5 | 371 | bool check_threshold(float freq) { |
melangeaddict | 2:9c0a83c5ded5 | 372 | if (freq>500) { |
melangeaddict | 2:9c0a83c5ded5 | 373 | lcd.cls(); |
melangeaddict | 2:9c0a83c5ded5 | 374 | lcd.printf("Pluck string \nagain"); |
melangeaddict | 2:9c0a83c5ded5 | 375 | return false; |
melangeaddict | 2:9c0a83c5ded5 | 376 | } else |
melangeaddict | 2:9c0a83c5ded5 | 377 | return true; |
melangeaddict | 2:9c0a83c5ded5 | 378 | } |