A program to automatically tune a guitar. Written by Justin Reidhead and Steven Swenson
Dependencies: FFT FrequencyFinder Motor NewTextLCD PinDetect mbed strings
main.cpp@13:948c7d19acb9, 2012-04-26 (annotated)
- Committer:
- melangeaddict
- Date:
- Thu Apr 26 21:22:56 2012 +0000
- Revision:
- 13:948c7d19acb9
- Parent:
- 12:03c189de6e2e
- Child:
- 14:fd59e7acf2e5
working on string steps;
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 | 12:03c189de6e2e | 69 | int get_steps(float desired, float current); |
melangeaddict | 2:9c0a83c5ded5 | 70 | //************************************************* |
melangeaddict | 2:9c0a83c5ded5 | 71 | //*********************main************************ |
melangeaddict | 2:9c0a83c5ded5 | 72 | int main() { |
melangeaddict | 3:c672e782f19b | 73 | lcd.cls();//Clear the LCD |
melangeaddict | 3:c672e782f19b | 74 | lcd.printf("Perfect\n Pitch");//Modify to whatever we want to name this thing |
melangeaddict | 9:2a211133e79a | 75 | LED_initialize(); |
melangeaddict | 11:bdad1acccdad | 76 | // wait(.5); |
melangeaddict | 2:9c0a83c5ded5 | 77 | |
melangeaddict | 3:c672e782f19b | 78 | device_init();//Setup buttons and set global variables |
melangeaddict | 3:c672e782f19b | 79 | |
melangeaddict | 11:bdad1acccdad | 80 | |
melangeaddict | 13:948c7d19acb9 | 81 | float freq_acc[6] = {.7,.7,.6,.6,.5,.5};//Values to be between when tuning i.e., the first string is +-.7 to desired |
melangeaddict | 2:9c0a83c5ded5 | 82 | int state=0,next_state=0; |
melangeaddict | 12:03c189de6e2e | 83 | int num_steps=0; |
melangeaddict | 11:bdad1acccdad | 84 | //float old_freq=0; |
melangeaddict | 12:03c189de6e2e | 85 | short current_direction=up; |
melangeaddict | 2:9c0a83c5ded5 | 86 | float new_freq=0; |
melangeaddict | 2:9c0a83c5ded5 | 87 | float desired_freq=0; |
melangeaddict | 2:9c0a83c5ded5 | 88 | strings *temp=&strings_array[0]; |
melangeaddict | 7:057bcd99a3f8 | 89 | wait(.5); |
melangeaddict | 3:c672e782f19b | 90 | |
melangeaddict | 3:c672e782f19b | 91 | output_menu(); |
melangeaddict | 3:c672e782f19b | 92 | |
melangeaddict | 2:9c0a83c5ded5 | 93 | while (1) { |
melangeaddict | 2:9c0a83c5ded5 | 94 | state=next_state; |
melangeaddict | 2:9c0a83c5ded5 | 95 | |
melangeaddict | 2:9c0a83c5ded5 | 96 | switch (state) { |
melangeaddict | 2:9c0a83c5ded5 | 97 | //------------------------------------ |
melangeaddict | 2:9c0a83c5ded5 | 98 | case 0://Stay here till the user selects the string and pitch |
melangeaddict | 2:9c0a83c5ded5 | 99 | if (start_tuning==true) { |
melangeaddict | 2:9c0a83c5ded5 | 100 | next_state=1; |
melangeaddict | 2:9c0a83c5ded5 | 101 | } else { |
melangeaddict | 2:9c0a83c5ded5 | 102 | next_state=0; |
melangeaddict | 3:c672e782f19b | 103 | //output_menu(); |
melangeaddict | 2:9c0a83c5ded5 | 104 | } |
melangeaddict | 2:9c0a83c5ded5 | 105 | break; |
melangeaddict | 2:9c0a83c5ded5 | 106 | //---------------------------------------- |
melangeaddict | 2:9c0a83c5ded5 | 107 | case 1://motor calibration state |
melangeaddict | 9:2a211133e79a | 108 | //motor_calibration();//determine which direction is up and down for the motor |
melangeaddict | 2:9c0a83c5ded5 | 109 | next_state=2; |
melangeaddict | 2:9c0a83c5ded5 | 110 | break; |
melangeaddict | 2:9c0a83c5ded5 | 111 | //----------------------------------------- |
melangeaddict | 2:9c0a83c5ded5 | 112 | case 2://begin the actual tuning |
melangeaddict | 12:03c189de6e2e | 113 | |
melangeaddict | 2:9c0a83c5ded5 | 114 | temp=&strings_array[selected_string]; |
melangeaddict | 3:c672e782f19b | 115 | desired_freq=temp->get_freq();//Get the desired frequency for the string selected |
melangeaddict | 11:bdad1acccdad | 116 | //old_freq=desired_freq;//We have to initalize it to something... |
melangeaddict | 2:9c0a83c5ded5 | 117 | |
melangeaddict | 2:9c0a83c5ded5 | 118 | next_state=3; |
melangeaddict | 2:9c0a83c5ded5 | 119 | break; |
melangeaddict | 2:9c0a83c5ded5 | 120 | //----------------------------------------- |
melangeaddict | 3:c672e782f19b | 121 | case 3://Do the dirty work of tuning |
melangeaddict | 3:c672e782f19b | 122 | new_freq=guitar.find_frequency();//Get the current frequency of the string |
melangeaddict | 9:2a211133e79a | 123 | |
melangeaddict | 12:03c189de6e2e | 124 | if (desired_freq*2.2>new_freq && desired_freq*1.8<new_freq) { |
melangeaddict | 12:03c189de6e2e | 125 | new_freq=new_freq/2; |
melangeaddict | 12:03c189de6e2e | 126 | } |
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 | 12:03c189de6e2e | 129 | |
melangeaddict | 12:03c189de6e2e | 130 | num_steps=get_steps(desired_freq, new_freq); |
melangeaddict | 12:03c189de6e2e | 131 | |
melangeaddict | 13:948c7d19acb9 | 132 | if ((desired_freq-freq_acc[selected_string])<new_freq && (desired_freq+freq_acc[selected_string])>new_freq) {//We are within .5Hz of the desired frequency |
melangeaddict | 11:bdad1acccdad | 133 | |
melangeaddict | 12:03c189de6e2e | 134 | new_freq=guitar.find_frequency(); |
melangeaddict | 13:948c7d19acb9 | 135 | if ((desired_freq-freq_acc[selected_string])>new_freq || (desired_freq+freq_acc[selected_string])<new_freq) {//This checks the frequency again to make sure we are close |
melangeaddict | 12:03c189de6e2e | 136 | next_state=3; |
melangeaddict | 12:03c189de6e2e | 137 | break; |
melangeaddict | 12:03c189de6e2e | 138 | } |
melangeaddict | 12:03c189de6e2e | 139 | |
melangeaddict | 2:9c0a83c5ded5 | 140 | lcd.cls(); |
melangeaddict | 5:c0fd99f07536 | 141 | lcd.printf("String %d\ntuned",selected_string+1); |
melangeaddict | 8:651fbf5ae98a | 142 | set_LED(1,0);//blue on |
melangeaddict | 8:651fbf5ae98a | 143 | set_LED(2,1);//red off; |
melangeaddict | 8:651fbf5ae98a | 144 | set_LED(3,1);//green off |
melangeaddict | 12:03c189de6e2e | 145 | wait(1); |
melangeaddict | 2:9c0a83c5ded5 | 146 | |
melangeaddict | 2:9c0a83c5ded5 | 147 | start_tuning=false; |
melangeaddict | 5:c0fd99f07536 | 148 | output_menu(); |
melangeaddict | 5:c0fd99f07536 | 149 | setup_buttons(); |
melangeaddict | 13:948c7d19acb9 | 150 | new_freq=0;//Just to reset |
melangeaddict | 2:9c0a83c5ded5 | 151 | next_state=0; |
melangeaddict | 3:c672e782f19b | 152 | } else if ((desired_freq-.5)>new_freq) {//We are too low, and need to turn the string tigher |
melangeaddict | 12:03c189de6e2e | 153 | if (current_direction==down) { |
melangeaddict | 13:948c7d19acb9 | 154 | num_steps+=10;//For deadband |
melangeaddict | 12:03c189de6e2e | 155 | current_direction=up; |
melangeaddict | 12:03c189de6e2e | 156 | } |
melangeaddict | 12:03c189de6e2e | 157 | |
melangeaddict | 13:948c7d19acb9 | 158 | //lcd.cls(); |
melangeaddict | 13:948c7d19acb9 | 159 | //lcd.printf("Tuning up"); |
melangeaddict | 9:2a211133e79a | 160 | |
melangeaddict | 8:651fbf5ae98a | 161 | set_LED(1,1);//blue off |
melangeaddict | 8:651fbf5ae98a | 162 | set_LED(2,0);//red on; |
melangeaddict | 8:651fbf5ae98a | 163 | set_LED(3,1);//green off |
melangeaddict | 9:2a211133e79a | 164 | |
melangeaddict | 11:bdad1acccdad | 165 | //found_frequency=0; |
melangeaddict | 9:2a211133e79a | 166 | |
melangeaddict | 12:03c189de6e2e | 167 | //wait(.5); |
melangeaddict | 8:651fbf5ae98a | 168 | |
melangeaddict | 12:03c189de6e2e | 169 | motor.motor_turn(up,num_steps);//TODO:Adjust # of steps |
melangeaddict | 2:9c0a83c5ded5 | 170 | next_state=3; |
melangeaddict | 8:651fbf5ae98a | 171 | |
melangeaddict | 3:c672e782f19b | 172 | } else {//We are too high, and need to loosen the string |
melangeaddict | 12:03c189de6e2e | 173 | if (current_direction==up) { |
melangeaddict | 13:948c7d19acb9 | 174 | num_steps+=10;//For deadband |
melangeaddict | 12:03c189de6e2e | 175 | current_direction=down; |
melangeaddict | 12:03c189de6e2e | 176 | } |
melangeaddict | 12:03c189de6e2e | 177 | |
melangeaddict | 13:948c7d19acb9 | 178 | //lcd.cls(); |
melangeaddict | 13:948c7d19acb9 | 179 | //lcd.printf("Tuning down"); |
melangeaddict | 8:651fbf5ae98a | 180 | set_LED(1,1);//blue off |
melangeaddict | 8:651fbf5ae98a | 181 | set_LED(2,1);//red off; |
melangeaddict | 8:651fbf5ae98a | 182 | set_LED(3,0);//green on |
melangeaddict | 11:bdad1acccdad | 183 | |
melangeaddict | 11:bdad1acccdad | 184 | //found_frequency=0; |
melangeaddict | 11:bdad1acccdad | 185 | |
melangeaddict | 12:03c189de6e2e | 186 | //wait(.5); |
melangeaddict | 8:651fbf5ae98a | 187 | |
melangeaddict | 12:03c189de6e2e | 188 | motor.motor_turn(down,num_steps); |
melangeaddict | 2:9c0a83c5ded5 | 189 | next_state=3; |
melangeaddict | 2:9c0a83c5ded5 | 190 | } |
melangeaddict | 2:9c0a83c5ded5 | 191 | } else { |
melangeaddict | 2:9c0a83c5ded5 | 192 | next_state=3; |
melangeaddict | 2:9c0a83c5ded5 | 193 | } |
melangeaddict | 2:9c0a83c5ded5 | 194 | |
melangeaddict | 8:651fbf5ae98a | 195 | if (start_tuning==false) {//If the stop button is pressed, the state machine returns to user input |
melangeaddict | 8:651fbf5ae98a | 196 | next_state=0; |
melangeaddict | 8:651fbf5ae98a | 197 | setup_buttons(); |
melangeaddict | 8:651fbf5ae98a | 198 | output_menu(); |
melangeaddict | 8:651fbf5ae98a | 199 | } |
melangeaddict | 8:651fbf5ae98a | 200 | |
melangeaddict | 2:9c0a83c5ded5 | 201 | //TODO:Determine number of steps per frequency change |
melangeaddict | 9:2a211133e79a | 202 | |
melangeaddict | 2:9c0a83c5ded5 | 203 | break; |
melangeaddict | 2:9c0a83c5ded5 | 204 | //----------------------------------------- |
melangeaddict | 3:c672e782f19b | 205 | case 4://Winding mode |
melangeaddict | 13:948c7d19acb9 | 206 | |
melangeaddict | 3:c672e782f19b | 207 | if (current_mode==winding_mode) { |
melangeaddict | 3:c672e782f19b | 208 | if (wind_up) { |
melangeaddict | 3:c672e782f19b | 209 | motor.motor_turn(up,5);//TODO:Adjust number of turns |
melangeaddict | 3:c672e782f19b | 210 | } |
melangeaddict | 3:c672e782f19b | 211 | |
melangeaddict | 3:c672e782f19b | 212 | if (wind_down) { |
melangeaddict | 3:c672e782f19b | 213 | motor.motor_turn(down,5);//TODO:Adjust number of turns |
melangeaddict | 3:c672e782f19b | 214 | } |
melangeaddict | 3:c672e782f19b | 215 | next_state=4; |
melangeaddict | 3:c672e782f19b | 216 | } else { |
melangeaddict | 7:057bcd99a3f8 | 217 | output_menu(); |
melangeaddict | 7:057bcd99a3f8 | 218 | wind_up=false; |
melangeaddict | 7:057bcd99a3f8 | 219 | wind_down=false; |
melangeaddict | 7:057bcd99a3f8 | 220 | setup_buttons(); |
melangeaddict | 11:bdad1acccdad | 221 | //set_LED(1,0); |
melangeaddict | 3:c672e782f19b | 222 | next_state=0; |
melangeaddict | 3:c672e782f19b | 223 | } |
melangeaddict | 3:c672e782f19b | 224 | break; |
melangeaddict | 2:9c0a83c5ded5 | 225 | default: |
melangeaddict | 2:9c0a83c5ded5 | 226 | break; |
melangeaddict | 3:c672e782f19b | 227 | }//end switch |
melangeaddict | 2:9c0a83c5ded5 | 228 | wait_ms(5); |
melangeaddict | 2:9c0a83c5ded5 | 229 | |
melangeaddict | 2:9c0a83c5ded5 | 230 | if (start_tuning==false) {//If the stop button is pressed, the state machine returns to user input |
melangeaddict | 2:9c0a83c5ded5 | 231 | next_state=0; |
melangeaddict | 4:e370f322a697 | 232 | //setup_buttons(); |
melangeaddict | 4:e370f322a697 | 233 | //output_menu(); |
melangeaddict | 2:9c0a83c5ded5 | 234 | } |
melangeaddict | 3:c672e782f19b | 235 | |
melangeaddict | 3:c672e782f19b | 236 | if (current_mode==winding_mode) {//TODO:Debug this |
melangeaddict | 3:c672e782f19b | 237 | next_state=4; |
melangeaddict | 3:c672e782f19b | 238 | } |
melangeaddict | 3:c672e782f19b | 239 | |
melangeaddict | 3:c672e782f19b | 240 | if (start_tuning) { |
melangeaddict | 12:03c189de6e2e | 241 | if (check_threshold(new_freq)) { |
melangeaddict | 12:03c189de6e2e | 242 | lcd.cls(); |
melangeaddict | 12:03c189de6e2e | 243 | lcd.locate(9,0); |
melangeaddict | 12:03c189de6e2e | 244 | lcd.printf("%f",new_freq); |
melangeaddict | 12:03c189de6e2e | 245 | lcd.locate(9,1); |
melangeaddict | 12:03c189de6e2e | 246 | lcd.printf("%f",desired_freq); |
melangeaddict | 7:057bcd99a3f8 | 247 | |
melangeaddict | 12:03c189de6e2e | 248 | lcd.locate(0,1); |
melangeaddict | 12:03c189de6e2e | 249 | lcd.printf("Desired"); |
melangeaddict | 12:03c189de6e2e | 250 | lcd.locate(0,0); |
melangeaddict | 12:03c189de6e2e | 251 | lcd.printf("Detected"); |
melangeaddict | 12:03c189de6e2e | 252 | } |
melangeaddict | 3:c672e782f19b | 253 | } |
melangeaddict | 3:c672e782f19b | 254 | }//end while |
melangeaddict | 2:9c0a83c5ded5 | 255 | |
melangeaddict | 2:9c0a83c5ded5 | 256 | // return 0; |
melangeaddict | 2:9c0a83c5ded5 | 257 | } |
melangeaddict | 2:9c0a83c5ded5 | 258 | |
melangeaddict | 2:9c0a83c5ded5 | 259 | //*************************************************** |
melangeaddict | 2:9c0a83c5ded5 | 260 | //******************functions************************ |
melangeaddict | 3:c672e782f19b | 261 | //Display the string and pitch selection menu |
melangeaddict | 2:9c0a83c5ded5 | 262 | void output_menu() { |
melangeaddict | 2:9c0a83c5ded5 | 263 | lcd.cls(); |
melangeaddict | 4:e370f322a697 | 264 | lcd.printf("Select String: %d",selected_string+1); |
melangeaddict | 4:e370f322a697 | 265 | |
melangeaddict | 3:c672e782f19b | 266 | lcd.locate(0,1); |
melangeaddict | 3:c672e782f19b | 267 | strings *temp=&strings_array[selected_string]; |
melangeaddict | 3:c672e782f19b | 268 | lcd.printf("Select Pitch: "); |
melangeaddict | 5:c0fd99f07536 | 269 | lcd.printf("%s",temp->get_note()); |
melangeaddict | 5:c0fd99f07536 | 270 | //wait(.5); |
melangeaddict | 12:03c189de6e2e | 271 | |
melangeaddict | 11:bdad1acccdad | 272 | set_LED(1,0);//Turn blue LED on |
melangeaddict | 11:bdad1acccdad | 273 | set_LED(2,1); |
melangeaddict | 11:bdad1acccdad | 274 | set_LED(3,1); |
melangeaddict | 2:9c0a83c5ded5 | 275 | } |
melangeaddict | 2:9c0a83c5ded5 | 276 | |
melangeaddict | 2:9c0a83c5ded5 | 277 | //*************************************************** |
melangeaddict | 3:c672e782f19b | 278 | //Initialize the buttons |
melangeaddict | 2:9c0a83c5ded5 | 279 | void button_init() { |
melangeaddict | 2:9c0a83c5ded5 | 280 | string_but.mode( PullDown ); |
melangeaddict | 2:9c0a83c5ded5 | 281 | string_but.setSampleFrequency(); |
melangeaddict | 2:9c0a83c5ded5 | 282 | |
melangeaddict | 2:9c0a83c5ded5 | 283 | pitch_but.mode( PullDown ); |
melangeaddict | 2:9c0a83c5ded5 | 284 | pitch_but.setSampleFrequency(); |
melangeaddict | 2:9c0a83c5ded5 | 285 | |
melangeaddict | 2:9c0a83c5ded5 | 286 | start_but.mode( PullDown ); |
melangeaddict | 2:9c0a83c5ded5 | 287 | start_but.setSampleFrequency(); |
melangeaddict | 2:9c0a83c5ded5 | 288 | |
melangeaddict | 2:9c0a83c5ded5 | 289 | mode_but.mode( PullDown ); |
melangeaddict | 2:9c0a83c5ded5 | 290 | mode_but.setSampleFrequency(); |
melangeaddict | 4:e370f322a697 | 291 | |
melangeaddict | 4:e370f322a697 | 292 | setup_buttons(); |
melangeaddict | 2:9c0a83c5ded5 | 293 | } |
melangeaddict | 2:9c0a83c5ded5 | 294 | |
melangeaddict | 2:9c0a83c5ded5 | 295 | //*************************************************** |
melangeaddict | 3:c672e782f19b | 296 | //Depending on the current mode, the buttons do different things |
melangeaddict | 2:9c0a83c5ded5 | 297 | void setup_buttons() { |
melangeaddict | 3:c672e782f19b | 298 | if (current_mode==tuning_mode) {//Tuning mode |
melangeaddict | 2:9c0a83c5ded5 | 299 | string_but.attach_asserted(&string_sel); |
melangeaddict | 3:c672e782f19b | 300 | string_but.attach_deasserted(&do_nothing); |
melangeaddict | 2:9c0a83c5ded5 | 301 | pitch_but.attach_asserted(&pitch_sel); |
melangeaddict | 3:c672e782f19b | 302 | pitch_but.attach_deasserted(&do_nothing); |
melangeaddict | 2:9c0a83c5ded5 | 303 | start_but.attach_asserted(&start); |
melangeaddict | 2:9c0a83c5ded5 | 304 | mode_but.attach_asserted(&mode); |
melangeaddict | 3:c672e782f19b | 305 | } else {//Winding mode |
melangeaddict | 3:c672e782f19b | 306 | string_but.attach_asserted(&wind_up_start); |
melangeaddict | 3:c672e782f19b | 307 | string_but.attach_deasserted(&wind_up_stop); |
melangeaddict | 3:c672e782f19b | 308 | pitch_but.attach_asserted(&wind_down_start); |
melangeaddict | 3:c672e782f19b | 309 | pitch_but.attach_deasserted(&wind_down_stop); |
melangeaddict | 2:9c0a83c5ded5 | 310 | start_but.attach_asserted(&do_nothing); |
melangeaddict | 2:9c0a83c5ded5 | 311 | mode_but.attach_asserted(&mode); |
melangeaddict | 2:9c0a83c5ded5 | 312 | } |
melangeaddict | 2:9c0a83c5ded5 | 313 | } |
melangeaddict | 2:9c0a83c5ded5 | 314 | //*************************************************** |
melangeaddict | 3:c672e782f19b | 315 | //Change the selected string - there are only six strings |
melangeaddict | 2:9c0a83c5ded5 | 316 | void string_sel() { |
melangeaddict | 8:651fbf5ae98a | 317 | selected_string--; |
melangeaddict | 8:651fbf5ae98a | 318 | if (selected_string<0) |
melangeaddict | 8:651fbf5ae98a | 319 | selected_string=5; |
melangeaddict | 2:9c0a83c5ded5 | 320 | |
melangeaddict | 5:c0fd99f07536 | 321 | strings *temp=&strings_array[selected_string]; |
melangeaddict | 5:c0fd99f07536 | 322 | temp->reset_index(); |
melangeaddict | 5:c0fd99f07536 | 323 | |
melangeaddict | 2:9c0a83c5ded5 | 324 | output_menu(); |
melangeaddict | 2:9c0a83c5ded5 | 325 | } |
melangeaddict | 2:9c0a83c5ded5 | 326 | //*************************************************** |
melangeaddict | 3:c672e782f19b | 327 | //Change the pitch of the selected string |
melangeaddict | 2:9c0a83c5ded5 | 328 | void pitch_sel() { |
melangeaddict | 2:9c0a83c5ded5 | 329 | strings *temp=&strings_array[selected_string]; |
melangeaddict | 2:9c0a83c5ded5 | 330 | temp->inc_index(); |
melangeaddict | 2:9c0a83c5ded5 | 331 | |
melangeaddict | 2:9c0a83c5ded5 | 332 | output_menu(); |
melangeaddict | 2:9c0a83c5ded5 | 333 | } |
melangeaddict | 2:9c0a83c5ded5 | 334 | //*************************************************** |
melangeaddict | 3:c672e782f19b | 335 | //Start the tuning process |
melangeaddict | 2:9c0a83c5ded5 | 336 | void start() { |
melangeaddict | 2:9c0a83c5ded5 | 337 | start_tuning=true; |
melangeaddict | 3:c672e782f19b | 338 | |
melangeaddict | 3:c672e782f19b | 339 | string_but.attach_asserted(&do_nothing);//Disable the other buttons |
melangeaddict | 5:c0fd99f07536 | 340 | string_but.attach_deasserted(&do_nothing); |
melangeaddict | 3:c672e782f19b | 341 | pitch_but.attach_asserted(&do_nothing); |
melangeaddict | 5:c0fd99f07536 | 342 | pitch_but.attach_deasserted(&do_nothing); |
melangeaddict | 3:c672e782f19b | 343 | start_but.attach_asserted(&stop); //except for the start/stop button |
melangeaddict | 3:c672e782f19b | 344 | mode_but.attach_asserted(&do_nothing); |
melangeaddict | 2:9c0a83c5ded5 | 345 | } |
melangeaddict | 2:9c0a83c5ded5 | 346 | //*************************************************** |
melangeaddict | 2:9c0a83c5ded5 | 347 | void mode() { |
melangeaddict | 2:9c0a83c5ded5 | 348 | if (current_mode==tuning_mode) { |
melangeaddict | 13:948c7d19acb9 | 349 | set_LED(2,0);//red off |
melangeaddict | 13:948c7d19acb9 | 350 | set_LED(1,0);//blue on |
melangeaddict | 13:948c7d19acb9 | 351 | set_LED(3,1);//green on |
melangeaddict | 13:948c7d19acb9 | 352 | |
melangeaddict | 2:9c0a83c5ded5 | 353 | current_mode=winding_mode; |
melangeaddict | 2:9c0a83c5ded5 | 354 | lcd.cls(); |
melangeaddict | 2:9c0a83c5ded5 | 355 | lcd.printf("Winding Mode"); |
melangeaddict | 4:e370f322a697 | 356 | wait(1); |
melangeaddict | 4:e370f322a697 | 357 | lcd.cls(); |
melangeaddict | 5:c0fd99f07536 | 358 | lcd.printf("String for up\nPitch for down"); |
melangeaddict | 2:9c0a83c5ded5 | 359 | } else { |
melangeaddict | 13:948c7d19acb9 | 360 | set_LED(2,1);//red off |
melangeaddict | 13:948c7d19acb9 | 361 | set_LED(1,0);//blue on |
melangeaddict | 13:948c7d19acb9 | 362 | set_LED(3,1);//green off |
melangeaddict | 13:948c7d19acb9 | 363 | |
melangeaddict | 2:9c0a83c5ded5 | 364 | current_mode=tuning_mode; |
melangeaddict | 2:9c0a83c5ded5 | 365 | lcd.cls(); |
melangeaddict | 2:9c0a83c5ded5 | 366 | lcd.printf("Tuning Mode"); |
melangeaddict | 3:c672e782f19b | 367 | wait(.5); |
melangeaddict | 4:e370f322a697 | 368 | output_menu(); |
melangeaddict | 2:9c0a83c5ded5 | 369 | } |
melangeaddict | 3:c672e782f19b | 370 | setup_buttons();//Change the functions the buttons connect to |
melangeaddict | 2:9c0a83c5ded5 | 371 | } |
melangeaddict | 2:9c0a83c5ded5 | 372 | //*************************************************** |
melangeaddict | 2:9c0a83c5ded5 | 373 | void stop() { |
melangeaddict | 2:9c0a83c5ded5 | 374 | start_tuning=false; |
melangeaddict | 7:057bcd99a3f8 | 375 | |
melangeaddict | 7:057bcd99a3f8 | 376 | output_menu(); |
melangeaddict | 7:057bcd99a3f8 | 377 | setup_buttons(); |
melangeaddict | 2:9c0a83c5ded5 | 378 | } |
melangeaddict | 2:9c0a83c5ded5 | 379 | //*************************************************** |
melangeaddict | 2:9c0a83c5ded5 | 380 | void do_nothing() { |
melangeaddict | 2:9c0a83c5ded5 | 381 | return; |
melangeaddict | 2:9c0a83c5ded5 | 382 | } |
melangeaddict | 2:9c0a83c5ded5 | 383 | //*************************************************** |
melangeaddict | 3:c672e782f19b | 384 | void wind_up_start() { |
melangeaddict | 3:c672e782f19b | 385 | wind_up=true; |
melangeaddict | 13:948c7d19acb9 | 386 | set_LED(2,0);//red on |
melangeaddict | 13:948c7d19acb9 | 387 | set_LED(1,1);//blue off |
melangeaddict | 13:948c7d19acb9 | 388 | set_LED(3,1);//green off |
melangeaddict | 2:9c0a83c5ded5 | 389 | } |
melangeaddict | 2:9c0a83c5ded5 | 390 | //*************************************************** |
melangeaddict | 3:c672e782f19b | 391 | void wind_up_stop() { |
melangeaddict | 3:c672e782f19b | 392 | wind_up=false; |
melangeaddict | 13:948c7d19acb9 | 393 | set_LED(2,0);//red off |
melangeaddict | 13:948c7d19acb9 | 394 | set_LED(1,0);//blue on |
melangeaddict | 13:948c7d19acb9 | 395 | set_LED(3,1);//green on |
melangeaddict | 3:c672e782f19b | 396 | } |
melangeaddict | 3:c672e782f19b | 397 | //*************************************************** |
melangeaddict | 3:c672e782f19b | 398 | void wind_down_start() { |
melangeaddict | 3:c672e782f19b | 399 | wind_down=true; |
melangeaddict | 13:948c7d19acb9 | 400 | set_LED(3,0);//green on |
melangeaddict | 13:948c7d19acb9 | 401 | set_LED(1,1);//blue off |
melangeaddict | 13:948c7d19acb9 | 402 | set_LED(2,1);// off |
melangeaddict | 3:c672e782f19b | 403 | } |
melangeaddict | 3:c672e782f19b | 404 | //*************************************************** |
melangeaddict | 3:c672e782f19b | 405 | void wind_down_stop() { |
melangeaddict | 3:c672e782f19b | 406 | wind_down=false; |
melangeaddict | 13:948c7d19acb9 | 407 | set_LED(2,0);//red off |
melangeaddict | 13:948c7d19acb9 | 408 | set_LED(1,0);//blue on |
melangeaddict | 13:948c7d19acb9 | 409 | set_LED(3,1);//green on |
melangeaddict | 2:9c0a83c5ded5 | 410 | } |
melangeaddict | 2:9c0a83c5ded5 | 411 | //*************************************************** |
melangeaddict | 2:9c0a83c5ded5 | 412 | void device_init() { |
melangeaddict | 3:c672e782f19b | 413 | |
melangeaddict | 2:9c0a83c5ded5 | 414 | strings string1(1); |
melangeaddict | 2:9c0a83c5ded5 | 415 | strings string2(2); |
melangeaddict | 2:9c0a83c5ded5 | 416 | strings string3(3); |
melangeaddict | 2:9c0a83c5ded5 | 417 | strings string4(4); |
melangeaddict | 2:9c0a83c5ded5 | 418 | strings string5(5); |
melangeaddict | 2:9c0a83c5ded5 | 419 | strings string6(6); |
melangeaddict | 2:9c0a83c5ded5 | 420 | |
melangeaddict | 3:c672e782f19b | 421 | strings_array[0]=string1; |
melangeaddict | 3:c672e782f19b | 422 | strings_array[1]=string2; |
melangeaddict | 3:c672e782f19b | 423 | strings_array[2]=string3; |
melangeaddict | 3:c672e782f19b | 424 | strings_array[3]=string4; |
melangeaddict | 3:c672e782f19b | 425 | strings_array[4]=string5; |
melangeaddict | 3:c672e782f19b | 426 | strings_array[5]=string6; |
melangeaddict | 3:c672e782f19b | 427 | |
melangeaddict | 4:e370f322a697 | 428 | selected_string=5; |
melangeaddict | 2:9c0a83c5ded5 | 429 | current_mode=tuning_mode; |
melangeaddict | 2:9c0a83c5ded5 | 430 | start_tuning=false; |
melangeaddict | 7:057bcd99a3f8 | 431 | up=1; |
melangeaddict | 7:057bcd99a3f8 | 432 | down=0; |
melangeaddict | 7:057bcd99a3f8 | 433 | wind_up=false; |
melangeaddict | 7:057bcd99a3f8 | 434 | wind_down=false; |
melangeaddict | 7:057bcd99a3f8 | 435 | bias=.5; |
melangeaddict | 2:9c0a83c5ded5 | 436 | |
melangeaddict | 2:9c0a83c5ded5 | 437 | button_init(); |
melangeaddict | 2:9c0a83c5ded5 | 438 | |
melangeaddict | 2:9c0a83c5ded5 | 439 | output_menu(); |
melangeaddict | 3:c672e782f19b | 440 | |
melangeaddict | 2:9c0a83c5ded5 | 441 | } |
melangeaddict | 2:9c0a83c5ded5 | 442 | //*************************************************** |
melangeaddict | 2:9c0a83c5ded5 | 443 | void motor_calibration() { |
melangeaddict | 2:9c0a83c5ded5 | 444 | lcd.cls(); |
melangeaddict | 2:9c0a83c5ded5 | 445 | lcd.printf("Calibrate Motor"); |
melangeaddict | 3:c672e782f19b | 446 | wait(.5); |
melangeaddict | 2:9c0a83c5ded5 | 447 | |
melangeaddict | 2:9c0a83c5ded5 | 448 | float freq=0, freq_up=0, freq_down=0; |
melangeaddict | 2:9c0a83c5ded5 | 449 | bool done=false; |
melangeaddict | 2:9c0a83c5ded5 | 450 | lcd.cls(); |
melangeaddict | 2:9c0a83c5ded5 | 451 | lcd.printf("Please pluck\nstring"); |
melangeaddict | 4:e370f322a697 | 452 | wait(1); |
melangeaddict | 2:9c0a83c5ded5 | 453 | |
melangeaddict | 2:9c0a83c5ded5 | 454 | //motor.motor_turn(up,25)//TODO: Adjust the number of steps here |
melangeaddict | 2:9c0a83c5ded5 | 455 | //On second thought, we don't need to tune up and down for this, we can find the current frequency |
melangeaddict | 2:9c0a83c5ded5 | 456 | //and then turn the peg for the two frequencies! |
melangeaddict | 2:9c0a83c5ded5 | 457 | while (!done) { |
melangeaddict | 2:9c0a83c5ded5 | 458 | freq=guitar.find_frequency(); |
melangeaddict | 2:9c0a83c5ded5 | 459 | |
melangeaddict | 2:9c0a83c5ded5 | 460 | if (check_threshold(freq)) { |
melangeaddict | 3:c672e782f19b | 461 | lcd.cls(); |
melangeaddict | 2:9c0a83c5ded5 | 462 | freq_up=freq; |
melangeaddict | 2:9c0a83c5ded5 | 463 | done=true; |
melangeaddict | 2:9c0a83c5ded5 | 464 | } |
melangeaddict | 4:e370f322a697 | 465 | |
melangeaddict | 4:e370f322a697 | 466 | if (start_tuning==false) |
melangeaddict | 4:e370f322a697 | 467 | break; |
melangeaddict | 2:9c0a83c5ded5 | 468 | } |
melangeaddict | 2:9c0a83c5ded5 | 469 | motor.motor_turn(down,25);//TODO: Adjust the number of steps here |
melangeaddict | 2:9c0a83c5ded5 | 470 | done=false; |
melangeaddict | 2:9c0a83c5ded5 | 471 | while (!done) { |
melangeaddict | 2:9c0a83c5ded5 | 472 | freq=guitar.find_frequency(); |
melangeaddict | 2:9c0a83c5ded5 | 473 | |
melangeaddict | 2:9c0a83c5ded5 | 474 | if (check_threshold(freq)) { |
melangeaddict | 3:c672e782f19b | 475 | lcd.cls(); |
melangeaddict | 2:9c0a83c5ded5 | 476 | freq_down=freq; |
melangeaddict | 2:9c0a83c5ded5 | 477 | done=true; |
melangeaddict | 2:9c0a83c5ded5 | 478 | } |
melangeaddict | 4:e370f322a697 | 479 | if (start_tuning==false) |
melangeaddict | 4:e370f322a697 | 480 | break; |
melangeaddict | 2:9c0a83c5ded5 | 481 | } |
melangeaddict | 2:9c0a83c5ded5 | 482 | |
melangeaddict | 2:9c0a83c5ded5 | 483 | if (freq_up<freq_down) { |
melangeaddict | 8:651fbf5ae98a | 484 | //down=0; |
melangeaddict | 8:651fbf5ae98a | 485 | //up=1; |
melangeaddict | 8:651fbf5ae98a | 486 | } else { |
melangeaddict | 8:651fbf5ae98a | 487 | //down=0; |
melangeaddict | 8:651fbf5ae98a | 488 | //up=1; |
melangeaddict | 2:9c0a83c5ded5 | 489 | } |
melangeaddict | 2:9c0a83c5ded5 | 490 | |
melangeaddict | 2:9c0a83c5ded5 | 491 | lcd.cls(); |
melangeaddict | 2:9c0a83c5ded5 | 492 | lcd.printf("Calibration Done"); |
melangeaddict | 4:e370f322a697 | 493 | |
melangeaddict | 4:e370f322a697 | 494 | if (start_tuning==false) { |
melangeaddict | 4:e370f322a697 | 495 | output_menu(); |
melangeaddict | 4:e370f322a697 | 496 | setup_buttons(); |
melangeaddict | 4:e370f322a697 | 497 | } |
melangeaddict | 2:9c0a83c5ded5 | 498 | } |
melangeaddict | 2:9c0a83c5ded5 | 499 | //********************************************** |
melangeaddict | 2:9c0a83c5ded5 | 500 | bool check_threshold(float freq) { |
melangeaddict | 9:2a211133e79a | 501 | strings *temp=&strings_array[selected_string]; |
melangeaddict | 9:2a211133e79a | 502 | float desired_freq=temp->get_freq();//Get the desired frequency for the string selected |
melangeaddict | 12:03c189de6e2e | 503 | |
melangeaddict | 12:03c189de6e2e | 504 | int hertz=0; |
melangeaddict | 12:03c189de6e2e | 505 | switch (selected_string) { |
melangeaddict | 12:03c189de6e2e | 506 | case 0: |
melangeaddict | 12:03c189de6e2e | 507 | hertz=85; |
melangeaddict | 12:03c189de6e2e | 508 | break; |
melangeaddict | 12:03c189de6e2e | 509 | |
melangeaddict | 12:03c189de6e2e | 510 | case 1: |
melangeaddict | 12:03c189de6e2e | 511 | hertz=66; |
melangeaddict | 12:03c189de6e2e | 512 | break; |
melangeaddict | 12:03c189de6e2e | 513 | |
melangeaddict | 12:03c189de6e2e | 514 | case 2: |
melangeaddict | 12:03c189de6e2e | 515 | hertz=53; |
melangeaddict | 12:03c189de6e2e | 516 | break; |
melangeaddict | 12:03c189de6e2e | 517 | |
melangeaddict | 12:03c189de6e2e | 518 | case 3: |
melangeaddict | 12:03c189de6e2e | 519 | hertz=42; |
melangeaddict | 12:03c189de6e2e | 520 | break; |
melangeaddict | 12:03c189de6e2e | 521 | |
melangeaddict | 12:03c189de6e2e | 522 | case 4: |
melangeaddict | 12:03c189de6e2e | 523 | hertz=29; |
melangeaddict | 12:03c189de6e2e | 524 | break; |
melangeaddict | 12:03c189de6e2e | 525 | |
melangeaddict | 12:03c189de6e2e | 526 | case 5: |
melangeaddict | 12:03c189de6e2e | 527 | hertz=22; |
melangeaddict | 12:03c189de6e2e | 528 | break; |
melangeaddict | 12:03c189de6e2e | 529 | |
melangeaddict | 12:03c189de6e2e | 530 | default: |
melangeaddict | 12:03c189de6e2e | 531 | break; |
melangeaddict | 12:03c189de6e2e | 532 | } |
melangeaddict | 12:03c189de6e2e | 533 | |
melangeaddict | 12:03c189de6e2e | 534 | if (freq>(desired_freq+hertz) || freq<(desired_freq-hertz) || freq>500) {//new_freq>(desired_freq+50) || new_freq<(desired_freq-50) |
melangeaddict | 2:9c0a83c5ded5 | 535 | lcd.cls(); |
melangeaddict | 11:bdad1acccdad | 536 | lcd.printf("Pluck string %d\nagain",selected_string+1); |
melangeaddict | 12:03c189de6e2e | 537 | |
melangeaddict | 11:bdad1acccdad | 538 | set_LED(1,1); |
melangeaddict | 11:bdad1acccdad | 539 | set_LED(2,1); |
melangeaddict | 11:bdad1acccdad | 540 | set_LED(3,1);//Turn led off |
melangeaddict | 12:03c189de6e2e | 541 | |
melangeaddict | 11:bdad1acccdad | 542 | set_LED(2,0); |
melangeaddict | 11:bdad1acccdad | 543 | set_LED(3,0);//Make the LED turn yellow |
melangeaddict | 12:03c189de6e2e | 544 | |
melangeaddict | 12:03c189de6e2e | 545 | //wait(.5); |
melangeaddict | 2:9c0a83c5ded5 | 546 | return false; |
melangeaddict | 2:9c0a83c5ded5 | 547 | } else |
melangeaddict | 2:9c0a83c5ded5 | 548 | return true; |
melangeaddict | 2:9c0a83c5ded5 | 549 | } |
melangeaddict | 6:46fb12f6ace1 | 550 | |
melangeaddict | 6:46fb12f6ace1 | 551 | |
melangeaddict | 6:46fb12f6ace1 | 552 | |
melangeaddict | 6:46fb12f6ace1 | 553 | void LED_initialize() { |
melangeaddict | 13:948c7d19acb9 | 554 | float wait_time=0.1; |
melangeaddict | 9:2a211133e79a | 555 | /* ledBlue=1;//red |
melangeaddict | 9:2a211133e79a | 556 | ledGreen=1; |
melangeaddict | 9:2a211133e79a | 557 | ledRed=0; |
melangeaddict | 9:2a211133e79a | 558 | wait(wait_time); |
melangeaddict | 9:2a211133e79a | 559 | ledRed=1;//green |
melangeaddict | 6:46fb12f6ace1 | 560 | ledGreen=0; |
melangeaddict | 6:46fb12f6ace1 | 561 | wait(wait_time); |
melangeaddict | 6:46fb12f6ace1 | 562 | ledGreen=1; |
melangeaddict | 9:2a211133e79a | 563 | ledBlue=0;//blue |
melangeaddict | 6:46fb12f6ace1 | 564 | wait(wait_time); |
melangeaddict | 6:46fb12f6ace1 | 565 | |
melangeaddict | 9:2a211133e79a | 566 | for (int a=0; a<3; a++) { |
melangeaddict | 9:2a211133e79a | 567 | ledRed=0;//all |
melangeaddict | 9:2a211133e79a | 568 | ledGreen=0; |
melangeaddict | 9:2a211133e79a | 569 | ledBlue=0; |
melangeaddict | 9:2a211133e79a | 570 | wait(wait_time); |
melangeaddict | 9:2a211133e79a | 571 | ledRed=1; |
melangeaddict | 9:2a211133e79a | 572 | ledGreen=1; |
melangeaddict | 9:2a211133e79a | 573 | ledBlue=1; |
melangeaddict | 9:2a211133e79a | 574 | wait(wait_time); |
melangeaddict | 9:2a211133e79a | 575 | |
melangeaddict | 9:2a211133e79a | 576 | } |
melangeaddict | 9:2a211133e79a | 577 | ledBlue=0; |
melangeaddict | 6:46fb12f6ace1 | 578 | |
melangeaddict | 6:46fb12f6ace1 | 579 | |
melangeaddict | 9:2a211133e79a | 580 | ledRed=1;//blue |
melangeaddict | 9:2a211133e79a | 581 | ledGreen=1; |
melangeaddict | 9:2a211133e79a | 582 | */ |
melangeaddict | 9:2a211133e79a | 583 | set_LED(1,1); |
melangeaddict | 9:2a211133e79a | 584 | set_LED(2,1); |
melangeaddict | 9:2a211133e79a | 585 | set_LED(3,1); |
melangeaddict | 9:2a211133e79a | 586 | |
melangeaddict | 9:2a211133e79a | 587 | for (int b=1; b<4; b++) { |
melangeaddict | 9:2a211133e79a | 588 | for (int a=1; a<4; a++) { |
melangeaddict | 9:2a211133e79a | 589 | set_LED(a, 0); |
melangeaddict | 9:2a211133e79a | 590 | wait(wait_time); |
melangeaddict | 9:2a211133e79a | 591 | set_LED(a,1); |
melangeaddict | 9:2a211133e79a | 592 | wait(0.001); |
melangeaddict | 9:2a211133e79a | 593 | |
melangeaddict | 9:2a211133e79a | 594 | } |
melangeaddict | 9:2a211133e79a | 595 | } |
melangeaddict | 9:2a211133e79a | 596 | set_LED(1,0); |
melangeaddict | 9:2a211133e79a | 597 | set_LED(2,1); |
melangeaddict | 9:2a211133e79a | 598 | set_LED(3,1); |
melangeaddict | 6:46fb12f6ace1 | 599 | |
melangeaddict | 6:46fb12f6ace1 | 600 | return; |
melangeaddict | 6:46fb12f6ace1 | 601 | |
melangeaddict | 6:46fb12f6ace1 | 602 | } |
melangeaddict | 6:46fb12f6ace1 | 603 | |
melangeaddict | 6:46fb12f6ace1 | 604 | |
melangeaddict | 6:46fb12f6ace1 | 605 | void set_LED(int led, int value) { |
melangeaddict | 6:46fb12f6ace1 | 606 | //ledBlue=1; |
melangeaddict | 6:46fb12f6ace1 | 607 | //ledRed=2; |
melangeaddict | 6:46fb12f6ace1 | 608 | //ledGreen=3; |
melangeaddict | 8:651fbf5ae98a | 609 | //value must be an int: 0(on) or 1(off) |
melangeaddict | 9:2a211133e79a | 610 | |
melangeaddict | 8:651fbf5ae98a | 611 | if (value==0 || value==1) |
melangeaddict | 6:46fb12f6ace1 | 612 | |
melangeaddict | 7:057bcd99a3f8 | 613 | switch (led) { |
melangeaddict | 7:057bcd99a3f8 | 614 | case(1): |
melangeaddict | 7:057bcd99a3f8 | 615 | ledBlue=value; |
melangeaddict | 7:057bcd99a3f8 | 616 | break; |
melangeaddict | 6:46fb12f6ace1 | 617 | |
melangeaddict | 7:057bcd99a3f8 | 618 | case(2): |
melangeaddict | 7:057bcd99a3f8 | 619 | ledRed=value; |
melangeaddict | 7:057bcd99a3f8 | 620 | break; |
melangeaddict | 6:46fb12f6ace1 | 621 | |
melangeaddict | 7:057bcd99a3f8 | 622 | case(3): |
melangeaddict | 7:057bcd99a3f8 | 623 | ledGreen=value; |
melangeaddict | 7:057bcd99a3f8 | 624 | break; |
melangeaddict | 6:46fb12f6ace1 | 625 | |
melangeaddict | 7:057bcd99a3f8 | 626 | default: |
melangeaddict | 7:057bcd99a3f8 | 627 | break; |
melangeaddict | 6:46fb12f6ace1 | 628 | |
melangeaddict | 7:057bcd99a3f8 | 629 | } |
melangeaddict | 6:46fb12f6ace1 | 630 | |
melangeaddict | 6:46fb12f6ace1 | 631 | return; |
melangeaddict | 6:46fb12f6ace1 | 632 | |
melangeaddict | 12:03c189de6e2e | 633 | } |
melangeaddict | 12:03c189de6e2e | 634 | //*********************************** |
melangeaddict | 12:03c189de6e2e | 635 | //********get_steps***************** |
melangeaddict | 12:03c189de6e2e | 636 | int get_steps(float desired, float current) { |
melangeaddict | 12:03c189de6e2e | 637 | |
melangeaddict | 12:03c189de6e2e | 638 | float difference=abs(desired-current); |
melangeaddict | 12:03c189de6e2e | 639 | |
melangeaddict | 12:03c189de6e2e | 640 | switch (selected_string) { |
melangeaddict | 12:03c189de6e2e | 641 | case 5: |
melangeaddict | 12:03c189de6e2e | 642 | if (difference>10) { |
melangeaddict | 12:03c189de6e2e | 643 | return 60; |
melangeaddict | 12:03c189de6e2e | 644 | } else if (difference>3) { |
melangeaddict | 12:03c189de6e2e | 645 | return 35; |
melangeaddict | 13:948c7d19acb9 | 646 | } else if (difference>2) { |
melangeaddict | 13:948c7d19acb9 | 647 | return 20; |
melangeaddict | 12:03c189de6e2e | 648 | } else if (difference>1) { |
melangeaddict | 12:03c189de6e2e | 649 | return 15; |
melangeaddict | 12:03c189de6e2e | 650 | } else { |
melangeaddict | 12:03c189de6e2e | 651 | return 10; |
melangeaddict | 12:03c189de6e2e | 652 | } |
melangeaddict | 12:03c189de6e2e | 653 | case 4: |
melangeaddict | 13:948c7d19acb9 | 654 | if (difference>5) { |
melangeaddict | 12:03c189de6e2e | 655 | return 60; |
melangeaddict | 12:03c189de6e2e | 656 | } else if (difference>3) { |
melangeaddict | 12:03c189de6e2e | 657 | return 40; |
melangeaddict | 13:948c7d19acb9 | 658 | } else if (difference>2) { |
melangeaddict | 13:948c7d19acb9 | 659 | return 30; |
melangeaddict | 12:03c189de6e2e | 660 | } else if (difference>1) { |
melangeaddict | 12:03c189de6e2e | 661 | return 23; |
melangeaddict | 12:03c189de6e2e | 662 | } else { |
melangeaddict | 12:03c189de6e2e | 663 | return 10; |
melangeaddict | 12:03c189de6e2e | 664 | } |
melangeaddict | 12:03c189de6e2e | 665 | case 3: |
melangeaddict | 13:948c7d19acb9 | 666 | if (difference>5) { |
melangeaddict | 12:03c189de6e2e | 667 | return 40; |
melangeaddict | 12:03c189de6e2e | 668 | } else if (difference>3) { |
melangeaddict | 12:03c189de6e2e | 669 | return 25; |
melangeaddict | 13:948c7d19acb9 | 670 | } else if (difference>2) { |
melangeaddict | 13:948c7d19acb9 | 671 | return 20; |
melangeaddict | 12:03c189de6e2e | 672 | } else if (difference>1) { |
melangeaddict | 12:03c189de6e2e | 673 | return 15; |
melangeaddict | 12:03c189de6e2e | 674 | } else { |
melangeaddict | 12:03c189de6e2e | 675 | return 10; |
melangeaddict | 12:03c189de6e2e | 676 | } |
melangeaddict | 12:03c189de6e2e | 677 | case 2: |
melangeaddict | 13:948c7d19acb9 | 678 | if (difference>5) { |
melangeaddict | 12:03c189de6e2e | 679 | return 33; |
melangeaddict | 12:03c189de6e2e | 680 | } else if (difference>3) { |
melangeaddict | 13:948c7d19acb9 | 681 | return 23; |
melangeaddict | 13:948c7d19acb9 | 682 | } else if (difference>2) { |
melangeaddict | 13:948c7d19acb9 | 683 | return 17; |
melangeaddict | 12:03c189de6e2e | 684 | } else if (difference>1) { |
melangeaddict | 13:948c7d19acb9 | 685 | return 12; |
melangeaddict | 12:03c189de6e2e | 686 | } else { |
melangeaddict | 13:948c7d19acb9 | 687 | return 8; |
melangeaddict | 12:03c189de6e2e | 688 | } |
melangeaddict | 12:03c189de6e2e | 689 | case 1: |
melangeaddict | 13:948c7d19acb9 | 690 | if (difference>5) { |
melangeaddict | 13:948c7d19acb9 | 691 | return 35; |
melangeaddict | 12:03c189de6e2e | 692 | } else if (difference>3) { |
melangeaddict | 13:948c7d19acb9 | 693 | return 25; |
melangeaddict | 13:948c7d19acb9 | 694 | } else if (difference>2) { |
melangeaddict | 12:03c189de6e2e | 695 | return 20; |
melangeaddict | 12:03c189de6e2e | 696 | } else if (difference>1) { |
melangeaddict | 12:03c189de6e2e | 697 | return 10; |
melangeaddict | 12:03c189de6e2e | 698 | } else { |
melangeaddict | 13:948c7d19acb9 | 699 | return 5; |
melangeaddict | 12:03c189de6e2e | 700 | } |
melangeaddict | 12:03c189de6e2e | 701 | case 0: |
melangeaddict | 13:948c7d19acb9 | 702 | if (difference>8) { |
melangeaddict | 13:948c7d19acb9 | 703 | return 30; |
melangeaddict | 13:948c7d19acb9 | 704 | } else if (difference>6) { |
melangeaddict | 12:03c189de6e2e | 705 | return 20; |
melangeaddict | 13:948c7d19acb9 | 706 | } else if (difference>4) { |
melangeaddict | 12:03c189de6e2e | 707 | return 15; |
melangeaddict | 12:03c189de6e2e | 708 | } else if (difference>1) { |
melangeaddict | 13:948c7d19acb9 | 709 | return 10; |
melangeaddict | 12:03c189de6e2e | 710 | } else { |
melangeaddict | 13:948c7d19acb9 | 711 | return 5; |
melangeaddict | 12:03c189de6e2e | 712 | } |
melangeaddict | 12:03c189de6e2e | 713 | |
melangeaddict | 12:03c189de6e2e | 714 | |
melangeaddict | 12:03c189de6e2e | 715 | default: |
melangeaddict | 12:03c189de6e2e | 716 | return 0; |
melangeaddict | 12:03c189de6e2e | 717 | }//end switch |
melangeaddict | 6:46fb12f6ace1 | 718 | } |