A program to automatically tune a guitar. Written by Justin Reidhead and Steven Swenson

Dependencies:   FFT FrequencyFinder Motor NewTextLCD PinDetect mbed strings

Committer:
melangeaddict
Date:
Sun Apr 15 00:20:48 2012 +0000
Revision:
1:4a82a5178506
Parent:
0:d8909ac31fbf
Child:
2:9c0a83c5ded5
Immediately after QT 14 April

Who changed what in which revision?

UserRevisionLine numberNew contents of line
melangeaddict 1:4a82a5178506 1 //#include "mbed.h"
melangeaddict 1:4a82a5178506 2 //#include "Motor.h"
melangeaddict 1:4a82a5178506 3 //#include "FrequencyFinder.h"
melangeaddict 1:4a82a5178506 4 //#include "NewTextLCD.h"
melangeaddict 1:4a82a5178506 5 //#include "PinDetect.h"
melangeaddict 1:4a82a5178506 6 #include "strings.h"
melangeaddict 1:4a82a5178506 7 #include <iostream>
melangeaddict 1:4a82a5178506 8 #include <stdio.h>
melangeaddict 1:4a82a5178506 9 #include <vector>
melangeaddict 0:d8909ac31fbf 10
melangeaddict 1:4a82a5178506 11 using namespace std;
melangeaddict 1:4a82a5178506 12 //***************************************************
melangeaddict 1:4a82a5178506 13 //***************Globals*****************************
melangeaddict 1:4a82a5178506 14 //PinDetect string_but(p11);
melangeaddict 1:4a82a5178506 15 //PinDetect pitch_but(p12);
melangeaddict 1:4a82a5178506 16 //PinDetect start_but(p13);
melangeaddict 1:4a82a5178506 17 //PinDetect mode_but(p14);
melangeaddict 0:d8909ac31fbf 18
melangeaddict 1:4a82a5178506 19 //DigitalOut led1(LED1);
melangeaddict 1:4a82a5178506 20 //DigitalOut led2(LED2);
melangeaddict 0:d8909ac31fbf 21
melangeaddict 1:4a82a5178506 22 //TextLCD lcd(p21,p22,p23,p24,p25,p26);
melangeaddict 1:4a82a5178506 23 //Motor motor(p26,p22,p21);//enable, direction, step
melangeaddict 1:4a82a5178506 24 //FrequencyFinder guitar(p20);//input
melangeaddict 0:d8909ac31fbf 25
melangeaddict 1:4a82a5178506 26 int selected_string;
melangeaddict 1:4a82a5178506 27 //int selected_pitch;
melangeaddict 1:4a82a5178506 28 bool current_mode;
melangeaddict 1:4a82a5178506 29 bool start_tuning;
melangeaddict 1:4a82a5178506 30 bool up, down;
melangeaddict 0:d8909ac31fbf 31
melangeaddict 1:4a82a5178506 32 vector<strings> strings_array;
melangeaddict 0:d8909ac31fbf 33
melangeaddict 1:4a82a5178506 34 //***************************************************
melangeaddict 1:4a82a5178506 35 //*****************constants*************************
melangeaddict 1:4a82a5178506 36 const bool tuning_mode=false;
melangeaddict 1:4a82a5178506 37 const bool winding_mode=true;
melangeaddict 0:d8909ac31fbf 38
melangeaddict 1:4a82a5178506 39 //***************************************************
melangeaddict 1:4a82a5178506 40 //******************prototypes***********************
melangeaddict 1:4a82a5178506 41 void device_init();
melangeaddict 0:d8909ac31fbf 42
melangeaddict 1:4a82a5178506 43 void string_sel();
melangeaddict 1:4a82a5178506 44 void pitch_sel();
melangeaddict 1:4a82a5178506 45 void start();
melangeaddict 1:4a82a5178506 46 void stop();
melangeaddict 1:4a82a5178506 47 void mode();
melangeaddict 1:4a82a5178506 48 void do_nothing();
melangeaddict 1:4a82a5178506 49 void wind_up();
melangeaddict 1:4a82a5178506 50 void wind_down();
melangeaddict 0:d8909ac31fbf 51
melangeaddict 1:4a82a5178506 52 void button_init();
melangeaddict 1:4a82a5178506 53 void setup_buttons();
melangeaddict 1:4a82a5178506 54 void output_menu();
melangeaddict 1:4a82a5178506 55 void motor_calibration();
melangeaddict 1:4a82a5178506 56 bool check_threshold(float);
melangeaddict 1:4a82a5178506 57 //*************************************************
melangeaddict 1:4a82a5178506 58 //*********************main************************
melangeaddict 0:d8909ac31fbf 59 int main() {
melangeaddict 1:4a82a5178506 60 // lcd.cls();
melangeaddict 1:4a82a5178506 61 // lcd.printf("Perfect\n Pitch");
melangeaddict 1:4a82a5178506 62
melangeaddict 1:4a82a5178506 63 // wait(.5);
melangeaddict 0:d8909ac31fbf 64
melangeaddict 0:d8909ac31fbf 65 device_init();
melangeaddict 1:4a82a5178506 66
melangeaddict 1:4a82a5178506 67 int state=0,next_state=0;
melangeaddict 1:4a82a5178506 68 float old_freq=0,new_freq=0;
melangeaddict 1:4a82a5178506 69 float desired_freq;
melangeaddict 1:4a82a5178506 70 strings *temp=&strings_array[0];
melangeaddict 0:d8909ac31fbf 71 while (1) {
melangeaddict 1:4a82a5178506 72
melangeaddict 0:d8909ac31fbf 73 state=next_state;
melangeaddict 1:4a82a5178506 74
melangeaddict 1:4a82a5178506 75 switch(state){
melangeaddict 1:4a82a5178506 76 //------------------------------------
melangeaddict 1:4a82a5178506 77 case 0://Stay here till the user selects the string and pitch
melangeaddict 1:4a82a5178506 78 if(start_tuning==true){
melangeaddict 0:d8909ac31fbf 79 next_state=1;
melangeaddict 0:d8909ac31fbf 80 }
melangeaddict 1:4a82a5178506 81 else{
melangeaddict 0:d8909ac31fbf 82 next_state=0;
melangeaddict 1:4a82a5178506 83 }
melangeaddict 1:4a82a5178506 84
melangeaddict 1:4a82a5178506 85 break;
melangeaddict 1:4a82a5178506 86 //----------------------------------------
melangeaddict 1:4a82a5178506 87 case 1://motor calibration state
melangeaddict 1:4a82a5178506 88 motor_calibration();
melangeaddict 1:4a82a5178506 89 next_state=2;
melangeaddict 1:4a82a5178506 90 break;
melangeaddict 1:4a82a5178506 91 //-----------------------------------------
melangeaddict 1:4a82a5178506 92 case 2://begin the actual tuning
melangeaddict 1:4a82a5178506 93 temp=&strings_array[selected_string];
melangeaddict 1:4a82a5178506 94 desired_freq=temp->get_freq();
melangeaddict 1:4a82a5178506 95
melangeaddict 1:4a82a5178506 96 next_state=3;
melangeaddict 1:4a82a5178506 97 break;
melangeaddict 1:4a82a5178506 98 //-----------------------------------------
melangeaddict 1:4a82a5178506 99 case 3:
melangeaddict 1:4a82a5178506 100 //new_freq=frequency.find_frequency();
melangeaddict 1:4a82a5178506 101 if(check_threshold(new_freq)){
melangeaddict 1:4a82a5178506 102 if((desired_freq-.5)<new_freq && (desired_freq+.5)>new_freq){//We are within .5Hz of the desired frequency
melangeaddict 1:4a82a5178506 103 //lcd.cls();
melangeaddict 1:4a82a5178506 104 //lcd.
melangeaddict 1:4a82a5178506 105 printf("String %d\ntuned",selected_string);
melangeaddict 1:4a82a5178506 106 //wait(.5);
melangeaddict 1:4a82a5178506 107
melangeaddict 1:4a82a5178506 108 start_tuning=false;
melangeaddict 1:4a82a5178506 109 next_state=0;
melangeaddict 1:4a82a5178506 110 }
melangeaddict 1:4a82a5178506 111 else if((desired_freq-.5)>new_freq){
melangeaddict 1:4a82a5178506 112 //motor(up,# of steps);
melangeaddict 1:4a82a5178506 113 next_state=3;
melangeaddict 1:4a82a5178506 114 }
melangeaddict 1:4a82a5178506 115 else{
melangeaddict 1:4a82a5178506 116 //motor(down,# of steps);
melangeaddict 1:4a82a5178506 117 next_state=3;
melangeaddict 1:4a82a5178506 118 }
melangeaddict 1:4a82a5178506 119 }
melangeaddict 1:4a82a5178506 120 else{
melangeaddict 1:4a82a5178506 121 next_state=3;
melangeaddict 1:4a82a5178506 122 }
melangeaddict 1:4a82a5178506 123
melangeaddict 1:4a82a5178506 124 //TODO:Determine number of steps per frequency change
melangeaddict 1:4a82a5178506 125 old_freq=new_freq;
melangeaddict 1:4a82a5178506 126 break;
melangeaddict 1:4a82a5178506 127 //-----------------------------------------
melangeaddict 1:4a82a5178506 128 default:
melangeaddict 1:4a82a5178506 129 break;
melangeaddict 1:4a82a5178506 130 }
melangeaddict 1:4a82a5178506 131 //wait_ms(5);
melangeaddict 1:4a82a5178506 132
melangeaddict 1:4a82a5178506 133 if(start_tuning==false){//If the stop button is pressed, the state machine returns to user input
melangeaddict 1:4a82a5178506 134 next_state=0;
melangeaddict 1:4a82a5178506 135 }
melangeaddict 1:4a82a5178506 136 }
melangeaddict 1:4a82a5178506 137
melangeaddict 1:4a82a5178506 138 // return 0;
melangeaddict 1:4a82a5178506 139 }
melangeaddict 1:4a82a5178506 140
melangeaddict 1:4a82a5178506 141 //***************************************************
melangeaddict 1:4a82a5178506 142 //******************functions************************
melangeaddict 1:4a82a5178506 143 void output_menu() {
melangeaddict 1:4a82a5178506 144 // lcd.cls();
melangeaddict 1:4a82a5178506 145 strings temp=strings_array[selected_string];
melangeaddict 1:4a82a5178506 146 /*lcd.*/printf("Select String: %d\nSelect Pitch: %s",selected_string,temp.get_note().c_str());
melangeaddict 1:4a82a5178506 147 fflush(stdout);
melangeaddict 0:d8909ac31fbf 148 }
melangeaddict 0:d8909ac31fbf 149
melangeaddict 1:4a82a5178506 150 //***************************************************
melangeaddict 1:4a82a5178506 151 void button_init(){
melangeaddict 1:4a82a5178506 152 // string_but.mode( PullDown );
melangeaddict 1:4a82a5178506 153 // string_but.setSampleFrequency();
melangeaddict 1:4a82a5178506 154
melangeaddict 1:4a82a5178506 155 // pitch_but.mode( PullDown );
melangeaddict 1:4a82a5178506 156 // pitch_but.setSampleFrequency();
melangeaddict 1:4a82a5178506 157
melangeaddict 1:4a82a5178506 158 // start_but.mode( PullDown );
melangeaddict 1:4a82a5178506 159 // start_but.setSampleFrequency();
melangeaddict 1:4a82a5178506 160
melangeaddict 1:4a82a5178506 161 // mode_but.mode( PullDown );
melangeaddict 1:4a82a5178506 162 // mode_but.setSampleFrequency();
melangeaddict 1:4a82a5178506 163 }
melangeaddict 1:4a82a5178506 164
melangeaddict 1:4a82a5178506 165 //***************************************************
melangeaddict 1:4a82a5178506 166 void setup_buttons() {
melangeaddict 1:4a82a5178506 167 if(current_mode==tuning_mode){
melangeaddict 1:4a82a5178506 168 // string_but.attach_asserted(&string_sel);
melangeaddict 1:4a82a5178506 169 // pitch_but.attach_asserted(&pitch_sel);
melangeaddict 1:4a82a5178506 170 // start_but.attach_asserted(&start);
melangeaddict 1:4a82a5178506 171 // mode_but.attach_asserted(&mode);
melangeaddict 1:4a82a5178506 172 }
melangeaddict 1:4a82a5178506 173 else{
melangeaddict 1:4a82a5178506 174 // string_but.attach_asserted(&wind_up);
melangeaddict 1:4a82a5178506 175 // pitch_but.attach_asserted(&wind_down);
melangeaddict 1:4a82a5178506 176 // start_but.attach_asserted(&do_nothing);
melangeaddict 1:4a82a5178506 177 // mode_but.attach_asserted(&mode);
melangeaddict 0:d8909ac31fbf 178 }
melangeaddict 0:d8909ac31fbf 179 }
melangeaddict 1:4a82a5178506 180 //***************************************************
melangeaddict 1:4a82a5178506 181 void string_sel() {
melangeaddict 1:4a82a5178506 182 selected_string++;
melangeaddict 1:4a82a5178506 183 if (selected_string>6)
melangeaddict 1:4a82a5178506 184 selected_string=1;
melangeaddict 1:4a82a5178506 185
melangeaddict 1:4a82a5178506 186 output_menu();
melangeaddict 1:4a82a5178506 187 }
melangeaddict 1:4a82a5178506 188 //***************************************************
melangeaddict 1:4a82a5178506 189 void pitch_sel() {
melangeaddict 1:4a82a5178506 190 strings *temp=&strings_array[selected_string];
melangeaddict 1:4a82a5178506 191 temp->inc_index();
melangeaddict 1:4a82a5178506 192
melangeaddict 1:4a82a5178506 193 output_menu();
melangeaddict 1:4a82a5178506 194 }
melangeaddict 1:4a82a5178506 195 //***************************************************
melangeaddict 1:4a82a5178506 196 void start() {
melangeaddict 1:4a82a5178506 197 start_tuning=true;
melangeaddict 1:4a82a5178506 198 }
melangeaddict 1:4a82a5178506 199 //***************************************************
melangeaddict 1:4a82a5178506 200 void mode() {
melangeaddict 1:4a82a5178506 201 if(current_mode==tuning_mode){
melangeaddict 1:4a82a5178506 202 current_mode=winding_mode;
melangeaddict 1:4a82a5178506 203 //lcd.cls();
melangeaddict 1:4a82a5178506 204 /*lcd.*/printf("Winding Mode");
melangeaddict 1:4a82a5178506 205 //wait(1);
melangeaddict 1:4a82a5178506 206 }
melangeaddict 1:4a82a5178506 207 else{
melangeaddict 1:4a82a5178506 208 current_mode=tuning_mode;
melangeaddict 1:4a82a5178506 209 //lcd.cls();
melangeaddict 1:4a82a5178506 210 /*lcd.*/printf("Tuning Mode");
melangeaddict 1:4a82a5178506 211 //wait(1);
melangeaddict 1:4a82a5178506 212 }
melangeaddict 1:4a82a5178506 213 setup_buttons();
melangeaddict 1:4a82a5178506 214 }
melangeaddict 1:4a82a5178506 215 //***************************************************
melangeaddict 1:4a82a5178506 216 void stop() {
melangeaddict 1:4a82a5178506 217 start_tuning=false;
melangeaddict 1:4a82a5178506 218 }
melangeaddict 1:4a82a5178506 219 //***************************************************
melangeaddict 1:4a82a5178506 220 void do_nothing(){
melangeaddict 1:4a82a5178506 221 return;
melangeaddict 1:4a82a5178506 222 }
melangeaddict 1:4a82a5178506 223 //***************************************************
melangeaddict 1:4a82a5178506 224 void wind_up(){
melangeaddict 1:4a82a5178506 225 //motor.motor_turn(1,10);
melangeaddict 1:4a82a5178506 226 }
melangeaddict 1:4a82a5178506 227 //***************************************************
melangeaddict 1:4a82a5178506 228 void wind_down(){
melangeaddict 1:4a82a5178506 229 //motor.motor_turn(0,10);
melangeaddict 1:4a82a5178506 230 }
melangeaddict 1:4a82a5178506 231 //***************************************************
melangeaddict 1:4a82a5178506 232 void device_init(){
melangeaddict 1:4a82a5178506 233 strings string1(1);
melangeaddict 1:4a82a5178506 234 strings string2(2);
melangeaddict 1:4a82a5178506 235 strings string3(3);
melangeaddict 1:4a82a5178506 236 strings string4(4);
melangeaddict 1:4a82a5178506 237 strings string5(5);
melangeaddict 1:4a82a5178506 238 strings string6(6);
melangeaddict 1:4a82a5178506 239 strings_array.push_back(string1);
melangeaddict 1:4a82a5178506 240 strings_array.push_back(string2);
melangeaddict 1:4a82a5178506 241 strings_array.push_back(string3);
melangeaddict 1:4a82a5178506 242 strings_array.push_back(string4);
melangeaddict 1:4a82a5178506 243 strings_array.push_back(string5);
melangeaddict 1:4a82a5178506 244 strings_array.push_back(string6);
melangeaddict 1:4a82a5178506 245
melangeaddict 1:4a82a5178506 246 selected_string=1;
melangeaddict 1:4a82a5178506 247 current_mode=tuning_mode;
melangeaddict 1:4a82a5178506 248 start_tuning=false;
melangeaddict 1:4a82a5178506 249 up=true;
melangeaddict 1:4a82a5178506 250 down=false;
melangeaddict 1:4a82a5178506 251
melangeaddict 1:4a82a5178506 252 button_init();
melangeaddict 1:4a82a5178506 253
melangeaddict 1:4a82a5178506 254 output_menu();
melangeaddict 1:4a82a5178506 255 }
melangeaddict 1:4a82a5178506 256 //***************************************************
melangeaddict 1:4a82a5178506 257 void motor_calibration(){
melangeaddict 1:4a82a5178506 258 //lcd.cls();
melangeaddict 1:4a82a5178506 259 //lcd.
melangeaddict 1:4a82a5178506 260 printf("Calibrate Motor");
melangeaddict 1:4a82a5178506 261
melangeaddict 1:4a82a5178506 262 float freq=0, freq_up, freq_down;
melangeaddict 1:4a82a5178506 263 // bool calibration_done=false;
melangeaddict 1:4a82a5178506 264 bool done=false;
melangeaddict 1:4a82a5178506 265 //lcd.cls();
melangeaddict 1:4a82a5178506 266 /*lcd.*/
melangeaddict 1:4a82a5178506 267 printf("Please pluck\nstring");
melangeaddict 1:4a82a5178506 268
melangeaddict 1:4a82a5178506 269 //motor.motor_turn(up,25)//TODO: Adjust the number of steps here
melangeaddict 1:4a82a5178506 270 //On second thought, we don't need to tune up and down for this, we can find the current frequency
melangeaddict 1:4a82a5178506 271 //and then turn the peg for two frequencies!
melangeaddict 1:4a82a5178506 272 while(!done){
melangeaddict 1:4a82a5178506 273 //freq=frequency.find_frequency();
melangeaddict 1:4a82a5178506 274
melangeaddict 1:4a82a5178506 275 if(freq>500){
melangeaddict 1:4a82a5178506 276 printf("Please pluck\nstring again");
melangeaddict 1:4a82a5178506 277 //wait(.5);
melangeaddict 1:4a82a5178506 278 //lcd.cls();
melangeaddict 1:4a82a5178506 279 }
melangeaddict 1:4a82a5178506 280 else{
melangeaddict 1:4a82a5178506 281 freq_up=freq;
melangeaddict 1:4a82a5178506 282 done=true;
melangeaddict 1:4a82a5178506 283 }
melangeaddict 1:4a82a5178506 284 }
melangeaddict 1:4a82a5178506 285 //motor.motor_turn(down,25)//TODO: Adjust the number of steps here
melangeaddict 1:4a82a5178506 286 done=false;
melangeaddict 1:4a82a5178506 287 while(!done){
melangeaddict 1:4a82a5178506 288 //freq=frequency.find_frequency();
melangeaddict 1:4a82a5178506 289
melangeaddict 1:4a82a5178506 290 if(freq>500){
melangeaddict 1:4a82a5178506 291 printf("Please pluck\nstring again");
melangeaddict 1:4a82a5178506 292 //wait(.5);
melangeaddict 1:4a82a5178506 293 //lcd.cls();
melangeaddict 1:4a82a5178506 294 }
melangeaddict 1:4a82a5178506 295 else{
melangeaddict 1:4a82a5178506 296 freq_down=freq;
melangeaddict 1:4a82a5178506 297 done=true;
melangeaddict 1:4a82a5178506 298 }
melangeaddict 1:4a82a5178506 299 }
melangeaddict 1:4a82a5178506 300
melangeaddict 1:4a82a5178506 301 if(freq_up<freq_down){
melangeaddict 1:4a82a5178506 302 down=true;
melangeaddict 1:4a82a5178506 303 up=false;
melangeaddict 1:4a82a5178506 304 }
melangeaddict 1:4a82a5178506 305
melangeaddict 1:4a82a5178506 306 //lcd.cls();
melangeaddict 1:4a82a5178506 307 //lcd.
melangeaddict 1:4a82a5178506 308 printf("Calibration Done");
melangeaddict 1:4a82a5178506 309 }
melangeaddict 1:4a82a5178506 310 //**********************************************
melangeaddict 1:4a82a5178506 311 bool check_threshold(float freq){
melangeaddict 1:4a82a5178506 312 if(freq>500){
melangeaddict 1:4a82a5178506 313 //lcd.cls();
melangeaddict 1:4a82a5178506 314 //lcd.
melangeaddict 1:4a82a5178506 315 printf("Pluck string \nagain");
melangeaddict 1:4a82a5178506 316 return false;
melangeaddict 1:4a82a5178506 317 }
melangeaddict 1:4a82a5178506 318 else
melangeaddict 1:4a82a5178506 319 return true;
melangeaddict 1:4a82a5178506 320 }