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