A program to automatically tune a guitar. Written by Justin Reidhead and Steven Swenson
Dependencies: FFT FrequencyFinder Motor NewTextLCD PinDetect mbed strings
main.cpp@12:03c189de6e2e, 2012-04-25 (annotated)
- Committer:
- melangeaddict
- Date:
- Wed Apr 25 04:09:25 2012 +0000
- Revision:
- 12:03c189de6e2e
- Parent:
- 11:bdad1acccdad
- Child:
- 13:948c7d19acb9
BOO YAAAAH slut;
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 | 8:651fbf5ae98a | 81 | |
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 | 2:9c0a83c5ded5 | 132 | if ((desired_freq-.5)<new_freq && (desired_freq+.5)>new_freq) {//We are within .5Hz of the desired frequency |
melangeaddict | 11:bdad1acccdad | 133 | |
melangeaddict | 12:03c189de6e2e | 134 | new_freq=guitar.find_frequency(); |
melangeaddict | 12:03c189de6e2e | 135 | if ((desired_freq-.5)>new_freq || (desired_freq+.5)<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 | 11:bdad1acccdad | 140 | |
melangeaddict | 2:9c0a83c5ded5 | 141 | lcd.cls(); |
melangeaddict | 5:c0fd99f07536 | 142 | lcd.printf("String %d\ntuned",selected_string+1); |
melangeaddict | 8:651fbf5ae98a | 143 | set_LED(1,0);//blue on |
melangeaddict | 8:651fbf5ae98a | 144 | set_LED(2,1);//red off; |
melangeaddict | 8:651fbf5ae98a | 145 | set_LED(3,1);//green off |
melangeaddict | 12:03c189de6e2e | 146 | wait(1); |
melangeaddict | 2:9c0a83c5ded5 | 147 | |
melangeaddict | 2:9c0a83c5ded5 | 148 | start_tuning=false; |
melangeaddict | 5:c0fd99f07536 | 149 | output_menu(); |
melangeaddict | 5:c0fd99f07536 | 150 | setup_buttons(); |
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 | 12:03c189de6e2e | 154 | num_steps+=18;//For deadband |
melangeaddict | 12:03c189de6e2e | 155 | current_direction=up; |
melangeaddict | 12:03c189de6e2e | 156 | } |
melangeaddict | 12:03c189de6e2e | 157 | |
melangeaddict | 8:651fbf5ae98a | 158 | lcd.cls(); |
melangeaddict | 8:651fbf5ae98a | 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 | 12:03c189de6e2e | 174 | num_steps+=18;//For deadband |
melangeaddict | 12:03c189de6e2e | 175 | current_direction=down; |
melangeaddict | 12:03c189de6e2e | 176 | } |
melangeaddict | 12:03c189de6e2e | 177 | |
melangeaddict | 8:651fbf5ae98a | 178 | lcd.cls(); |
melangeaddict | 8:651fbf5ae98a | 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 | 3:c672e782f19b | 206 | if (current_mode==winding_mode) { |
melangeaddict | 3:c672e782f19b | 207 | if (wind_up) { |
melangeaddict | 3:c672e782f19b | 208 | motor.motor_turn(up,5);//TODO:Adjust number of turns |
melangeaddict | 3:c672e782f19b | 209 | } |
melangeaddict | 3:c672e782f19b | 210 | |
melangeaddict | 3:c672e782f19b | 211 | if (wind_down) { |
melangeaddict | 3:c672e782f19b | 212 | motor.motor_turn(down,5);//TODO:Adjust number of turns |
melangeaddict | 3:c672e782f19b | 213 | } |
melangeaddict | 3:c672e782f19b | 214 | next_state=4; |
melangeaddict | 3:c672e782f19b | 215 | } else { |
melangeaddict | 7:057bcd99a3f8 | 216 | output_menu(); |
melangeaddict | 7:057bcd99a3f8 | 217 | wind_up=false; |
melangeaddict | 7:057bcd99a3f8 | 218 | wind_down=false; |
melangeaddict | 7:057bcd99a3f8 | 219 | setup_buttons(); |
melangeaddict | 11:bdad1acccdad | 220 | //set_LED(1,0); |
melangeaddict | 3:c672e782f19b | 221 | next_state=0; |
melangeaddict | 3:c672e782f19b | 222 | } |
melangeaddict | 3:c672e782f19b | 223 | break; |
melangeaddict | 2:9c0a83c5ded5 | 224 | default: |
melangeaddict | 2:9c0a83c5ded5 | 225 | break; |
melangeaddict | 3:c672e782f19b | 226 | }//end switch |
melangeaddict | 2:9c0a83c5ded5 | 227 | wait_ms(5); |
melangeaddict | 2:9c0a83c5ded5 | 228 | |
melangeaddict | 2:9c0a83c5ded5 | 229 | if (start_tuning==false) {//If the stop button is pressed, the state machine returns to user input |
melangeaddict | 2:9c0a83c5ded5 | 230 | next_state=0; |
melangeaddict | 4:e370f322a697 | 231 | //setup_buttons(); |
melangeaddict | 4:e370f322a697 | 232 | //output_menu(); |
melangeaddict | 2:9c0a83c5ded5 | 233 | } |
melangeaddict | 3:c672e782f19b | 234 | |
melangeaddict | 3:c672e782f19b | 235 | if (current_mode==winding_mode) {//TODO:Debug this |
melangeaddict | 3:c672e782f19b | 236 | next_state=4; |
melangeaddict | 3:c672e782f19b | 237 | } |
melangeaddict | 3:c672e782f19b | 238 | |
melangeaddict | 3:c672e782f19b | 239 | if (start_tuning) { |
melangeaddict | 12:03c189de6e2e | 240 | if (check_threshold(new_freq)) { |
melangeaddict | 12:03c189de6e2e | 241 | lcd.cls(); |
melangeaddict | 12:03c189de6e2e | 242 | lcd.locate(9,0); |
melangeaddict | 12:03c189de6e2e | 243 | lcd.printf("%f",new_freq); |
melangeaddict | 12:03c189de6e2e | 244 | lcd.locate(9,1); |
melangeaddict | 12:03c189de6e2e | 245 | lcd.printf("%f",desired_freq); |
melangeaddict | 7:057bcd99a3f8 | 246 | |
melangeaddict | 12:03c189de6e2e | 247 | lcd.locate(0,1); |
melangeaddict | 12:03c189de6e2e | 248 | lcd.printf("Desired"); |
melangeaddict | 12:03c189de6e2e | 249 | lcd.locate(0,0); |
melangeaddict | 12:03c189de6e2e | 250 | lcd.printf("Detected"); |
melangeaddict | 12:03c189de6e2e | 251 | } |
melangeaddict | 3:c672e782f19b | 252 | } |
melangeaddict | 3:c672e782f19b | 253 | }//end while |
melangeaddict | 2:9c0a83c5ded5 | 254 | |
melangeaddict | 2:9c0a83c5ded5 | 255 | // return 0; |
melangeaddict | 2:9c0a83c5ded5 | 256 | } |
melangeaddict | 2:9c0a83c5ded5 | 257 | |
melangeaddict | 2:9c0a83c5ded5 | 258 | //*************************************************** |
melangeaddict | 2:9c0a83c5ded5 | 259 | //******************functions************************ |
melangeaddict | 3:c672e782f19b | 260 | //Display the string and pitch selection menu |
melangeaddict | 2:9c0a83c5ded5 | 261 | void output_menu() { |
melangeaddict | 2:9c0a83c5ded5 | 262 | lcd.cls(); |
melangeaddict | 4:e370f322a697 | 263 | lcd.printf("Select String: %d",selected_string+1); |
melangeaddict | 4:e370f322a697 | 264 | |
melangeaddict | 3:c672e782f19b | 265 | lcd.locate(0,1); |
melangeaddict | 3:c672e782f19b | 266 | strings *temp=&strings_array[selected_string]; |
melangeaddict | 3:c672e782f19b | 267 | lcd.printf("Select Pitch: "); |
melangeaddict | 5:c0fd99f07536 | 268 | lcd.printf("%s",temp->get_note()); |
melangeaddict | 5:c0fd99f07536 | 269 | //wait(.5); |
melangeaddict | 12:03c189de6e2e | 270 | |
melangeaddict | 11:bdad1acccdad | 271 | set_LED(1,0);//Turn blue LED on |
melangeaddict | 11:bdad1acccdad | 272 | set_LED(2,1); |
melangeaddict | 11:bdad1acccdad | 273 | set_LED(3,1); |
melangeaddict | 2:9c0a83c5ded5 | 274 | } |
melangeaddict | 2:9c0a83c5ded5 | 275 | |
melangeaddict | 2:9c0a83c5ded5 | 276 | //*************************************************** |
melangeaddict | 3:c672e782f19b | 277 | //Initialize the buttons |
melangeaddict | 2:9c0a83c5ded5 | 278 | void button_init() { |
melangeaddict | 2:9c0a83c5ded5 | 279 | string_but.mode( PullDown ); |
melangeaddict | 2:9c0a83c5ded5 | 280 | string_but.setSampleFrequency(); |
melangeaddict | 2:9c0a83c5ded5 | 281 | |
melangeaddict | 2:9c0a83c5ded5 | 282 | pitch_but.mode( PullDown ); |
melangeaddict | 2:9c0a83c5ded5 | 283 | pitch_but.setSampleFrequency(); |
melangeaddict | 2:9c0a83c5ded5 | 284 | |
melangeaddict | 2:9c0a83c5ded5 | 285 | start_but.mode( PullDown ); |
melangeaddict | 2:9c0a83c5ded5 | 286 | start_but.setSampleFrequency(); |
melangeaddict | 2:9c0a83c5ded5 | 287 | |
melangeaddict | 2:9c0a83c5ded5 | 288 | mode_but.mode( PullDown ); |
melangeaddict | 2:9c0a83c5ded5 | 289 | mode_but.setSampleFrequency(); |
melangeaddict | 4:e370f322a697 | 290 | |
melangeaddict | 4:e370f322a697 | 291 | setup_buttons(); |
melangeaddict | 2:9c0a83c5ded5 | 292 | } |
melangeaddict | 2:9c0a83c5ded5 | 293 | |
melangeaddict | 2:9c0a83c5ded5 | 294 | //*************************************************** |
melangeaddict | 3:c672e782f19b | 295 | //Depending on the current mode, the buttons do different things |
melangeaddict | 2:9c0a83c5ded5 | 296 | void setup_buttons() { |
melangeaddict | 3:c672e782f19b | 297 | if (current_mode==tuning_mode) {//Tuning mode |
melangeaddict | 2:9c0a83c5ded5 | 298 | string_but.attach_asserted(&string_sel); |
melangeaddict | 3:c672e782f19b | 299 | string_but.attach_deasserted(&do_nothing); |
melangeaddict | 2:9c0a83c5ded5 | 300 | pitch_but.attach_asserted(&pitch_sel); |
melangeaddict | 3:c672e782f19b | 301 | pitch_but.attach_deasserted(&do_nothing); |
melangeaddict | 2:9c0a83c5ded5 | 302 | start_but.attach_asserted(&start); |
melangeaddict | 2:9c0a83c5ded5 | 303 | mode_but.attach_asserted(&mode); |
melangeaddict | 3:c672e782f19b | 304 | } else {//Winding mode |
melangeaddict | 3:c672e782f19b | 305 | string_but.attach_asserted(&wind_up_start); |
melangeaddict | 3:c672e782f19b | 306 | string_but.attach_deasserted(&wind_up_stop); |
melangeaddict | 3:c672e782f19b | 307 | pitch_but.attach_asserted(&wind_down_start); |
melangeaddict | 3:c672e782f19b | 308 | pitch_but.attach_deasserted(&wind_down_stop); |
melangeaddict | 2:9c0a83c5ded5 | 309 | start_but.attach_asserted(&do_nothing); |
melangeaddict | 2:9c0a83c5ded5 | 310 | mode_but.attach_asserted(&mode); |
melangeaddict | 2:9c0a83c5ded5 | 311 | } |
melangeaddict | 2:9c0a83c5ded5 | 312 | } |
melangeaddict | 2:9c0a83c5ded5 | 313 | //*************************************************** |
melangeaddict | 3:c672e782f19b | 314 | //Change the selected string - there are only six strings |
melangeaddict | 2:9c0a83c5ded5 | 315 | void string_sel() { |
melangeaddict | 8:651fbf5ae98a | 316 | selected_string--; |
melangeaddict | 8:651fbf5ae98a | 317 | if (selected_string<0) |
melangeaddict | 8:651fbf5ae98a | 318 | selected_string=5; |
melangeaddict | 2:9c0a83c5ded5 | 319 | |
melangeaddict | 5:c0fd99f07536 | 320 | strings *temp=&strings_array[selected_string]; |
melangeaddict | 5:c0fd99f07536 | 321 | temp->reset_index(); |
melangeaddict | 5:c0fd99f07536 | 322 | |
melangeaddict | 2:9c0a83c5ded5 | 323 | output_menu(); |
melangeaddict | 2:9c0a83c5ded5 | 324 | } |
melangeaddict | 2:9c0a83c5ded5 | 325 | //*************************************************** |
melangeaddict | 3:c672e782f19b | 326 | //Change the pitch of the selected string |
melangeaddict | 2:9c0a83c5ded5 | 327 | void pitch_sel() { |
melangeaddict | 2:9c0a83c5ded5 | 328 | strings *temp=&strings_array[selected_string]; |
melangeaddict | 2:9c0a83c5ded5 | 329 | temp->inc_index(); |
melangeaddict | 2:9c0a83c5ded5 | 330 | |
melangeaddict | 2:9c0a83c5ded5 | 331 | output_menu(); |
melangeaddict | 2:9c0a83c5ded5 | 332 | } |
melangeaddict | 2:9c0a83c5ded5 | 333 | //*************************************************** |
melangeaddict | 3:c672e782f19b | 334 | //Start the tuning process |
melangeaddict | 2:9c0a83c5ded5 | 335 | void start() { |
melangeaddict | 2:9c0a83c5ded5 | 336 | start_tuning=true; |
melangeaddict | 3:c672e782f19b | 337 | |
melangeaddict | 3:c672e782f19b | 338 | string_but.attach_asserted(&do_nothing);//Disable the other buttons |
melangeaddict | 5:c0fd99f07536 | 339 | string_but.attach_deasserted(&do_nothing); |
melangeaddict | 3:c672e782f19b | 340 | pitch_but.attach_asserted(&do_nothing); |
melangeaddict | 5:c0fd99f07536 | 341 | pitch_but.attach_deasserted(&do_nothing); |
melangeaddict | 3:c672e782f19b | 342 | start_but.attach_asserted(&stop); //except for the start/stop button |
melangeaddict | 3:c672e782f19b | 343 | mode_but.attach_asserted(&do_nothing); |
melangeaddict | 2:9c0a83c5ded5 | 344 | } |
melangeaddict | 2:9c0a83c5ded5 | 345 | //*************************************************** |
melangeaddict | 2:9c0a83c5ded5 | 346 | void mode() { |
melangeaddict | 2:9c0a83c5ded5 | 347 | if (current_mode==tuning_mode) { |
melangeaddict | 2:9c0a83c5ded5 | 348 | current_mode=winding_mode; |
melangeaddict | 2:9c0a83c5ded5 | 349 | lcd.cls(); |
melangeaddict | 2:9c0a83c5ded5 | 350 | lcd.printf("Winding Mode"); |
melangeaddict | 4:e370f322a697 | 351 | wait(1); |
melangeaddict | 4:e370f322a697 | 352 | lcd.cls(); |
melangeaddict | 5:c0fd99f07536 | 353 | lcd.printf("String for up\nPitch for down"); |
melangeaddict | 2:9c0a83c5ded5 | 354 | } else { |
melangeaddict | 2:9c0a83c5ded5 | 355 | current_mode=tuning_mode; |
melangeaddict | 2:9c0a83c5ded5 | 356 | lcd.cls(); |
melangeaddict | 2:9c0a83c5ded5 | 357 | lcd.printf("Tuning Mode"); |
melangeaddict | 3:c672e782f19b | 358 | wait(.5); |
melangeaddict | 4:e370f322a697 | 359 | output_menu(); |
melangeaddict | 2:9c0a83c5ded5 | 360 | } |
melangeaddict | 3:c672e782f19b | 361 | setup_buttons();//Change the functions the buttons connect to |
melangeaddict | 2:9c0a83c5ded5 | 362 | } |
melangeaddict | 2:9c0a83c5ded5 | 363 | //*************************************************** |
melangeaddict | 2:9c0a83c5ded5 | 364 | void stop() { |
melangeaddict | 2:9c0a83c5ded5 | 365 | start_tuning=false; |
melangeaddict | 7:057bcd99a3f8 | 366 | |
melangeaddict | 7:057bcd99a3f8 | 367 | output_menu(); |
melangeaddict | 7:057bcd99a3f8 | 368 | setup_buttons(); |
melangeaddict | 2:9c0a83c5ded5 | 369 | } |
melangeaddict | 2:9c0a83c5ded5 | 370 | //*************************************************** |
melangeaddict | 2:9c0a83c5ded5 | 371 | void do_nothing() { |
melangeaddict | 2:9c0a83c5ded5 | 372 | return; |
melangeaddict | 2:9c0a83c5ded5 | 373 | } |
melangeaddict | 2:9c0a83c5ded5 | 374 | //*************************************************** |
melangeaddict | 3:c672e782f19b | 375 | void wind_up_start() { |
melangeaddict | 3:c672e782f19b | 376 | wind_up=true; |
melangeaddict | 2:9c0a83c5ded5 | 377 | } |
melangeaddict | 2:9c0a83c5ded5 | 378 | //*************************************************** |
melangeaddict | 3:c672e782f19b | 379 | void wind_up_stop() { |
melangeaddict | 3:c672e782f19b | 380 | wind_up=false; |
melangeaddict | 3:c672e782f19b | 381 | } |
melangeaddict | 3:c672e782f19b | 382 | //*************************************************** |
melangeaddict | 3:c672e782f19b | 383 | void wind_down_start() { |
melangeaddict | 3:c672e782f19b | 384 | wind_down=true; |
melangeaddict | 3:c672e782f19b | 385 | } |
melangeaddict | 3:c672e782f19b | 386 | //*************************************************** |
melangeaddict | 3:c672e782f19b | 387 | void wind_down_stop() { |
melangeaddict | 3:c672e782f19b | 388 | wind_down=false; |
melangeaddict | 2:9c0a83c5ded5 | 389 | } |
melangeaddict | 2:9c0a83c5ded5 | 390 | //*************************************************** |
melangeaddict | 2:9c0a83c5ded5 | 391 | void device_init() { |
melangeaddict | 3:c672e782f19b | 392 | |
melangeaddict | 2:9c0a83c5ded5 | 393 | strings string1(1); |
melangeaddict | 2:9c0a83c5ded5 | 394 | strings string2(2); |
melangeaddict | 2:9c0a83c5ded5 | 395 | strings string3(3); |
melangeaddict | 2:9c0a83c5ded5 | 396 | strings string4(4); |
melangeaddict | 2:9c0a83c5ded5 | 397 | strings string5(5); |
melangeaddict | 2:9c0a83c5ded5 | 398 | strings string6(6); |
melangeaddict | 2:9c0a83c5ded5 | 399 | |
melangeaddict | 3:c672e782f19b | 400 | strings_array[0]=string1; |
melangeaddict | 3:c672e782f19b | 401 | strings_array[1]=string2; |
melangeaddict | 3:c672e782f19b | 402 | strings_array[2]=string3; |
melangeaddict | 3:c672e782f19b | 403 | strings_array[3]=string4; |
melangeaddict | 3:c672e782f19b | 404 | strings_array[4]=string5; |
melangeaddict | 3:c672e782f19b | 405 | strings_array[5]=string6; |
melangeaddict | 3:c672e782f19b | 406 | |
melangeaddict | 4:e370f322a697 | 407 | selected_string=5; |
melangeaddict | 2:9c0a83c5ded5 | 408 | current_mode=tuning_mode; |
melangeaddict | 2:9c0a83c5ded5 | 409 | start_tuning=false; |
melangeaddict | 7:057bcd99a3f8 | 410 | up=1; |
melangeaddict | 7:057bcd99a3f8 | 411 | down=0; |
melangeaddict | 7:057bcd99a3f8 | 412 | wind_up=false; |
melangeaddict | 7:057bcd99a3f8 | 413 | wind_down=false; |
melangeaddict | 7:057bcd99a3f8 | 414 | bias=.5; |
melangeaddict | 2:9c0a83c5ded5 | 415 | |
melangeaddict | 2:9c0a83c5ded5 | 416 | button_init(); |
melangeaddict | 2:9c0a83c5ded5 | 417 | |
melangeaddict | 2:9c0a83c5ded5 | 418 | output_menu(); |
melangeaddict | 3:c672e782f19b | 419 | |
melangeaddict | 2:9c0a83c5ded5 | 420 | } |
melangeaddict | 2:9c0a83c5ded5 | 421 | //*************************************************** |
melangeaddict | 2:9c0a83c5ded5 | 422 | void motor_calibration() { |
melangeaddict | 2:9c0a83c5ded5 | 423 | lcd.cls(); |
melangeaddict | 2:9c0a83c5ded5 | 424 | lcd.printf("Calibrate Motor"); |
melangeaddict | 3:c672e782f19b | 425 | wait(.5); |
melangeaddict | 2:9c0a83c5ded5 | 426 | |
melangeaddict | 2:9c0a83c5ded5 | 427 | float freq=0, freq_up=0, freq_down=0; |
melangeaddict | 2:9c0a83c5ded5 | 428 | bool done=false; |
melangeaddict | 2:9c0a83c5ded5 | 429 | lcd.cls(); |
melangeaddict | 2:9c0a83c5ded5 | 430 | lcd.printf("Please pluck\nstring"); |
melangeaddict | 4:e370f322a697 | 431 | wait(1); |
melangeaddict | 2:9c0a83c5ded5 | 432 | |
melangeaddict | 2:9c0a83c5ded5 | 433 | //motor.motor_turn(up,25)//TODO: Adjust the number of steps here |
melangeaddict | 2:9c0a83c5ded5 | 434 | //On second thought, we don't need to tune up and down for this, we can find the current frequency |
melangeaddict | 2:9c0a83c5ded5 | 435 | //and then turn the peg for the two frequencies! |
melangeaddict | 2:9c0a83c5ded5 | 436 | while (!done) { |
melangeaddict | 2:9c0a83c5ded5 | 437 | freq=guitar.find_frequency(); |
melangeaddict | 2:9c0a83c5ded5 | 438 | |
melangeaddict | 2:9c0a83c5ded5 | 439 | if (check_threshold(freq)) { |
melangeaddict | 3:c672e782f19b | 440 | lcd.cls(); |
melangeaddict | 2:9c0a83c5ded5 | 441 | freq_up=freq; |
melangeaddict | 2:9c0a83c5ded5 | 442 | done=true; |
melangeaddict | 2:9c0a83c5ded5 | 443 | } |
melangeaddict | 4:e370f322a697 | 444 | |
melangeaddict | 4:e370f322a697 | 445 | if (start_tuning==false) |
melangeaddict | 4:e370f322a697 | 446 | break; |
melangeaddict | 2:9c0a83c5ded5 | 447 | } |
melangeaddict | 2:9c0a83c5ded5 | 448 | motor.motor_turn(down,25);//TODO: Adjust the number of steps here |
melangeaddict | 2:9c0a83c5ded5 | 449 | done=false; |
melangeaddict | 2:9c0a83c5ded5 | 450 | while (!done) { |
melangeaddict | 2:9c0a83c5ded5 | 451 | freq=guitar.find_frequency(); |
melangeaddict | 2:9c0a83c5ded5 | 452 | |
melangeaddict | 2:9c0a83c5ded5 | 453 | if (check_threshold(freq)) { |
melangeaddict | 3:c672e782f19b | 454 | lcd.cls(); |
melangeaddict | 2:9c0a83c5ded5 | 455 | freq_down=freq; |
melangeaddict | 2:9c0a83c5ded5 | 456 | done=true; |
melangeaddict | 2:9c0a83c5ded5 | 457 | } |
melangeaddict | 4:e370f322a697 | 458 | if (start_tuning==false) |
melangeaddict | 4:e370f322a697 | 459 | break; |
melangeaddict | 2:9c0a83c5ded5 | 460 | } |
melangeaddict | 2:9c0a83c5ded5 | 461 | |
melangeaddict | 2:9c0a83c5ded5 | 462 | if (freq_up<freq_down) { |
melangeaddict | 8:651fbf5ae98a | 463 | //down=0; |
melangeaddict | 8:651fbf5ae98a | 464 | //up=1; |
melangeaddict | 8:651fbf5ae98a | 465 | } else { |
melangeaddict | 8:651fbf5ae98a | 466 | //down=0; |
melangeaddict | 8:651fbf5ae98a | 467 | //up=1; |
melangeaddict | 2:9c0a83c5ded5 | 468 | } |
melangeaddict | 2:9c0a83c5ded5 | 469 | |
melangeaddict | 2:9c0a83c5ded5 | 470 | lcd.cls(); |
melangeaddict | 2:9c0a83c5ded5 | 471 | lcd.printf("Calibration Done"); |
melangeaddict | 4:e370f322a697 | 472 | |
melangeaddict | 4:e370f322a697 | 473 | if (start_tuning==false) { |
melangeaddict | 4:e370f322a697 | 474 | output_menu(); |
melangeaddict | 4:e370f322a697 | 475 | setup_buttons(); |
melangeaddict | 4:e370f322a697 | 476 | } |
melangeaddict | 2:9c0a83c5ded5 | 477 | } |
melangeaddict | 2:9c0a83c5ded5 | 478 | //********************************************** |
melangeaddict | 2:9c0a83c5ded5 | 479 | bool check_threshold(float freq) { |
melangeaddict | 9:2a211133e79a | 480 | strings *temp=&strings_array[selected_string]; |
melangeaddict | 9:2a211133e79a | 481 | float desired_freq=temp->get_freq();//Get the desired frequency for the string selected |
melangeaddict | 12:03c189de6e2e | 482 | |
melangeaddict | 12:03c189de6e2e | 483 | int hertz=0; |
melangeaddict | 12:03c189de6e2e | 484 | switch (selected_string) { |
melangeaddict | 12:03c189de6e2e | 485 | case 0: |
melangeaddict | 12:03c189de6e2e | 486 | hertz=85; |
melangeaddict | 12:03c189de6e2e | 487 | break; |
melangeaddict | 12:03c189de6e2e | 488 | |
melangeaddict | 12:03c189de6e2e | 489 | case 1: |
melangeaddict | 12:03c189de6e2e | 490 | hertz=66; |
melangeaddict | 12:03c189de6e2e | 491 | break; |
melangeaddict | 12:03c189de6e2e | 492 | |
melangeaddict | 12:03c189de6e2e | 493 | case 2: |
melangeaddict | 12:03c189de6e2e | 494 | hertz=53; |
melangeaddict | 12:03c189de6e2e | 495 | break; |
melangeaddict | 12:03c189de6e2e | 496 | |
melangeaddict | 12:03c189de6e2e | 497 | case 3: |
melangeaddict | 12:03c189de6e2e | 498 | hertz=42; |
melangeaddict | 12:03c189de6e2e | 499 | break; |
melangeaddict | 12:03c189de6e2e | 500 | |
melangeaddict | 12:03c189de6e2e | 501 | case 4: |
melangeaddict | 12:03c189de6e2e | 502 | hertz=29; |
melangeaddict | 12:03c189de6e2e | 503 | break; |
melangeaddict | 12:03c189de6e2e | 504 | |
melangeaddict | 12:03c189de6e2e | 505 | case 5: |
melangeaddict | 12:03c189de6e2e | 506 | hertz=22; |
melangeaddict | 12:03c189de6e2e | 507 | break; |
melangeaddict | 12:03c189de6e2e | 508 | |
melangeaddict | 12:03c189de6e2e | 509 | default: |
melangeaddict | 12:03c189de6e2e | 510 | break; |
melangeaddict | 12:03c189de6e2e | 511 | } |
melangeaddict | 12:03c189de6e2e | 512 | |
melangeaddict | 12:03c189de6e2e | 513 | if (freq>(desired_freq+hertz) || freq<(desired_freq-hertz) || freq>500) {//new_freq>(desired_freq+50) || new_freq<(desired_freq-50) |
melangeaddict | 2:9c0a83c5ded5 | 514 | lcd.cls(); |
melangeaddict | 11:bdad1acccdad | 515 | lcd.printf("Pluck string %d\nagain",selected_string+1); |
melangeaddict | 12:03c189de6e2e | 516 | |
melangeaddict | 11:bdad1acccdad | 517 | set_LED(1,1); |
melangeaddict | 11:bdad1acccdad | 518 | set_LED(2,1); |
melangeaddict | 11:bdad1acccdad | 519 | set_LED(3,1);//Turn led off |
melangeaddict | 12:03c189de6e2e | 520 | |
melangeaddict | 11:bdad1acccdad | 521 | set_LED(2,0); |
melangeaddict | 11:bdad1acccdad | 522 | set_LED(3,0);//Make the LED turn yellow |
melangeaddict | 12:03c189de6e2e | 523 | |
melangeaddict | 12:03c189de6e2e | 524 | //wait(.5); |
melangeaddict | 2:9c0a83c5ded5 | 525 | return false; |
melangeaddict | 2:9c0a83c5ded5 | 526 | } else |
melangeaddict | 2:9c0a83c5ded5 | 527 | return true; |
melangeaddict | 2:9c0a83c5ded5 | 528 | } |
melangeaddict | 6:46fb12f6ace1 | 529 | |
melangeaddict | 6:46fb12f6ace1 | 530 | |
melangeaddict | 6:46fb12f6ace1 | 531 | |
melangeaddict | 6:46fb12f6ace1 | 532 | void LED_initialize() { |
melangeaddict | 6:46fb12f6ace1 | 533 | float wait_time=0.3; |
melangeaddict | 9:2a211133e79a | 534 | /* ledBlue=1;//red |
melangeaddict | 9:2a211133e79a | 535 | ledGreen=1; |
melangeaddict | 9:2a211133e79a | 536 | ledRed=0; |
melangeaddict | 9:2a211133e79a | 537 | wait(wait_time); |
melangeaddict | 9:2a211133e79a | 538 | ledRed=1;//green |
melangeaddict | 6:46fb12f6ace1 | 539 | ledGreen=0; |
melangeaddict | 6:46fb12f6ace1 | 540 | wait(wait_time); |
melangeaddict | 6:46fb12f6ace1 | 541 | ledGreen=1; |
melangeaddict | 9:2a211133e79a | 542 | ledBlue=0;//blue |
melangeaddict | 6:46fb12f6ace1 | 543 | wait(wait_time); |
melangeaddict | 6:46fb12f6ace1 | 544 | |
melangeaddict | 9:2a211133e79a | 545 | for (int a=0; a<3; a++) { |
melangeaddict | 9:2a211133e79a | 546 | ledRed=0;//all |
melangeaddict | 9:2a211133e79a | 547 | ledGreen=0; |
melangeaddict | 9:2a211133e79a | 548 | ledBlue=0; |
melangeaddict | 9:2a211133e79a | 549 | wait(wait_time); |
melangeaddict | 9:2a211133e79a | 550 | ledRed=1; |
melangeaddict | 9:2a211133e79a | 551 | ledGreen=1; |
melangeaddict | 9:2a211133e79a | 552 | ledBlue=1; |
melangeaddict | 9:2a211133e79a | 553 | wait(wait_time); |
melangeaddict | 9:2a211133e79a | 554 | |
melangeaddict | 9:2a211133e79a | 555 | } |
melangeaddict | 9:2a211133e79a | 556 | ledBlue=0; |
melangeaddict | 6:46fb12f6ace1 | 557 | |
melangeaddict | 6:46fb12f6ace1 | 558 | |
melangeaddict | 9:2a211133e79a | 559 | ledRed=1;//blue |
melangeaddict | 9:2a211133e79a | 560 | ledGreen=1; |
melangeaddict | 9:2a211133e79a | 561 | */ |
melangeaddict | 9:2a211133e79a | 562 | set_LED(1,1); |
melangeaddict | 9:2a211133e79a | 563 | set_LED(2,1); |
melangeaddict | 9:2a211133e79a | 564 | set_LED(3,1); |
melangeaddict | 9:2a211133e79a | 565 | |
melangeaddict | 9:2a211133e79a | 566 | for (int b=1; b<4; b++) { |
melangeaddict | 9:2a211133e79a | 567 | for (int a=1; a<4; a++) { |
melangeaddict | 9:2a211133e79a | 568 | set_LED(a, 0); |
melangeaddict | 9:2a211133e79a | 569 | wait(wait_time); |
melangeaddict | 9:2a211133e79a | 570 | set_LED(a,1); |
melangeaddict | 9:2a211133e79a | 571 | wait(0.001); |
melangeaddict | 9:2a211133e79a | 572 | |
melangeaddict | 9:2a211133e79a | 573 | } |
melangeaddict | 9:2a211133e79a | 574 | } |
melangeaddict | 9:2a211133e79a | 575 | set_LED(1,0); |
melangeaddict | 9:2a211133e79a | 576 | set_LED(2,1); |
melangeaddict | 9:2a211133e79a | 577 | set_LED(3,1); |
melangeaddict | 6:46fb12f6ace1 | 578 | |
melangeaddict | 6:46fb12f6ace1 | 579 | return; |
melangeaddict | 6:46fb12f6ace1 | 580 | |
melangeaddict | 6:46fb12f6ace1 | 581 | } |
melangeaddict | 6:46fb12f6ace1 | 582 | |
melangeaddict | 6:46fb12f6ace1 | 583 | |
melangeaddict | 6:46fb12f6ace1 | 584 | void set_LED(int led, int value) { |
melangeaddict | 6:46fb12f6ace1 | 585 | //ledBlue=1; |
melangeaddict | 6:46fb12f6ace1 | 586 | //ledRed=2; |
melangeaddict | 6:46fb12f6ace1 | 587 | //ledGreen=3; |
melangeaddict | 8:651fbf5ae98a | 588 | //value must be an int: 0(on) or 1(off) |
melangeaddict | 9:2a211133e79a | 589 | |
melangeaddict | 8:651fbf5ae98a | 590 | if (value==0 || value==1) |
melangeaddict | 6:46fb12f6ace1 | 591 | |
melangeaddict | 7:057bcd99a3f8 | 592 | switch (led) { |
melangeaddict | 7:057bcd99a3f8 | 593 | case(1): |
melangeaddict | 7:057bcd99a3f8 | 594 | ledBlue=value; |
melangeaddict | 7:057bcd99a3f8 | 595 | break; |
melangeaddict | 6:46fb12f6ace1 | 596 | |
melangeaddict | 7:057bcd99a3f8 | 597 | case(2): |
melangeaddict | 7:057bcd99a3f8 | 598 | ledRed=value; |
melangeaddict | 7:057bcd99a3f8 | 599 | break; |
melangeaddict | 6:46fb12f6ace1 | 600 | |
melangeaddict | 7:057bcd99a3f8 | 601 | case(3): |
melangeaddict | 7:057bcd99a3f8 | 602 | ledGreen=value; |
melangeaddict | 7:057bcd99a3f8 | 603 | break; |
melangeaddict | 6:46fb12f6ace1 | 604 | |
melangeaddict | 7:057bcd99a3f8 | 605 | default: |
melangeaddict | 7:057bcd99a3f8 | 606 | break; |
melangeaddict | 6:46fb12f6ace1 | 607 | |
melangeaddict | 7:057bcd99a3f8 | 608 | } |
melangeaddict | 6:46fb12f6ace1 | 609 | |
melangeaddict | 6:46fb12f6ace1 | 610 | return; |
melangeaddict | 6:46fb12f6ace1 | 611 | |
melangeaddict | 12:03c189de6e2e | 612 | } |
melangeaddict | 12:03c189de6e2e | 613 | //*********************************** |
melangeaddict | 12:03c189de6e2e | 614 | //********get_steps***************** |
melangeaddict | 12:03c189de6e2e | 615 | int get_steps(float desired, float current) { |
melangeaddict | 12:03c189de6e2e | 616 | |
melangeaddict | 12:03c189de6e2e | 617 | float difference=abs(desired-current); |
melangeaddict | 12:03c189de6e2e | 618 | |
melangeaddict | 12:03c189de6e2e | 619 | switch (selected_string) { |
melangeaddict | 12:03c189de6e2e | 620 | case 5: |
melangeaddict | 12:03c189de6e2e | 621 | if (difference>10) { |
melangeaddict | 12:03c189de6e2e | 622 | return 60; |
melangeaddict | 12:03c189de6e2e | 623 | } else if (difference>3) { |
melangeaddict | 12:03c189de6e2e | 624 | return 35; |
melangeaddict | 12:03c189de6e2e | 625 | } else if (difference>1) { |
melangeaddict | 12:03c189de6e2e | 626 | return 15; |
melangeaddict | 12:03c189de6e2e | 627 | } else { |
melangeaddict | 12:03c189de6e2e | 628 | return 10; |
melangeaddict | 12:03c189de6e2e | 629 | } |
melangeaddict | 12:03c189de6e2e | 630 | case 4: |
melangeaddict | 12:03c189de6e2e | 631 | if (difference>10) { |
melangeaddict | 12:03c189de6e2e | 632 | return 60; |
melangeaddict | 12:03c189de6e2e | 633 | } else if (difference>3) { |
melangeaddict | 12:03c189de6e2e | 634 | return 40; |
melangeaddict | 12:03c189de6e2e | 635 | } else if (difference>1) { |
melangeaddict | 12:03c189de6e2e | 636 | return 23; |
melangeaddict | 12:03c189de6e2e | 637 | } else { |
melangeaddict | 12:03c189de6e2e | 638 | return 10; |
melangeaddict | 12:03c189de6e2e | 639 | } |
melangeaddict | 12:03c189de6e2e | 640 | case 3: |
melangeaddict | 12:03c189de6e2e | 641 | if (difference>10) { |
melangeaddict | 12:03c189de6e2e | 642 | return 40; |
melangeaddict | 12:03c189de6e2e | 643 | } else if (difference>3) { |
melangeaddict | 12:03c189de6e2e | 644 | return 25; |
melangeaddict | 12:03c189de6e2e | 645 | } else if (difference>1) { |
melangeaddict | 12:03c189de6e2e | 646 | return 15; |
melangeaddict | 12:03c189de6e2e | 647 | } else { |
melangeaddict | 12:03c189de6e2e | 648 | return 10; |
melangeaddict | 12:03c189de6e2e | 649 | } |
melangeaddict | 12:03c189de6e2e | 650 | case 2: |
melangeaddict | 12:03c189de6e2e | 651 | if (difference>10) { |
melangeaddict | 12:03c189de6e2e | 652 | return 33; |
melangeaddict | 12:03c189de6e2e | 653 | } else if (difference>3) { |
melangeaddict | 12:03c189de6e2e | 654 | return 20; |
melangeaddict | 12:03c189de6e2e | 655 | } else if (difference>1) { |
melangeaddict | 12:03c189de6e2e | 656 | return 15; |
melangeaddict | 12:03c189de6e2e | 657 | } else { |
melangeaddict | 12:03c189de6e2e | 658 | return 10; |
melangeaddict | 12:03c189de6e2e | 659 | } |
melangeaddict | 12:03c189de6e2e | 660 | case 1: |
melangeaddict | 12:03c189de6e2e | 661 | if (difference>10) { |
melangeaddict | 12:03c189de6e2e | 662 | return 30; |
melangeaddict | 12:03c189de6e2e | 663 | } else if (difference>3) { |
melangeaddict | 12:03c189de6e2e | 664 | return 20; |
melangeaddict | 12:03c189de6e2e | 665 | } else if (difference>1) { |
melangeaddict | 12:03c189de6e2e | 666 | return 10; |
melangeaddict | 12:03c189de6e2e | 667 | } else { |
melangeaddict | 12:03c189de6e2e | 668 | return 7; |
melangeaddict | 12:03c189de6e2e | 669 | } |
melangeaddict | 12:03c189de6e2e | 670 | case 0: |
melangeaddict | 12:03c189de6e2e | 671 | if (difference>10) { |
melangeaddict | 12:03c189de6e2e | 672 | return 20; |
melangeaddict | 12:03c189de6e2e | 673 | } else if (difference>3) { |
melangeaddict | 12:03c189de6e2e | 674 | return 15; |
melangeaddict | 12:03c189de6e2e | 675 | } else if (difference>1) { |
melangeaddict | 12:03c189de6e2e | 676 | return 8; |
melangeaddict | 12:03c189de6e2e | 677 | } else { |
melangeaddict | 12:03c189de6e2e | 678 | return 4; |
melangeaddict | 12:03c189de6e2e | 679 | } |
melangeaddict | 12:03c189de6e2e | 680 | |
melangeaddict | 12:03c189de6e2e | 681 | |
melangeaddict | 12:03c189de6e2e | 682 | default: |
melangeaddict | 12:03c189de6e2e | 683 | return 0; |
melangeaddict | 12:03c189de6e2e | 684 | break; |
melangeaddict | 12:03c189de6e2e | 685 | }//end switch |
melangeaddict | 6:46fb12f6ace1 | 686 | } |