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