A program to automatically tune a guitar. Written by Justin Reidhead and Steven Swenson
Dependencies: FFT FrequencyFinder Motor NewTextLCD PinDetect mbed strings
main.cpp@7:057bcd99a3f8, 2012-04-20 (annotated)
- Committer:
- melangeaddict
- Date:
- Fri Apr 20 17:28:27 2012 +0000
- Revision:
- 7:057bcd99a3f8
- Parent:
- 6:46fb12f6ace1
- Child:
- 8:651fbf5ae98a
kinda works; lcd looks good
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 | 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 | 7:057bcd99a3f8 | 21 | Motor motor(p24,p19,p21);//Setup for the motor (enable, direction, step) |
melangeaddict | 7:057bcd99a3f8 | 22 | FrequencyFinder guitar(p20);//Interface to get data from guitar (input pin) |
melangeaddict | 7:057bcd99a3f8 | 23 | AnalogOut bias(p18);//1.6v DC offset |
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 | 7:057bcd99a3f8 | 35 | short 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 | 7:057bcd99a3f8 | 74 | |
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 | 7:057bcd99a3f8 | 83 | wait(.5); |
melangeaddict | 3:c672e782f19b | 84 | |
melangeaddict | 3:c672e782f19b | 85 | output_menu(); |
melangeaddict | 3:c672e782f19b | 86 | |
melangeaddict | 2:9c0a83c5ded5 | 87 | while (1) { |
melangeaddict | 2:9c0a83c5ded5 | 88 | state=next_state; |
melangeaddict | 2:9c0a83c5ded5 | 89 | |
melangeaddict | 2:9c0a83c5ded5 | 90 | switch (state) { |
melangeaddict | 2:9c0a83c5ded5 | 91 | //------------------------------------ |
melangeaddict | 2:9c0a83c5ded5 | 92 | case 0://Stay here till the user selects the string and pitch |
melangeaddict | 2:9c0a83c5ded5 | 93 | if (start_tuning==true) { |
melangeaddict | 2:9c0a83c5ded5 | 94 | next_state=1; |
melangeaddict | 2:9c0a83c5ded5 | 95 | } else { |
melangeaddict | 2:9c0a83c5ded5 | 96 | next_state=0; |
melangeaddict | 3:c672e782f19b | 97 | //output_menu(); |
melangeaddict | 2:9c0a83c5ded5 | 98 | } |
melangeaddict | 2:9c0a83c5ded5 | 99 | break; |
melangeaddict | 2:9c0a83c5ded5 | 100 | //---------------------------------------- |
melangeaddict | 2:9c0a83c5ded5 | 101 | case 1://motor calibration state |
melangeaddict | 3:c672e782f19b | 102 | motor_calibration();//determine which direction is up and down for the motor |
melangeaddict | 2:9c0a83c5ded5 | 103 | next_state=2; |
melangeaddict | 2:9c0a83c5ded5 | 104 | break; |
melangeaddict | 2:9c0a83c5ded5 | 105 | //----------------------------------------- |
melangeaddict | 2:9c0a83c5ded5 | 106 | case 2://begin the actual tuning |
melangeaddict | 2:9c0a83c5ded5 | 107 | temp=&strings_array[selected_string]; |
melangeaddict | 3:c672e782f19b | 108 | desired_freq=temp->get_freq();//Get the desired frequency for the string selected |
melangeaddict | 2:9c0a83c5ded5 | 109 | |
melangeaddict | 2:9c0a83c5ded5 | 110 | next_state=3; |
melangeaddict | 2:9c0a83c5ded5 | 111 | break; |
melangeaddict | 2:9c0a83c5ded5 | 112 | //----------------------------------------- |
melangeaddict | 3:c672e782f19b | 113 | case 3://Do the dirty work of tuning |
melangeaddict | 3:c672e782f19b | 114 | new_freq=guitar.find_frequency();//Get the current frequency of the string |
melangeaddict | 3:c672e782f19b | 115 | if (check_threshold(new_freq)) {//The check_threshold function makes sure the frequency is valid (less than 500 Hz) |
melangeaddict | 2:9c0a83c5ded5 | 116 | if ((desired_freq-.5)<new_freq && (desired_freq+.5)>new_freq) {//We are within .5Hz of the desired frequency |
melangeaddict | 2:9c0a83c5ded5 | 117 | lcd.cls(); |
melangeaddict | 5:c0fd99f07536 | 118 | lcd.printf("String %d\ntuned",selected_string+1); |
melangeaddict | 2:9c0a83c5ded5 | 119 | wait(.5); |
melangeaddict | 2:9c0a83c5ded5 | 120 | |
melangeaddict | 2:9c0a83c5ded5 | 121 | start_tuning=false; |
melangeaddict | 5:c0fd99f07536 | 122 | output_menu(); |
melangeaddict | 5:c0fd99f07536 | 123 | setup_buttons(); |
melangeaddict | 2:9c0a83c5ded5 | 124 | next_state=0; |
melangeaddict | 3:c672e782f19b | 125 | } else if ((desired_freq-.5)>new_freq) {//We are too low, and need to turn the string tigher |
melangeaddict | 5:c0fd99f07536 | 126 | motor.motor_turn(up,10);//TODO:Adjust # of steps |
melangeaddict | 2:9c0a83c5ded5 | 127 | next_state=3; |
melangeaddict | 3:c672e782f19b | 128 | } else {//We are too high, and need to loosen the string |
melangeaddict | 5:c0fd99f07536 | 129 | motor.motor_turn(down,10); |
melangeaddict | 2:9c0a83c5ded5 | 130 | next_state=3; |
melangeaddict | 2:9c0a83c5ded5 | 131 | } |
melangeaddict | 2:9c0a83c5ded5 | 132 | } else { |
melangeaddict | 2:9c0a83c5ded5 | 133 | next_state=3; |
melangeaddict | 2:9c0a83c5ded5 | 134 | } |
melangeaddict | 2:9c0a83c5ded5 | 135 | |
melangeaddict | 2:9c0a83c5ded5 | 136 | //TODO:Determine number of steps per frequency change |
melangeaddict | 2:9c0a83c5ded5 | 137 | //old_freq=new_freq; |
melangeaddict | 2:9c0a83c5ded5 | 138 | break; |
melangeaddict | 2:9c0a83c5ded5 | 139 | //----------------------------------------- |
melangeaddict | 3:c672e782f19b | 140 | case 4://Winding mode |
melangeaddict | 3:c672e782f19b | 141 | if (current_mode==winding_mode) { |
melangeaddict | 3:c672e782f19b | 142 | if (wind_up) { |
melangeaddict | 3:c672e782f19b | 143 | motor.motor_turn(up,5);//TODO:Adjust number of turns |
melangeaddict | 3:c672e782f19b | 144 | } |
melangeaddict | 3:c672e782f19b | 145 | |
melangeaddict | 3:c672e782f19b | 146 | if (wind_down) { |
melangeaddict | 3:c672e782f19b | 147 | motor.motor_turn(down,5);//TODO:Adjust number of turns |
melangeaddict | 3:c672e782f19b | 148 | } |
melangeaddict | 3:c672e782f19b | 149 | next_state=4; |
melangeaddict | 3:c672e782f19b | 150 | } else { |
melangeaddict | 7:057bcd99a3f8 | 151 | output_menu(); |
melangeaddict | 7:057bcd99a3f8 | 152 | wind_up=false; |
melangeaddict | 7:057bcd99a3f8 | 153 | wind_down=false; |
melangeaddict | 7:057bcd99a3f8 | 154 | setup_buttons(); |
melangeaddict | 3:c672e782f19b | 155 | next_state=0; |
melangeaddict | 3:c672e782f19b | 156 | } |
melangeaddict | 3:c672e782f19b | 157 | break; |
melangeaddict | 2:9c0a83c5ded5 | 158 | default: |
melangeaddict | 2:9c0a83c5ded5 | 159 | break; |
melangeaddict | 3:c672e782f19b | 160 | }//end switch |
melangeaddict | 2:9c0a83c5ded5 | 161 | wait_ms(5); |
melangeaddict | 2:9c0a83c5ded5 | 162 | |
melangeaddict | 2:9c0a83c5ded5 | 163 | if (start_tuning==false) {//If the stop button is pressed, the state machine returns to user input |
melangeaddict | 2:9c0a83c5ded5 | 164 | next_state=0; |
melangeaddict | 4:e370f322a697 | 165 | //setup_buttons(); |
melangeaddict | 4:e370f322a697 | 166 | //output_menu(); |
melangeaddict | 2:9c0a83c5ded5 | 167 | } |
melangeaddict | 3:c672e782f19b | 168 | |
melangeaddict | 3:c672e782f19b | 169 | if (current_mode==winding_mode) {//TODO:Debug this |
melangeaddict | 3:c672e782f19b | 170 | next_state=4; |
melangeaddict | 3:c672e782f19b | 171 | } |
melangeaddict | 3:c672e782f19b | 172 | |
melangeaddict | 3:c672e782f19b | 173 | if (start_tuning) { |
melangeaddict | 3:c672e782f19b | 174 | lcd.cls(); |
melangeaddict | 7:057bcd99a3f8 | 175 | |
melangeaddict | 7:057bcd99a3f8 | 176 | lcd.locate(9,0); |
melangeaddict | 7:057bcd99a3f8 | 177 | lcd.printf("%f",new_freq); |
melangeaddict | 7:057bcd99a3f8 | 178 | lcd.locate(9,1); |
melangeaddict | 7:057bcd99a3f8 | 179 | lcd.printf("%f",desired_freq); |
melangeaddict | 7:057bcd99a3f8 | 180 | |
melangeaddict | 5:c0fd99f07536 | 181 | lcd.locate(0,1); |
melangeaddict | 7:057bcd99a3f8 | 182 | lcd.printf("Desired"); |
melangeaddict | 7:057bcd99a3f8 | 183 | lcd.locate(0,0); |
melangeaddict | 7:057bcd99a3f8 | 184 | lcd.printf("Detected"); |
melangeaddict | 3:c672e782f19b | 185 | } |
melangeaddict | 3:c672e782f19b | 186 | |
melangeaddict | 3:c672e782f19b | 187 | }//end while |
melangeaddict | 2:9c0a83c5ded5 | 188 | |
melangeaddict | 2:9c0a83c5ded5 | 189 | // return 0; |
melangeaddict | 2:9c0a83c5ded5 | 190 | } |
melangeaddict | 2:9c0a83c5ded5 | 191 | |
melangeaddict | 2:9c0a83c5ded5 | 192 | //*************************************************** |
melangeaddict | 2:9c0a83c5ded5 | 193 | //******************functions************************ |
melangeaddict | 3:c672e782f19b | 194 | //Display the string and pitch selection menu |
melangeaddict | 2:9c0a83c5ded5 | 195 | void output_menu() { |
melangeaddict | 2:9c0a83c5ded5 | 196 | lcd.cls(); |
melangeaddict | 4:e370f322a697 | 197 | lcd.printf("Select String: %d",selected_string+1); |
melangeaddict | 4:e370f322a697 | 198 | |
melangeaddict | 3:c672e782f19b | 199 | lcd.locate(0,1); |
melangeaddict | 3:c672e782f19b | 200 | strings *temp=&strings_array[selected_string]; |
melangeaddict | 3:c672e782f19b | 201 | lcd.printf("Select Pitch: "); |
melangeaddict | 5:c0fd99f07536 | 202 | lcd.printf("%s",temp->get_note()); |
melangeaddict | 5:c0fd99f07536 | 203 | //wait(.5); |
melangeaddict | 2:9c0a83c5ded5 | 204 | } |
melangeaddict | 2:9c0a83c5ded5 | 205 | |
melangeaddict | 2:9c0a83c5ded5 | 206 | //*************************************************** |
melangeaddict | 3:c672e782f19b | 207 | //Initialize the buttons |
melangeaddict | 2:9c0a83c5ded5 | 208 | void button_init() { |
melangeaddict | 2:9c0a83c5ded5 | 209 | string_but.mode( PullDown ); |
melangeaddict | 2:9c0a83c5ded5 | 210 | string_but.setSampleFrequency(); |
melangeaddict | 2:9c0a83c5ded5 | 211 | |
melangeaddict | 2:9c0a83c5ded5 | 212 | pitch_but.mode( PullDown ); |
melangeaddict | 2:9c0a83c5ded5 | 213 | pitch_but.setSampleFrequency(); |
melangeaddict | 2:9c0a83c5ded5 | 214 | |
melangeaddict | 2:9c0a83c5ded5 | 215 | start_but.mode( PullDown ); |
melangeaddict | 2:9c0a83c5ded5 | 216 | start_but.setSampleFrequency(); |
melangeaddict | 2:9c0a83c5ded5 | 217 | |
melangeaddict | 2:9c0a83c5ded5 | 218 | mode_but.mode( PullDown ); |
melangeaddict | 2:9c0a83c5ded5 | 219 | mode_but.setSampleFrequency(); |
melangeaddict | 4:e370f322a697 | 220 | |
melangeaddict | 4:e370f322a697 | 221 | setup_buttons(); |
melangeaddict | 2:9c0a83c5ded5 | 222 | } |
melangeaddict | 2:9c0a83c5ded5 | 223 | |
melangeaddict | 2:9c0a83c5ded5 | 224 | //*************************************************** |
melangeaddict | 3:c672e782f19b | 225 | //Depending on the current mode, the buttons do different things |
melangeaddict | 2:9c0a83c5ded5 | 226 | void setup_buttons() { |
melangeaddict | 3:c672e782f19b | 227 | if (current_mode==tuning_mode) {//Tuning mode |
melangeaddict | 2:9c0a83c5ded5 | 228 | string_but.attach_asserted(&string_sel); |
melangeaddict | 3:c672e782f19b | 229 | string_but.attach_deasserted(&do_nothing); |
melangeaddict | 2:9c0a83c5ded5 | 230 | pitch_but.attach_asserted(&pitch_sel); |
melangeaddict | 3:c672e782f19b | 231 | pitch_but.attach_deasserted(&do_nothing); |
melangeaddict | 2:9c0a83c5ded5 | 232 | start_but.attach_asserted(&start); |
melangeaddict | 2:9c0a83c5ded5 | 233 | mode_but.attach_asserted(&mode); |
melangeaddict | 3:c672e782f19b | 234 | } else {//Winding mode |
melangeaddict | 3:c672e782f19b | 235 | string_but.attach_asserted(&wind_up_start); |
melangeaddict | 3:c672e782f19b | 236 | string_but.attach_deasserted(&wind_up_stop); |
melangeaddict | 3:c672e782f19b | 237 | pitch_but.attach_asserted(&wind_down_start); |
melangeaddict | 3:c672e782f19b | 238 | pitch_but.attach_deasserted(&wind_down_stop); |
melangeaddict | 2:9c0a83c5ded5 | 239 | start_but.attach_asserted(&do_nothing); |
melangeaddict | 2:9c0a83c5ded5 | 240 | mode_but.attach_asserted(&mode); |
melangeaddict | 2:9c0a83c5ded5 | 241 | } |
melangeaddict | 2:9c0a83c5ded5 | 242 | } |
melangeaddict | 2:9c0a83c5ded5 | 243 | //*************************************************** |
melangeaddict | 3:c672e782f19b | 244 | //Change the selected string - there are only six strings |
melangeaddict | 2:9c0a83c5ded5 | 245 | void string_sel() { |
melangeaddict | 2:9c0a83c5ded5 | 246 | selected_string++; |
melangeaddict | 4:e370f322a697 | 247 | if (selected_string>5) |
melangeaddict | 4:e370f322a697 | 248 | selected_string=0; |
melangeaddict | 2:9c0a83c5ded5 | 249 | |
melangeaddict | 5:c0fd99f07536 | 250 | strings *temp=&strings_array[selected_string]; |
melangeaddict | 5:c0fd99f07536 | 251 | temp->reset_index(); |
melangeaddict | 5:c0fd99f07536 | 252 | |
melangeaddict | 2:9c0a83c5ded5 | 253 | output_menu(); |
melangeaddict | 2:9c0a83c5ded5 | 254 | } |
melangeaddict | 2:9c0a83c5ded5 | 255 | //*************************************************** |
melangeaddict | 3:c672e782f19b | 256 | //Change the pitch of the selected string |
melangeaddict | 2:9c0a83c5ded5 | 257 | void pitch_sel() { |
melangeaddict | 2:9c0a83c5ded5 | 258 | strings *temp=&strings_array[selected_string]; |
melangeaddict | 2:9c0a83c5ded5 | 259 | temp->inc_index(); |
melangeaddict | 2:9c0a83c5ded5 | 260 | |
melangeaddict | 2:9c0a83c5ded5 | 261 | output_menu(); |
melangeaddict | 2:9c0a83c5ded5 | 262 | } |
melangeaddict | 2:9c0a83c5ded5 | 263 | //*************************************************** |
melangeaddict | 3:c672e782f19b | 264 | //Start the tuning process |
melangeaddict | 2:9c0a83c5ded5 | 265 | void start() { |
melangeaddict | 2:9c0a83c5ded5 | 266 | start_tuning=true; |
melangeaddict | 3:c672e782f19b | 267 | |
melangeaddict | 3:c672e782f19b | 268 | string_but.attach_asserted(&do_nothing);//Disable the other buttons |
melangeaddict | 5:c0fd99f07536 | 269 | string_but.attach_deasserted(&do_nothing); |
melangeaddict | 3:c672e782f19b | 270 | pitch_but.attach_asserted(&do_nothing); |
melangeaddict | 5:c0fd99f07536 | 271 | pitch_but.attach_deasserted(&do_nothing); |
melangeaddict | 3:c672e782f19b | 272 | start_but.attach_asserted(&stop); //except for the start/stop button |
melangeaddict | 3:c672e782f19b | 273 | mode_but.attach_asserted(&do_nothing); |
melangeaddict | 2:9c0a83c5ded5 | 274 | } |
melangeaddict | 2:9c0a83c5ded5 | 275 | //*************************************************** |
melangeaddict | 2:9c0a83c5ded5 | 276 | void mode() { |
melangeaddict | 2:9c0a83c5ded5 | 277 | if (current_mode==tuning_mode) { |
melangeaddict | 2:9c0a83c5ded5 | 278 | current_mode=winding_mode; |
melangeaddict | 2:9c0a83c5ded5 | 279 | lcd.cls(); |
melangeaddict | 2:9c0a83c5ded5 | 280 | lcd.printf("Winding Mode"); |
melangeaddict | 4:e370f322a697 | 281 | wait(1); |
melangeaddict | 4:e370f322a697 | 282 | lcd.cls(); |
melangeaddict | 5:c0fd99f07536 | 283 | lcd.printf("String for up\nPitch for down"); |
melangeaddict | 2:9c0a83c5ded5 | 284 | } else { |
melangeaddict | 2:9c0a83c5ded5 | 285 | current_mode=tuning_mode; |
melangeaddict | 2:9c0a83c5ded5 | 286 | lcd.cls(); |
melangeaddict | 2:9c0a83c5ded5 | 287 | lcd.printf("Tuning Mode"); |
melangeaddict | 3:c672e782f19b | 288 | wait(.5); |
melangeaddict | 4:e370f322a697 | 289 | output_menu(); |
melangeaddict | 2:9c0a83c5ded5 | 290 | } |
melangeaddict | 3:c672e782f19b | 291 | setup_buttons();//Change the functions the buttons connect to |
melangeaddict | 2:9c0a83c5ded5 | 292 | } |
melangeaddict | 2:9c0a83c5ded5 | 293 | //*************************************************** |
melangeaddict | 2:9c0a83c5ded5 | 294 | void stop() { |
melangeaddict | 2:9c0a83c5ded5 | 295 | start_tuning=false; |
melangeaddict | 7:057bcd99a3f8 | 296 | |
melangeaddict | 7:057bcd99a3f8 | 297 | output_menu(); |
melangeaddict | 7:057bcd99a3f8 | 298 | setup_buttons(); |
melangeaddict | 2:9c0a83c5ded5 | 299 | } |
melangeaddict | 2:9c0a83c5ded5 | 300 | //*************************************************** |
melangeaddict | 2:9c0a83c5ded5 | 301 | void do_nothing() { |
melangeaddict | 2:9c0a83c5ded5 | 302 | return; |
melangeaddict | 2:9c0a83c5ded5 | 303 | } |
melangeaddict | 2:9c0a83c5ded5 | 304 | //*************************************************** |
melangeaddict | 3:c672e782f19b | 305 | void wind_up_start() { |
melangeaddict | 3:c672e782f19b | 306 | wind_up=true; |
melangeaddict | 2:9c0a83c5ded5 | 307 | } |
melangeaddict | 2:9c0a83c5ded5 | 308 | //*************************************************** |
melangeaddict | 3:c672e782f19b | 309 | void wind_up_stop() { |
melangeaddict | 3:c672e782f19b | 310 | wind_up=false; |
melangeaddict | 3:c672e782f19b | 311 | } |
melangeaddict | 3:c672e782f19b | 312 | //*************************************************** |
melangeaddict | 3:c672e782f19b | 313 | void wind_down_start() { |
melangeaddict | 3:c672e782f19b | 314 | wind_down=true; |
melangeaddict | 3:c672e782f19b | 315 | } |
melangeaddict | 3:c672e782f19b | 316 | //*************************************************** |
melangeaddict | 3:c672e782f19b | 317 | void wind_down_stop() { |
melangeaddict | 3:c672e782f19b | 318 | wind_down=false; |
melangeaddict | 2:9c0a83c5ded5 | 319 | } |
melangeaddict | 2:9c0a83c5ded5 | 320 | //*************************************************** |
melangeaddict | 2:9c0a83c5ded5 | 321 | void device_init() { |
melangeaddict | 3:c672e782f19b | 322 | |
melangeaddict | 2:9c0a83c5ded5 | 323 | strings string1(1); |
melangeaddict | 2:9c0a83c5ded5 | 324 | strings string2(2); |
melangeaddict | 2:9c0a83c5ded5 | 325 | strings string3(3); |
melangeaddict | 2:9c0a83c5ded5 | 326 | strings string4(4); |
melangeaddict | 2:9c0a83c5ded5 | 327 | strings string5(5); |
melangeaddict | 2:9c0a83c5ded5 | 328 | strings string6(6); |
melangeaddict | 2:9c0a83c5ded5 | 329 | |
melangeaddict | 3:c672e782f19b | 330 | strings_array[0]=string1; |
melangeaddict | 3:c672e782f19b | 331 | strings_array[1]=string2; |
melangeaddict | 3:c672e782f19b | 332 | strings_array[2]=string3; |
melangeaddict | 3:c672e782f19b | 333 | strings_array[3]=string4; |
melangeaddict | 3:c672e782f19b | 334 | strings_array[4]=string5; |
melangeaddict | 3:c672e782f19b | 335 | strings_array[5]=string6; |
melangeaddict | 3:c672e782f19b | 336 | |
melangeaddict | 4:e370f322a697 | 337 | selected_string=5; |
melangeaddict | 2:9c0a83c5ded5 | 338 | current_mode=tuning_mode; |
melangeaddict | 2:9c0a83c5ded5 | 339 | start_tuning=false; |
melangeaddict | 7:057bcd99a3f8 | 340 | up=1; |
melangeaddict | 7:057bcd99a3f8 | 341 | down=0; |
melangeaddict | 7:057bcd99a3f8 | 342 | wind_up=false; |
melangeaddict | 7:057bcd99a3f8 | 343 | wind_down=false; |
melangeaddict | 7:057bcd99a3f8 | 344 | bias=.5; |
melangeaddict | 2:9c0a83c5ded5 | 345 | |
melangeaddict | 2:9c0a83c5ded5 | 346 | button_init(); |
melangeaddict | 2:9c0a83c5ded5 | 347 | |
melangeaddict | 2:9c0a83c5ded5 | 348 | output_menu(); |
melangeaddict | 3:c672e782f19b | 349 | |
melangeaddict | 2:9c0a83c5ded5 | 350 | } |
melangeaddict | 2:9c0a83c5ded5 | 351 | //*************************************************** |
melangeaddict | 2:9c0a83c5ded5 | 352 | void motor_calibration() { |
melangeaddict | 2:9c0a83c5ded5 | 353 | lcd.cls(); |
melangeaddict | 2:9c0a83c5ded5 | 354 | lcd.printf("Calibrate Motor"); |
melangeaddict | 3:c672e782f19b | 355 | wait(.5); |
melangeaddict | 2:9c0a83c5ded5 | 356 | |
melangeaddict | 2:9c0a83c5ded5 | 357 | float freq=0, freq_up=0, freq_down=0; |
melangeaddict | 2:9c0a83c5ded5 | 358 | bool done=false; |
melangeaddict | 2:9c0a83c5ded5 | 359 | lcd.cls(); |
melangeaddict | 2:9c0a83c5ded5 | 360 | lcd.printf("Please pluck\nstring"); |
melangeaddict | 4:e370f322a697 | 361 | wait(1); |
melangeaddict | 2:9c0a83c5ded5 | 362 | |
melangeaddict | 2:9c0a83c5ded5 | 363 | //motor.motor_turn(up,25)//TODO: Adjust the number of steps here |
melangeaddict | 2:9c0a83c5ded5 | 364 | //On second thought, we don't need to tune up and down for this, we can find the current frequency |
melangeaddict | 2:9c0a83c5ded5 | 365 | //and then turn the peg for the two frequencies! |
melangeaddict | 2:9c0a83c5ded5 | 366 | while (!done) { |
melangeaddict | 2:9c0a83c5ded5 | 367 | freq=guitar.find_frequency(); |
melangeaddict | 2:9c0a83c5ded5 | 368 | |
melangeaddict | 2:9c0a83c5ded5 | 369 | if (check_threshold(freq)) { |
melangeaddict | 3:c672e782f19b | 370 | lcd.cls(); |
melangeaddict | 2:9c0a83c5ded5 | 371 | freq_up=freq; |
melangeaddict | 2:9c0a83c5ded5 | 372 | done=true; |
melangeaddict | 2:9c0a83c5ded5 | 373 | } |
melangeaddict | 4:e370f322a697 | 374 | |
melangeaddict | 4:e370f322a697 | 375 | if (start_tuning==false) |
melangeaddict | 4:e370f322a697 | 376 | break; |
melangeaddict | 2:9c0a83c5ded5 | 377 | } |
melangeaddict | 2:9c0a83c5ded5 | 378 | motor.motor_turn(down,25);//TODO: Adjust the number of steps here |
melangeaddict | 2:9c0a83c5ded5 | 379 | done=false; |
melangeaddict | 2:9c0a83c5ded5 | 380 | while (!done) { |
melangeaddict | 2:9c0a83c5ded5 | 381 | freq=guitar.find_frequency(); |
melangeaddict | 2:9c0a83c5ded5 | 382 | |
melangeaddict | 2:9c0a83c5ded5 | 383 | if (check_threshold(freq)) { |
melangeaddict | 3:c672e782f19b | 384 | lcd.cls(); |
melangeaddict | 2:9c0a83c5ded5 | 385 | freq_down=freq; |
melangeaddict | 2:9c0a83c5ded5 | 386 | done=true; |
melangeaddict | 2:9c0a83c5ded5 | 387 | } |
melangeaddict | 4:e370f322a697 | 388 | if (start_tuning==false) |
melangeaddict | 4:e370f322a697 | 389 | break; |
melangeaddict | 2:9c0a83c5ded5 | 390 | } |
melangeaddict | 2:9c0a83c5ded5 | 391 | |
melangeaddict | 2:9c0a83c5ded5 | 392 | if (freq_up<freq_down) { |
melangeaddict | 7:057bcd99a3f8 | 393 | down=1; |
melangeaddict | 7:057bcd99a3f8 | 394 | up=0; |
melangeaddict | 2:9c0a83c5ded5 | 395 | } |
melangeaddict | 2:9c0a83c5ded5 | 396 | |
melangeaddict | 2:9c0a83c5ded5 | 397 | lcd.cls(); |
melangeaddict | 2:9c0a83c5ded5 | 398 | lcd.printf("Calibration Done"); |
melangeaddict | 4:e370f322a697 | 399 | |
melangeaddict | 4:e370f322a697 | 400 | if (start_tuning==false) { |
melangeaddict | 4:e370f322a697 | 401 | output_menu(); |
melangeaddict | 4:e370f322a697 | 402 | setup_buttons(); |
melangeaddict | 4:e370f322a697 | 403 | } |
melangeaddict | 2:9c0a83c5ded5 | 404 | } |
melangeaddict | 2:9c0a83c5ded5 | 405 | //********************************************** |
melangeaddict | 2:9c0a83c5ded5 | 406 | bool check_threshold(float freq) { |
melangeaddict | 2:9c0a83c5ded5 | 407 | if (freq>500) { |
melangeaddict | 2:9c0a83c5ded5 | 408 | lcd.cls(); |
melangeaddict | 2:9c0a83c5ded5 | 409 | lcd.printf("Pluck string \nagain"); |
melangeaddict | 2:9c0a83c5ded5 | 410 | return false; |
melangeaddict | 2:9c0a83c5ded5 | 411 | } else |
melangeaddict | 2:9c0a83c5ded5 | 412 | return true; |
melangeaddict | 2:9c0a83c5ded5 | 413 | } |
melangeaddict | 6:46fb12f6ace1 | 414 | |
melangeaddict | 6:46fb12f6ace1 | 415 | |
melangeaddict | 6:46fb12f6ace1 | 416 | |
melangeaddict | 6:46fb12f6ace1 | 417 | void LED_initialize() { |
melangeaddict | 6:46fb12f6ace1 | 418 | float wait_time=0.3; |
melangeaddict | 6:46fb12f6ace1 | 419 | ledBlue=1;//red |
melangeaddict | 6:46fb12f6ace1 | 420 | ledGreen=1; |
melangeaddict | 6:46fb12f6ace1 | 421 | ledRed=0; |
melangeaddict | 6:46fb12f6ace1 | 422 | wait(wait_time); |
melangeaddict | 6:46fb12f6ace1 | 423 | ledRed=1;//green |
melangeaddict | 6:46fb12f6ace1 | 424 | ledGreen=0; |
melangeaddict | 6:46fb12f6ace1 | 425 | wait(wait_time); |
melangeaddict | 6:46fb12f6ace1 | 426 | ledGreen=1; |
melangeaddict | 6:46fb12f6ace1 | 427 | ledBlue=0;//blue |
melangeaddict | 6:46fb12f6ace1 | 428 | wait(wait_time); |
melangeaddict | 6:46fb12f6ace1 | 429 | |
melangeaddict | 6:46fb12f6ace1 | 430 | for (int a=0; a<5; a++) { |
melangeaddict | 6:46fb12f6ace1 | 431 | ledRed=0;//all |
melangeaddict | 6:46fb12f6ace1 | 432 | ledGreen=0; |
melangeaddict | 6:46fb12f6ace1 | 433 | ledBlue=0; |
melangeaddict | 6:46fb12f6ace1 | 434 | wait(wait_time); |
melangeaddict | 6:46fb12f6ace1 | 435 | ledRed=1; |
melangeaddict | 6:46fb12f6ace1 | 436 | ledGreen=1; |
melangeaddict | 6:46fb12f6ace1 | 437 | ledBlue=1; |
melangeaddict | 6:46fb12f6ace1 | 438 | wait(wait_time); |
melangeaddict | 6:46fb12f6ace1 | 439 | |
melangeaddict | 6:46fb12f6ace1 | 440 | } |
melangeaddict | 6:46fb12f6ace1 | 441 | ledBlue=0; |
melangeaddict | 6:46fb12f6ace1 | 442 | |
melangeaddict | 6:46fb12f6ace1 | 443 | |
melangeaddict | 6:46fb12f6ace1 | 444 | ledRed=1;//blue |
melangeaddict | 6:46fb12f6ace1 | 445 | ledGreen=1; |
melangeaddict | 6:46fb12f6ace1 | 446 | |
melangeaddict | 6:46fb12f6ace1 | 447 | return; |
melangeaddict | 6:46fb12f6ace1 | 448 | |
melangeaddict | 6:46fb12f6ace1 | 449 | } |
melangeaddict | 6:46fb12f6ace1 | 450 | |
melangeaddict | 6:46fb12f6ace1 | 451 | |
melangeaddict | 6:46fb12f6ace1 | 452 | void set_LED(int led, int value) { |
melangeaddict | 6:46fb12f6ace1 | 453 | //ledBlue=1; |
melangeaddict | 6:46fb12f6ace1 | 454 | //ledRed=2; |
melangeaddict | 6:46fb12f6ace1 | 455 | //ledGreen=3; |
melangeaddict | 6:46fb12f6ace1 | 456 | //value must be an int: 0 or 1 |
melangeaddict | 7:057bcd99a3f8 | 457 | if (value!=0) |
melangeaddict | 6:46fb12f6ace1 | 458 | |
melangeaddict | 7:057bcd99a3f8 | 459 | switch (led) { |
melangeaddict | 7:057bcd99a3f8 | 460 | case(1): |
melangeaddict | 7:057bcd99a3f8 | 461 | ledBlue=value; |
melangeaddict | 7:057bcd99a3f8 | 462 | break; |
melangeaddict | 6:46fb12f6ace1 | 463 | |
melangeaddict | 7:057bcd99a3f8 | 464 | case(2): |
melangeaddict | 7:057bcd99a3f8 | 465 | ledRed=value; |
melangeaddict | 7:057bcd99a3f8 | 466 | break; |
melangeaddict | 6:46fb12f6ace1 | 467 | |
melangeaddict | 7:057bcd99a3f8 | 468 | case(3): |
melangeaddict | 7:057bcd99a3f8 | 469 | ledGreen=value; |
melangeaddict | 7:057bcd99a3f8 | 470 | break; |
melangeaddict | 6:46fb12f6ace1 | 471 | |
melangeaddict | 7:057bcd99a3f8 | 472 | default: |
melangeaddict | 7:057bcd99a3f8 | 473 | break; |
melangeaddict | 6:46fb12f6ace1 | 474 | |
melangeaddict | 7:057bcd99a3f8 | 475 | } |
melangeaddict | 6:46fb12f6ace1 | 476 | |
melangeaddict | 6:46fb12f6ace1 | 477 | return; |
melangeaddict | 6:46fb12f6ace1 | 478 | |
melangeaddict | 6:46fb12f6ace1 | 479 | } |