A program to automatically tune a guitar. Written by Justin Reidhead and Steven Swenson
Dependencies: FFT FrequencyFinder Motor NewTextLCD PinDetect mbed strings
main.cpp@11:bdad1acccdad, 2012-04-25 (annotated)
- Committer:
- melangeaddict
- Date:
- Wed Apr 25 00:42:46 2012 +0000
- Revision:
- 11:bdad1acccdad
- Parent:
- 9:2a211133e79a
- Child:
- 12:03c189de6e2e
messed with gains of the strings;
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 | 8:651fbf5ae98a | 25 | DigitalOut ledBlue(p27); |
melangeaddict | 8:651fbf5ae98a | 26 | DigitalOut ledGreen(p28); |
melangeaddict | 8:651fbf5ae98a | 27 | DigitalOut ledRed(p29); |
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 | 9:2a211133e79a | 40 | |
melangeaddict | 9:2a211133e79a | 41 | LocalFileSystem local("local"); |
melangeaddict | 2:9c0a83c5ded5 | 42 | //*************************************************** |
melangeaddict | 2:9c0a83c5ded5 | 43 | //*****************constants************************* |
melangeaddict | 3:c672e782f19b | 44 | const bool tuning_mode=false;//For setting the mode to tuning or winding |
melangeaddict | 2:9c0a83c5ded5 | 45 | const bool winding_mode=true; |
melangeaddict | 2:9c0a83c5ded5 | 46 | |
melangeaddict | 2:9c0a83c5ded5 | 47 | //*************************************************** |
melangeaddict | 2:9c0a83c5ded5 | 48 | //******************prototypes*********************** |
melangeaddict | 2:9c0a83c5ded5 | 49 | void device_init(); |
melangeaddict | 2:9c0a83c5ded5 | 50 | |
melangeaddict | 9:2a211133e79a | 51 | void string_sel();//string select |
melangeaddict | 9:2a211133e79a | 52 | void pitch_sel();//pitch select |
melangeaddict | 9:2a211133e79a | 53 | void start();//start tuning |
melangeaddict | 9:2a211133e79a | 54 | void stop();//stop tuning |
melangeaddict | 9:2a211133e79a | 55 | void mode();//change mode |
melangeaddict | 9:2a211133e79a | 56 | void do_nothing();//does nothing |
melangeaddict | 9:2a211133e79a | 57 | void wind_up_start();//start motor winding up |
melangeaddict | 9:2a211133e79a | 58 | void wind_down_start();//start motor winding down |
melangeaddict | 9:2a211133e79a | 59 | void wind_up_stop();//stop motor winding up |
melangeaddict | 9:2a211133e79a | 60 | void wind_down_stop();//stop motor winding down |
melangeaddict | 2:9c0a83c5ded5 | 61 | |
melangeaddict | 9:2a211133e79a | 62 | void button_init();//Set sampling period etc... for buttons |
melangeaddict | 9:2a211133e79a | 63 | void setup_buttons();//set function calls for buttons |
melangeaddict | 9:2a211133e79a | 64 | void output_menu();//Output the main menu |
melangeaddict | 9:2a211133e79a | 65 | void motor_calibration();//Calibrate the motor's direction |
melangeaddict | 9:2a211133e79a | 66 | bool check_threshold(float);//check to make sure the frequency is valid (frequency to check) |
melangeaddict | 8:651fbf5ae98a | 67 | void LED_initialize();//Initialization sequence for LEDs, called at start-up |
melangeaddict | 8:651fbf5ae98a | 68 | void set_LED(int led, int value);//sets or clears an LED: Blue=1(in tune), Red=2(flat), Green=3(sharp). Value=1(off), 0(on); |
melangeaddict | 2:9c0a83c5ded5 | 69 | //************************************************* |
melangeaddict | 2:9c0a83c5ded5 | 70 | //*********************main************************ |
melangeaddict | 2:9c0a83c5ded5 | 71 | int main() { |
melangeaddict | 3:c672e782f19b | 72 | lcd.cls();//Clear the LCD |
melangeaddict | 3:c672e782f19b | 73 | lcd.printf("Perfect\n Pitch");//Modify to whatever we want to name this thing |
melangeaddict | 9:2a211133e79a | 74 | LED_initialize(); |
melangeaddict | 11:bdad1acccdad | 75 | // wait(.5); |
melangeaddict | 2:9c0a83c5ded5 | 76 | |
melangeaddict | 3:c672e782f19b | 77 | device_init();//Setup buttons and set global variables |
melangeaddict | 3:c672e782f19b | 78 | |
melangeaddict | 11:bdad1acccdad | 79 | |
melangeaddict | 8:651fbf5ae98a | 80 | |
melangeaddict | 2:9c0a83c5ded5 | 81 | int state=0,next_state=0; |
melangeaddict | 11:bdad1acccdad | 82 | //int num_steps=0; |
melangeaddict | 11:bdad1acccdad | 83 | //float old_freq=0; |
melangeaddict | 11:bdad1acccdad | 84 | //int found_frequency=0; |
melangeaddict | 2:9c0a83c5ded5 | 85 | float new_freq=0; |
melangeaddict | 2:9c0a83c5ded5 | 86 | float desired_freq=0; |
melangeaddict | 2:9c0a83c5ded5 | 87 | strings *temp=&strings_array[0]; |
melangeaddict | 7:057bcd99a3f8 | 88 | wait(.5); |
melangeaddict | 3:c672e782f19b | 89 | |
melangeaddict | 3:c672e782f19b | 90 | output_menu(); |
melangeaddict | 3:c672e782f19b | 91 | |
melangeaddict | 2:9c0a83c5ded5 | 92 | while (1) { |
melangeaddict | 2:9c0a83c5ded5 | 93 | state=next_state; |
melangeaddict | 2:9c0a83c5ded5 | 94 | |
melangeaddict | 2:9c0a83c5ded5 | 95 | switch (state) { |
melangeaddict | 2:9c0a83c5ded5 | 96 | //------------------------------------ |
melangeaddict | 2:9c0a83c5ded5 | 97 | case 0://Stay here till the user selects the string and pitch |
melangeaddict | 2:9c0a83c5ded5 | 98 | if (start_tuning==true) { |
melangeaddict | 2:9c0a83c5ded5 | 99 | next_state=1; |
melangeaddict | 2:9c0a83c5ded5 | 100 | } else { |
melangeaddict | 2:9c0a83c5ded5 | 101 | next_state=0; |
melangeaddict | 3:c672e782f19b | 102 | //output_menu(); |
melangeaddict | 2:9c0a83c5ded5 | 103 | } |
melangeaddict | 2:9c0a83c5ded5 | 104 | break; |
melangeaddict | 2:9c0a83c5ded5 | 105 | //---------------------------------------- |
melangeaddict | 2:9c0a83c5ded5 | 106 | case 1://motor calibration state |
melangeaddict | 9:2a211133e79a | 107 | //motor_calibration();//determine which direction is up and down for the motor |
melangeaddict | 2:9c0a83c5ded5 | 108 | next_state=2; |
melangeaddict | 2:9c0a83c5ded5 | 109 | break; |
melangeaddict | 2:9c0a83c5ded5 | 110 | //----------------------------------------- |
melangeaddict | 2:9c0a83c5ded5 | 111 | case 2://begin the actual tuning |
melangeaddict | 2:9c0a83c5ded5 | 112 | temp=&strings_array[selected_string]; |
melangeaddict | 3:c672e782f19b | 113 | desired_freq=temp->get_freq();//Get the desired frequency for the string selected |
melangeaddict | 11:bdad1acccdad | 114 | //old_freq=desired_freq;//We have to initalize it to something... |
melangeaddict | 2:9c0a83c5ded5 | 115 | |
melangeaddict | 2:9c0a83c5ded5 | 116 | next_state=3; |
melangeaddict | 2:9c0a83c5ded5 | 117 | break; |
melangeaddict | 2:9c0a83c5ded5 | 118 | //----------------------------------------- |
melangeaddict | 3:c672e782f19b | 119 | case 3://Do the dirty work of tuning |
melangeaddict | 3:c672e782f19b | 120 | new_freq=guitar.find_frequency();//Get the current frequency of the string |
melangeaddict | 9:2a211133e79a | 121 | |
melangeaddict | 9:2a211133e79a | 122 | //if (new_freq>(desired_freq+50) || new_freq<(desired_freq-50)) {//If the new frequency is wildly out of whack |
melangeaddict | 9:2a211133e79a | 123 | //new_freq=guitar.find_frequency();//Get the current frequency of the string, again |
melangeaddict | 9:2a211133e79a | 124 | // break; |
melangeaddict | 9:2a211133e79a | 125 | //} |
melangeaddict | 9:2a211133e79a | 126 | //num_steps=-5*abs(new_freq-old_freq); |
melangeaddict | 9:2a211133e79a | 127 | // old_freq=new_freq; |
melangeaddict | 9:2a211133e79a | 128 | |
melangeaddict | 3:c672e782f19b | 129 | if (check_threshold(new_freq)) {//The check_threshold function makes sure the frequency is valid (less than 500 Hz) |
melangeaddict | 2:9c0a83c5ded5 | 130 | if ((desired_freq-.5)<new_freq && (desired_freq+.5)>new_freq) {//We are within .5Hz of the desired frequency |
melangeaddict | 11:bdad1acccdad | 131 | |
melangeaddict | 11:bdad1acccdad | 132 | //if (found_frequency==0) { |
melangeaddict | 11:bdad1acccdad | 133 | // found_frequency++; |
melangeaddict | 11:bdad1acccdad | 134 | // next_state=3; |
melangeaddict | 11:bdad1acccdad | 135 | // break; |
melangeaddict | 11:bdad1acccdad | 136 | //} |
melangeaddict | 11:bdad1acccdad | 137 | |
melangeaddict | 2:9c0a83c5ded5 | 138 | lcd.cls(); |
melangeaddict | 5:c0fd99f07536 | 139 | lcd.printf("String %d\ntuned",selected_string+1); |
melangeaddict | 8:651fbf5ae98a | 140 | set_LED(1,0);//blue on |
melangeaddict | 8:651fbf5ae98a | 141 | set_LED(2,1);//red off; |
melangeaddict | 8:651fbf5ae98a | 142 | set_LED(3,1);//green off |
melangeaddict | 2:9c0a83c5ded5 | 143 | wait(.5); |
melangeaddict | 2:9c0a83c5ded5 | 144 | |
melangeaddict | 2:9c0a83c5ded5 | 145 | start_tuning=false; |
melangeaddict | 5:c0fd99f07536 | 146 | output_menu(); |
melangeaddict | 5:c0fd99f07536 | 147 | setup_buttons(); |
melangeaddict | 2:9c0a83c5ded5 | 148 | next_state=0; |
melangeaddict | 3:c672e782f19b | 149 | } else if ((desired_freq-.5)>new_freq) {//We are too low, and need to turn the string tigher |
melangeaddict | 8:651fbf5ae98a | 150 | lcd.cls(); |
melangeaddict | 8:651fbf5ae98a | 151 | lcd.printf("Tuning up"); |
melangeaddict | 9:2a211133e79a | 152 | |
melangeaddict | 8:651fbf5ae98a | 153 | set_LED(1,1);//blue off |
melangeaddict | 8:651fbf5ae98a | 154 | set_LED(2,0);//red on; |
melangeaddict | 8:651fbf5ae98a | 155 | set_LED(3,1);//green off |
melangeaddict | 9:2a211133e79a | 156 | |
melangeaddict | 11:bdad1acccdad | 157 | //found_frequency=0; |
melangeaddict | 9:2a211133e79a | 158 | |
melangeaddict | 8:651fbf5ae98a | 159 | wait(.5); |
melangeaddict | 8:651fbf5ae98a | 160 | |
melangeaddict | 5:c0fd99f07536 | 161 | motor.motor_turn(up,10);//TODO:Adjust # of steps |
melangeaddict | 2:9c0a83c5ded5 | 162 | next_state=3; |
melangeaddict | 8:651fbf5ae98a | 163 | |
melangeaddict | 3:c672e782f19b | 164 | } else {//We are too high, and need to loosen the string |
melangeaddict | 8:651fbf5ae98a | 165 | lcd.cls(); |
melangeaddict | 8:651fbf5ae98a | 166 | lcd.printf("Tuning down"); |
melangeaddict | 8:651fbf5ae98a | 167 | set_LED(1,1);//blue off |
melangeaddict | 8:651fbf5ae98a | 168 | set_LED(2,1);//red off; |
melangeaddict | 8:651fbf5ae98a | 169 | set_LED(3,0);//green on |
melangeaddict | 11:bdad1acccdad | 170 | |
melangeaddict | 11:bdad1acccdad | 171 | //found_frequency=0; |
melangeaddict | 11:bdad1acccdad | 172 | |
melangeaddict | 8:651fbf5ae98a | 173 | wait(.5); |
melangeaddict | 8:651fbf5ae98a | 174 | |
melangeaddict | 5:c0fd99f07536 | 175 | motor.motor_turn(down,10); |
melangeaddict | 2:9c0a83c5ded5 | 176 | next_state=3; |
melangeaddict | 2:9c0a83c5ded5 | 177 | } |
melangeaddict | 2:9c0a83c5ded5 | 178 | } else { |
melangeaddict | 2:9c0a83c5ded5 | 179 | next_state=3; |
melangeaddict | 2:9c0a83c5ded5 | 180 | } |
melangeaddict | 2:9c0a83c5ded5 | 181 | |
melangeaddict | 8:651fbf5ae98a | 182 | if (start_tuning==false) {//If the stop button is pressed, the state machine returns to user input |
melangeaddict | 8:651fbf5ae98a | 183 | next_state=0; |
melangeaddict | 8:651fbf5ae98a | 184 | setup_buttons(); |
melangeaddict | 8:651fbf5ae98a | 185 | output_menu(); |
melangeaddict | 8:651fbf5ae98a | 186 | } |
melangeaddict | 8:651fbf5ae98a | 187 | |
melangeaddict | 2:9c0a83c5ded5 | 188 | //TODO:Determine number of steps per frequency change |
melangeaddict | 9:2a211133e79a | 189 | |
melangeaddict | 2:9c0a83c5ded5 | 190 | break; |
melangeaddict | 2:9c0a83c5ded5 | 191 | //----------------------------------------- |
melangeaddict | 3:c672e782f19b | 192 | case 4://Winding mode |
melangeaddict | 3:c672e782f19b | 193 | if (current_mode==winding_mode) { |
melangeaddict | 3:c672e782f19b | 194 | if (wind_up) { |
melangeaddict | 3:c672e782f19b | 195 | motor.motor_turn(up,5);//TODO:Adjust number of turns |
melangeaddict | 3:c672e782f19b | 196 | } |
melangeaddict | 3:c672e782f19b | 197 | |
melangeaddict | 3:c672e782f19b | 198 | if (wind_down) { |
melangeaddict | 3:c672e782f19b | 199 | motor.motor_turn(down,5);//TODO:Adjust number of turns |
melangeaddict | 3:c672e782f19b | 200 | } |
melangeaddict | 3:c672e782f19b | 201 | next_state=4; |
melangeaddict | 3:c672e782f19b | 202 | } else { |
melangeaddict | 7:057bcd99a3f8 | 203 | output_menu(); |
melangeaddict | 7:057bcd99a3f8 | 204 | wind_up=false; |
melangeaddict | 7:057bcd99a3f8 | 205 | wind_down=false; |
melangeaddict | 7:057bcd99a3f8 | 206 | setup_buttons(); |
melangeaddict | 11:bdad1acccdad | 207 | //set_LED(1,0); |
melangeaddict | 3:c672e782f19b | 208 | next_state=0; |
melangeaddict | 3:c672e782f19b | 209 | } |
melangeaddict | 3:c672e782f19b | 210 | break; |
melangeaddict | 2:9c0a83c5ded5 | 211 | default: |
melangeaddict | 2:9c0a83c5ded5 | 212 | break; |
melangeaddict | 3:c672e782f19b | 213 | }//end switch |
melangeaddict | 2:9c0a83c5ded5 | 214 | wait_ms(5); |
melangeaddict | 2:9c0a83c5ded5 | 215 | |
melangeaddict | 2:9c0a83c5ded5 | 216 | if (start_tuning==false) {//If the stop button is pressed, the state machine returns to user input |
melangeaddict | 2:9c0a83c5ded5 | 217 | next_state=0; |
melangeaddict | 4:e370f322a697 | 218 | //setup_buttons(); |
melangeaddict | 4:e370f322a697 | 219 | //output_menu(); |
melangeaddict | 2:9c0a83c5ded5 | 220 | } |
melangeaddict | 3:c672e782f19b | 221 | |
melangeaddict | 3:c672e782f19b | 222 | if (current_mode==winding_mode) {//TODO:Debug this |
melangeaddict | 3:c672e782f19b | 223 | next_state=4; |
melangeaddict | 3:c672e782f19b | 224 | } |
melangeaddict | 3:c672e782f19b | 225 | |
melangeaddict | 3:c672e782f19b | 226 | if (start_tuning) { |
melangeaddict | 3:c672e782f19b | 227 | lcd.cls(); |
melangeaddict | 7:057bcd99a3f8 | 228 | |
melangeaddict | 7:057bcd99a3f8 | 229 | lcd.locate(9,0); |
melangeaddict | 7:057bcd99a3f8 | 230 | lcd.printf("%f",new_freq); |
melangeaddict | 7:057bcd99a3f8 | 231 | lcd.locate(9,1); |
melangeaddict | 7:057bcd99a3f8 | 232 | lcd.printf("%f",desired_freq); |
melangeaddict | 7:057bcd99a3f8 | 233 | |
melangeaddict | 5:c0fd99f07536 | 234 | lcd.locate(0,1); |
melangeaddict | 7:057bcd99a3f8 | 235 | lcd.printf("Desired"); |
melangeaddict | 7:057bcd99a3f8 | 236 | lcd.locate(0,0); |
melangeaddict | 7:057bcd99a3f8 | 237 | lcd.printf("Detected"); |
melangeaddict | 3:c672e782f19b | 238 | } |
melangeaddict | 3:c672e782f19b | 239 | |
melangeaddict | 3:c672e782f19b | 240 | }//end while |
melangeaddict | 2:9c0a83c5ded5 | 241 | |
melangeaddict | 2:9c0a83c5ded5 | 242 | // return 0; |
melangeaddict | 2:9c0a83c5ded5 | 243 | } |
melangeaddict | 2:9c0a83c5ded5 | 244 | |
melangeaddict | 2:9c0a83c5ded5 | 245 | //*************************************************** |
melangeaddict | 2:9c0a83c5ded5 | 246 | //******************functions************************ |
melangeaddict | 3:c672e782f19b | 247 | //Display the string and pitch selection menu |
melangeaddict | 2:9c0a83c5ded5 | 248 | void output_menu() { |
melangeaddict | 2:9c0a83c5ded5 | 249 | lcd.cls(); |
melangeaddict | 4:e370f322a697 | 250 | lcd.printf("Select String: %d",selected_string+1); |
melangeaddict | 4:e370f322a697 | 251 | |
melangeaddict | 3:c672e782f19b | 252 | lcd.locate(0,1); |
melangeaddict | 3:c672e782f19b | 253 | strings *temp=&strings_array[selected_string]; |
melangeaddict | 3:c672e782f19b | 254 | lcd.printf("Select Pitch: "); |
melangeaddict | 5:c0fd99f07536 | 255 | lcd.printf("%s",temp->get_note()); |
melangeaddict | 5:c0fd99f07536 | 256 | //wait(.5); |
melangeaddict | 11:bdad1acccdad | 257 | |
melangeaddict | 11:bdad1acccdad | 258 | set_LED(1,0);//Turn blue LED on |
melangeaddict | 11:bdad1acccdad | 259 | set_LED(2,1); |
melangeaddict | 11:bdad1acccdad | 260 | set_LED(3,1); |
melangeaddict | 2:9c0a83c5ded5 | 261 | } |
melangeaddict | 2:9c0a83c5ded5 | 262 | |
melangeaddict | 2:9c0a83c5ded5 | 263 | //*************************************************** |
melangeaddict | 3:c672e782f19b | 264 | //Initialize the buttons |
melangeaddict | 2:9c0a83c5ded5 | 265 | void button_init() { |
melangeaddict | 2:9c0a83c5ded5 | 266 | string_but.mode( PullDown ); |
melangeaddict | 2:9c0a83c5ded5 | 267 | string_but.setSampleFrequency(); |
melangeaddict | 2:9c0a83c5ded5 | 268 | |
melangeaddict | 2:9c0a83c5ded5 | 269 | pitch_but.mode( PullDown ); |
melangeaddict | 2:9c0a83c5ded5 | 270 | pitch_but.setSampleFrequency(); |
melangeaddict | 2:9c0a83c5ded5 | 271 | |
melangeaddict | 2:9c0a83c5ded5 | 272 | start_but.mode( PullDown ); |
melangeaddict | 2:9c0a83c5ded5 | 273 | start_but.setSampleFrequency(); |
melangeaddict | 2:9c0a83c5ded5 | 274 | |
melangeaddict | 2:9c0a83c5ded5 | 275 | mode_but.mode( PullDown ); |
melangeaddict | 2:9c0a83c5ded5 | 276 | mode_but.setSampleFrequency(); |
melangeaddict | 4:e370f322a697 | 277 | |
melangeaddict | 4:e370f322a697 | 278 | setup_buttons(); |
melangeaddict | 2:9c0a83c5ded5 | 279 | } |
melangeaddict | 2:9c0a83c5ded5 | 280 | |
melangeaddict | 2:9c0a83c5ded5 | 281 | //*************************************************** |
melangeaddict | 3:c672e782f19b | 282 | //Depending on the current mode, the buttons do different things |
melangeaddict | 2:9c0a83c5ded5 | 283 | void setup_buttons() { |
melangeaddict | 3:c672e782f19b | 284 | if (current_mode==tuning_mode) {//Tuning mode |
melangeaddict | 2:9c0a83c5ded5 | 285 | string_but.attach_asserted(&string_sel); |
melangeaddict | 3:c672e782f19b | 286 | string_but.attach_deasserted(&do_nothing); |
melangeaddict | 2:9c0a83c5ded5 | 287 | pitch_but.attach_asserted(&pitch_sel); |
melangeaddict | 3:c672e782f19b | 288 | pitch_but.attach_deasserted(&do_nothing); |
melangeaddict | 2:9c0a83c5ded5 | 289 | start_but.attach_asserted(&start); |
melangeaddict | 2:9c0a83c5ded5 | 290 | mode_but.attach_asserted(&mode); |
melangeaddict | 3:c672e782f19b | 291 | } else {//Winding mode |
melangeaddict | 3:c672e782f19b | 292 | string_but.attach_asserted(&wind_up_start); |
melangeaddict | 3:c672e782f19b | 293 | string_but.attach_deasserted(&wind_up_stop); |
melangeaddict | 3:c672e782f19b | 294 | pitch_but.attach_asserted(&wind_down_start); |
melangeaddict | 3:c672e782f19b | 295 | pitch_but.attach_deasserted(&wind_down_stop); |
melangeaddict | 2:9c0a83c5ded5 | 296 | start_but.attach_asserted(&do_nothing); |
melangeaddict | 2:9c0a83c5ded5 | 297 | mode_but.attach_asserted(&mode); |
melangeaddict | 2:9c0a83c5ded5 | 298 | } |
melangeaddict | 2:9c0a83c5ded5 | 299 | } |
melangeaddict | 2:9c0a83c5ded5 | 300 | //*************************************************** |
melangeaddict | 3:c672e782f19b | 301 | //Change the selected string - there are only six strings |
melangeaddict | 2:9c0a83c5ded5 | 302 | void string_sel() { |
melangeaddict | 8:651fbf5ae98a | 303 | selected_string--; |
melangeaddict | 8:651fbf5ae98a | 304 | if (selected_string<0) |
melangeaddict | 8:651fbf5ae98a | 305 | selected_string=5; |
melangeaddict | 2:9c0a83c5ded5 | 306 | |
melangeaddict | 5:c0fd99f07536 | 307 | strings *temp=&strings_array[selected_string]; |
melangeaddict | 5:c0fd99f07536 | 308 | temp->reset_index(); |
melangeaddict | 5:c0fd99f07536 | 309 | |
melangeaddict | 2:9c0a83c5ded5 | 310 | output_menu(); |
melangeaddict | 2:9c0a83c5ded5 | 311 | } |
melangeaddict | 2:9c0a83c5ded5 | 312 | //*************************************************** |
melangeaddict | 3:c672e782f19b | 313 | //Change the pitch of the selected string |
melangeaddict | 2:9c0a83c5ded5 | 314 | void pitch_sel() { |
melangeaddict | 2:9c0a83c5ded5 | 315 | strings *temp=&strings_array[selected_string]; |
melangeaddict | 2:9c0a83c5ded5 | 316 | temp->inc_index(); |
melangeaddict | 2:9c0a83c5ded5 | 317 | |
melangeaddict | 2:9c0a83c5ded5 | 318 | output_menu(); |
melangeaddict | 2:9c0a83c5ded5 | 319 | } |
melangeaddict | 2:9c0a83c5ded5 | 320 | //*************************************************** |
melangeaddict | 3:c672e782f19b | 321 | //Start the tuning process |
melangeaddict | 2:9c0a83c5ded5 | 322 | void start() { |
melangeaddict | 2:9c0a83c5ded5 | 323 | start_tuning=true; |
melangeaddict | 3:c672e782f19b | 324 | |
melangeaddict | 3:c672e782f19b | 325 | string_but.attach_asserted(&do_nothing);//Disable the other buttons |
melangeaddict | 5:c0fd99f07536 | 326 | string_but.attach_deasserted(&do_nothing); |
melangeaddict | 3:c672e782f19b | 327 | pitch_but.attach_asserted(&do_nothing); |
melangeaddict | 5:c0fd99f07536 | 328 | pitch_but.attach_deasserted(&do_nothing); |
melangeaddict | 3:c672e782f19b | 329 | start_but.attach_asserted(&stop); //except for the start/stop button |
melangeaddict | 3:c672e782f19b | 330 | mode_but.attach_asserted(&do_nothing); |
melangeaddict | 2:9c0a83c5ded5 | 331 | } |
melangeaddict | 2:9c0a83c5ded5 | 332 | //*************************************************** |
melangeaddict | 2:9c0a83c5ded5 | 333 | void mode() { |
melangeaddict | 2:9c0a83c5ded5 | 334 | if (current_mode==tuning_mode) { |
melangeaddict | 2:9c0a83c5ded5 | 335 | current_mode=winding_mode; |
melangeaddict | 2:9c0a83c5ded5 | 336 | lcd.cls(); |
melangeaddict | 2:9c0a83c5ded5 | 337 | lcd.printf("Winding Mode"); |
melangeaddict | 4:e370f322a697 | 338 | wait(1); |
melangeaddict | 4:e370f322a697 | 339 | lcd.cls(); |
melangeaddict | 5:c0fd99f07536 | 340 | lcd.printf("String for up\nPitch for down"); |
melangeaddict | 2:9c0a83c5ded5 | 341 | } else { |
melangeaddict | 2:9c0a83c5ded5 | 342 | current_mode=tuning_mode; |
melangeaddict | 2:9c0a83c5ded5 | 343 | lcd.cls(); |
melangeaddict | 2:9c0a83c5ded5 | 344 | lcd.printf("Tuning Mode"); |
melangeaddict | 3:c672e782f19b | 345 | wait(.5); |
melangeaddict | 4:e370f322a697 | 346 | output_menu(); |
melangeaddict | 2:9c0a83c5ded5 | 347 | } |
melangeaddict | 3:c672e782f19b | 348 | setup_buttons();//Change the functions the buttons connect to |
melangeaddict | 2:9c0a83c5ded5 | 349 | } |
melangeaddict | 2:9c0a83c5ded5 | 350 | //*************************************************** |
melangeaddict | 2:9c0a83c5ded5 | 351 | void stop() { |
melangeaddict | 2:9c0a83c5ded5 | 352 | start_tuning=false; |
melangeaddict | 7:057bcd99a3f8 | 353 | |
melangeaddict | 7:057bcd99a3f8 | 354 | output_menu(); |
melangeaddict | 7:057bcd99a3f8 | 355 | setup_buttons(); |
melangeaddict | 2:9c0a83c5ded5 | 356 | } |
melangeaddict | 2:9c0a83c5ded5 | 357 | //*************************************************** |
melangeaddict | 2:9c0a83c5ded5 | 358 | void do_nothing() { |
melangeaddict | 2:9c0a83c5ded5 | 359 | return; |
melangeaddict | 2:9c0a83c5ded5 | 360 | } |
melangeaddict | 2:9c0a83c5ded5 | 361 | //*************************************************** |
melangeaddict | 3:c672e782f19b | 362 | void wind_up_start() { |
melangeaddict | 3:c672e782f19b | 363 | wind_up=true; |
melangeaddict | 2:9c0a83c5ded5 | 364 | } |
melangeaddict | 2:9c0a83c5ded5 | 365 | //*************************************************** |
melangeaddict | 3:c672e782f19b | 366 | void wind_up_stop() { |
melangeaddict | 3:c672e782f19b | 367 | wind_up=false; |
melangeaddict | 3:c672e782f19b | 368 | } |
melangeaddict | 3:c672e782f19b | 369 | //*************************************************** |
melangeaddict | 3:c672e782f19b | 370 | void wind_down_start() { |
melangeaddict | 3:c672e782f19b | 371 | wind_down=true; |
melangeaddict | 3:c672e782f19b | 372 | } |
melangeaddict | 3:c672e782f19b | 373 | //*************************************************** |
melangeaddict | 3:c672e782f19b | 374 | void wind_down_stop() { |
melangeaddict | 3:c672e782f19b | 375 | wind_down=false; |
melangeaddict | 2:9c0a83c5ded5 | 376 | } |
melangeaddict | 2:9c0a83c5ded5 | 377 | //*************************************************** |
melangeaddict | 2:9c0a83c5ded5 | 378 | void device_init() { |
melangeaddict | 3:c672e782f19b | 379 | |
melangeaddict | 2:9c0a83c5ded5 | 380 | strings string1(1); |
melangeaddict | 2:9c0a83c5ded5 | 381 | strings string2(2); |
melangeaddict | 2:9c0a83c5ded5 | 382 | strings string3(3); |
melangeaddict | 2:9c0a83c5ded5 | 383 | strings string4(4); |
melangeaddict | 2:9c0a83c5ded5 | 384 | strings string5(5); |
melangeaddict | 2:9c0a83c5ded5 | 385 | strings string6(6); |
melangeaddict | 2:9c0a83c5ded5 | 386 | |
melangeaddict | 3:c672e782f19b | 387 | strings_array[0]=string1; |
melangeaddict | 3:c672e782f19b | 388 | strings_array[1]=string2; |
melangeaddict | 3:c672e782f19b | 389 | strings_array[2]=string3; |
melangeaddict | 3:c672e782f19b | 390 | strings_array[3]=string4; |
melangeaddict | 3:c672e782f19b | 391 | strings_array[4]=string5; |
melangeaddict | 3:c672e782f19b | 392 | strings_array[5]=string6; |
melangeaddict | 3:c672e782f19b | 393 | |
melangeaddict | 4:e370f322a697 | 394 | selected_string=5; |
melangeaddict | 2:9c0a83c5ded5 | 395 | current_mode=tuning_mode; |
melangeaddict | 2:9c0a83c5ded5 | 396 | start_tuning=false; |
melangeaddict | 7:057bcd99a3f8 | 397 | up=1; |
melangeaddict | 7:057bcd99a3f8 | 398 | down=0; |
melangeaddict | 7:057bcd99a3f8 | 399 | wind_up=false; |
melangeaddict | 7:057bcd99a3f8 | 400 | wind_down=false; |
melangeaddict | 7:057bcd99a3f8 | 401 | bias=.5; |
melangeaddict | 2:9c0a83c5ded5 | 402 | |
melangeaddict | 2:9c0a83c5ded5 | 403 | button_init(); |
melangeaddict | 2:9c0a83c5ded5 | 404 | |
melangeaddict | 2:9c0a83c5ded5 | 405 | output_menu(); |
melangeaddict | 3:c672e782f19b | 406 | |
melangeaddict | 2:9c0a83c5ded5 | 407 | } |
melangeaddict | 2:9c0a83c5ded5 | 408 | //*************************************************** |
melangeaddict | 2:9c0a83c5ded5 | 409 | void motor_calibration() { |
melangeaddict | 2:9c0a83c5ded5 | 410 | lcd.cls(); |
melangeaddict | 2:9c0a83c5ded5 | 411 | lcd.printf("Calibrate Motor"); |
melangeaddict | 3:c672e782f19b | 412 | wait(.5); |
melangeaddict | 2:9c0a83c5ded5 | 413 | |
melangeaddict | 2:9c0a83c5ded5 | 414 | float freq=0, freq_up=0, freq_down=0; |
melangeaddict | 2:9c0a83c5ded5 | 415 | bool done=false; |
melangeaddict | 2:9c0a83c5ded5 | 416 | lcd.cls(); |
melangeaddict | 2:9c0a83c5ded5 | 417 | lcd.printf("Please pluck\nstring"); |
melangeaddict | 4:e370f322a697 | 418 | wait(1); |
melangeaddict | 2:9c0a83c5ded5 | 419 | |
melangeaddict | 2:9c0a83c5ded5 | 420 | //motor.motor_turn(up,25)//TODO: Adjust the number of steps here |
melangeaddict | 2:9c0a83c5ded5 | 421 | //On second thought, we don't need to tune up and down for this, we can find the current frequency |
melangeaddict | 2:9c0a83c5ded5 | 422 | //and then turn the peg for the two frequencies! |
melangeaddict | 2:9c0a83c5ded5 | 423 | while (!done) { |
melangeaddict | 2:9c0a83c5ded5 | 424 | freq=guitar.find_frequency(); |
melangeaddict | 2:9c0a83c5ded5 | 425 | |
melangeaddict | 2:9c0a83c5ded5 | 426 | if (check_threshold(freq)) { |
melangeaddict | 3:c672e782f19b | 427 | lcd.cls(); |
melangeaddict | 2:9c0a83c5ded5 | 428 | freq_up=freq; |
melangeaddict | 2:9c0a83c5ded5 | 429 | done=true; |
melangeaddict | 2:9c0a83c5ded5 | 430 | } |
melangeaddict | 4:e370f322a697 | 431 | |
melangeaddict | 4:e370f322a697 | 432 | if (start_tuning==false) |
melangeaddict | 4:e370f322a697 | 433 | break; |
melangeaddict | 2:9c0a83c5ded5 | 434 | } |
melangeaddict | 2:9c0a83c5ded5 | 435 | motor.motor_turn(down,25);//TODO: Adjust the number of steps here |
melangeaddict | 2:9c0a83c5ded5 | 436 | done=false; |
melangeaddict | 2:9c0a83c5ded5 | 437 | while (!done) { |
melangeaddict | 2:9c0a83c5ded5 | 438 | freq=guitar.find_frequency(); |
melangeaddict | 2:9c0a83c5ded5 | 439 | |
melangeaddict | 2:9c0a83c5ded5 | 440 | if (check_threshold(freq)) { |
melangeaddict | 3:c672e782f19b | 441 | lcd.cls(); |
melangeaddict | 2:9c0a83c5ded5 | 442 | freq_down=freq; |
melangeaddict | 2:9c0a83c5ded5 | 443 | done=true; |
melangeaddict | 2:9c0a83c5ded5 | 444 | } |
melangeaddict | 4:e370f322a697 | 445 | if (start_tuning==false) |
melangeaddict | 4:e370f322a697 | 446 | break; |
melangeaddict | 2:9c0a83c5ded5 | 447 | } |
melangeaddict | 2:9c0a83c5ded5 | 448 | |
melangeaddict | 2:9c0a83c5ded5 | 449 | if (freq_up<freq_down) { |
melangeaddict | 8:651fbf5ae98a | 450 | //down=0; |
melangeaddict | 8:651fbf5ae98a | 451 | //up=1; |
melangeaddict | 8:651fbf5ae98a | 452 | } else { |
melangeaddict | 8:651fbf5ae98a | 453 | //down=0; |
melangeaddict | 8:651fbf5ae98a | 454 | //up=1; |
melangeaddict | 2:9c0a83c5ded5 | 455 | } |
melangeaddict | 2:9c0a83c5ded5 | 456 | |
melangeaddict | 2:9c0a83c5ded5 | 457 | lcd.cls(); |
melangeaddict | 2:9c0a83c5ded5 | 458 | lcd.printf("Calibration Done"); |
melangeaddict | 4:e370f322a697 | 459 | |
melangeaddict | 4:e370f322a697 | 460 | if (start_tuning==false) { |
melangeaddict | 4:e370f322a697 | 461 | output_menu(); |
melangeaddict | 4:e370f322a697 | 462 | setup_buttons(); |
melangeaddict | 4:e370f322a697 | 463 | } |
melangeaddict | 2:9c0a83c5ded5 | 464 | } |
melangeaddict | 2:9c0a83c5ded5 | 465 | //********************************************** |
melangeaddict | 2:9c0a83c5ded5 | 466 | bool check_threshold(float freq) { |
melangeaddict | 9:2a211133e79a | 467 | strings *temp=&strings_array[selected_string]; |
melangeaddict | 9:2a211133e79a | 468 | float desired_freq=temp->get_freq();//Get the desired frequency for the string selected |
melangeaddict | 11:bdad1acccdad | 469 | |
melangeaddict | 9:2a211133e79a | 470 | if (freq>(desired_freq+75) || freq<(desired_freq-75) || freq>500) {//new_freq>(desired_freq+50) || new_freq<(desired_freq-50) |
melangeaddict | 2:9c0a83c5ded5 | 471 | lcd.cls(); |
melangeaddict | 11:bdad1acccdad | 472 | lcd.printf("Pluck string %d\nagain",selected_string+1); |
melangeaddict | 11:bdad1acccdad | 473 | |
melangeaddict | 11:bdad1acccdad | 474 | set_LED(1,1); |
melangeaddict | 11:bdad1acccdad | 475 | set_LED(2,1); |
melangeaddict | 11:bdad1acccdad | 476 | set_LED(3,1);//Turn led off |
melangeaddict | 11:bdad1acccdad | 477 | |
melangeaddict | 11:bdad1acccdad | 478 | set_LED(2,0); |
melangeaddict | 11:bdad1acccdad | 479 | set_LED(3,0);//Make the LED turn yellow |
melangeaddict | 11:bdad1acccdad | 480 | |
melangeaddict | 9:2a211133e79a | 481 | wait(.5); |
melangeaddict | 2:9c0a83c5ded5 | 482 | return false; |
melangeaddict | 2:9c0a83c5ded5 | 483 | } else |
melangeaddict | 2:9c0a83c5ded5 | 484 | return true; |
melangeaddict | 2:9c0a83c5ded5 | 485 | } |
melangeaddict | 6:46fb12f6ace1 | 486 | |
melangeaddict | 6:46fb12f6ace1 | 487 | |
melangeaddict | 6:46fb12f6ace1 | 488 | |
melangeaddict | 6:46fb12f6ace1 | 489 | void LED_initialize() { |
melangeaddict | 6:46fb12f6ace1 | 490 | float wait_time=0.3; |
melangeaddict | 9:2a211133e79a | 491 | /* ledBlue=1;//red |
melangeaddict | 9:2a211133e79a | 492 | ledGreen=1; |
melangeaddict | 9:2a211133e79a | 493 | ledRed=0; |
melangeaddict | 9:2a211133e79a | 494 | wait(wait_time); |
melangeaddict | 9:2a211133e79a | 495 | ledRed=1;//green |
melangeaddict | 6:46fb12f6ace1 | 496 | ledGreen=0; |
melangeaddict | 6:46fb12f6ace1 | 497 | wait(wait_time); |
melangeaddict | 6:46fb12f6ace1 | 498 | ledGreen=1; |
melangeaddict | 9:2a211133e79a | 499 | ledBlue=0;//blue |
melangeaddict | 6:46fb12f6ace1 | 500 | wait(wait_time); |
melangeaddict | 6:46fb12f6ace1 | 501 | |
melangeaddict | 9:2a211133e79a | 502 | for (int a=0; a<3; a++) { |
melangeaddict | 9:2a211133e79a | 503 | ledRed=0;//all |
melangeaddict | 9:2a211133e79a | 504 | ledGreen=0; |
melangeaddict | 9:2a211133e79a | 505 | ledBlue=0; |
melangeaddict | 9:2a211133e79a | 506 | wait(wait_time); |
melangeaddict | 9:2a211133e79a | 507 | ledRed=1; |
melangeaddict | 9:2a211133e79a | 508 | ledGreen=1; |
melangeaddict | 9:2a211133e79a | 509 | ledBlue=1; |
melangeaddict | 9:2a211133e79a | 510 | wait(wait_time); |
melangeaddict | 9:2a211133e79a | 511 | |
melangeaddict | 9:2a211133e79a | 512 | } |
melangeaddict | 9:2a211133e79a | 513 | ledBlue=0; |
melangeaddict | 6:46fb12f6ace1 | 514 | |
melangeaddict | 6:46fb12f6ace1 | 515 | |
melangeaddict | 9:2a211133e79a | 516 | ledRed=1;//blue |
melangeaddict | 9:2a211133e79a | 517 | ledGreen=1; |
melangeaddict | 9:2a211133e79a | 518 | */ |
melangeaddict | 9:2a211133e79a | 519 | set_LED(1,1); |
melangeaddict | 9:2a211133e79a | 520 | set_LED(2,1); |
melangeaddict | 9:2a211133e79a | 521 | set_LED(3,1); |
melangeaddict | 9:2a211133e79a | 522 | |
melangeaddict | 9:2a211133e79a | 523 | for (int b=1; b<4; b++) { |
melangeaddict | 9:2a211133e79a | 524 | for (int a=1; a<4; a++) { |
melangeaddict | 9:2a211133e79a | 525 | set_LED(a, 0); |
melangeaddict | 9:2a211133e79a | 526 | wait(wait_time); |
melangeaddict | 9:2a211133e79a | 527 | set_LED(a,1); |
melangeaddict | 9:2a211133e79a | 528 | wait(0.001); |
melangeaddict | 9:2a211133e79a | 529 | |
melangeaddict | 9:2a211133e79a | 530 | } |
melangeaddict | 9:2a211133e79a | 531 | } |
melangeaddict | 9:2a211133e79a | 532 | set_LED(1,0); |
melangeaddict | 9:2a211133e79a | 533 | set_LED(2,1); |
melangeaddict | 9:2a211133e79a | 534 | set_LED(3,1); |
melangeaddict | 6:46fb12f6ace1 | 535 | |
melangeaddict | 6:46fb12f6ace1 | 536 | return; |
melangeaddict | 6:46fb12f6ace1 | 537 | |
melangeaddict | 6:46fb12f6ace1 | 538 | } |
melangeaddict | 6:46fb12f6ace1 | 539 | |
melangeaddict | 6:46fb12f6ace1 | 540 | |
melangeaddict | 6:46fb12f6ace1 | 541 | void set_LED(int led, int value) { |
melangeaddict | 6:46fb12f6ace1 | 542 | //ledBlue=1; |
melangeaddict | 6:46fb12f6ace1 | 543 | //ledRed=2; |
melangeaddict | 6:46fb12f6ace1 | 544 | //ledGreen=3; |
melangeaddict | 8:651fbf5ae98a | 545 | //value must be an int: 0(on) or 1(off) |
melangeaddict | 9:2a211133e79a | 546 | |
melangeaddict | 8:651fbf5ae98a | 547 | if (value==0 || value==1) |
melangeaddict | 6:46fb12f6ace1 | 548 | |
melangeaddict | 7:057bcd99a3f8 | 549 | switch (led) { |
melangeaddict | 7:057bcd99a3f8 | 550 | case(1): |
melangeaddict | 7:057bcd99a3f8 | 551 | ledBlue=value; |
melangeaddict | 7:057bcd99a3f8 | 552 | break; |
melangeaddict | 6:46fb12f6ace1 | 553 | |
melangeaddict | 7:057bcd99a3f8 | 554 | case(2): |
melangeaddict | 7:057bcd99a3f8 | 555 | ledRed=value; |
melangeaddict | 7:057bcd99a3f8 | 556 | break; |
melangeaddict | 6:46fb12f6ace1 | 557 | |
melangeaddict | 7:057bcd99a3f8 | 558 | case(3): |
melangeaddict | 7:057bcd99a3f8 | 559 | ledGreen=value; |
melangeaddict | 7:057bcd99a3f8 | 560 | break; |
melangeaddict | 6:46fb12f6ace1 | 561 | |
melangeaddict | 7:057bcd99a3f8 | 562 | default: |
melangeaddict | 7:057bcd99a3f8 | 563 | break; |
melangeaddict | 6:46fb12f6ace1 | 564 | |
melangeaddict | 7:057bcd99a3f8 | 565 | } |
melangeaddict | 6:46fb12f6ace1 | 566 | |
melangeaddict | 6:46fb12f6ace1 | 567 | return; |
melangeaddict | 6:46fb12f6ace1 | 568 | |
melangeaddict | 6:46fb12f6ace1 | 569 | } |