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