ECE 4180 final project. Sound and Bluetooth activated coffee maker
Dependencies: mbed wave_player mbed-rtos C12832_lcd 4DGL-uLCD-SE LCD_fonts SDFileSystem
main.cpp@8:a9b5acc63388, 2019-12-06 (annotated)
- Committer:
- nhummel3
- Date:
- Fri Dec 06 13:10:47 2019 +0000
- Revision:
- 8:a9b5acc63388
- Parent:
- 7:2e274cc18797
Temperature control and GUI added;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
dreschpe | 2:a69c8c5f5b03 | 1 | // example to test the mbed Lab Board lcd lib with the mbed rtos |
4180_1 | 4:79863d2ea5a0 | 2 | // Pot1 changes the contrast |
4180_1 | 4:79863d2ea5a0 | 3 | // Pot2 changes the speed of the sin wave |
dreschpe | 1:1c6a9eaf55b5 | 4 | |
dreschpe | 0:f6a57b843f79 | 5 | #include "mbed.h" |
dreschpe | 0:f6a57b843f79 | 6 | #include "rtos.h" |
dreschpe | 0:f6a57b843f79 | 7 | #include "Small_6.h" |
dreschpe | 0:f6a57b843f79 | 8 | #include "Small_7.h" |
dreschpe | 0:f6a57b843f79 | 9 | #include "Arial_9.h" |
dreschpe | 0:f6a57b843f79 | 10 | #include "stdio.h" |
dreschpe | 1:1c6a9eaf55b5 | 11 | #include "C12832_lcd.h" |
nhummel3 | 5:18b2796e4847 | 12 | #include "sound.h" |
nhummel3 | 6:e4aba042979b | 13 | #include "unsigned_fire_truck.h" |
nhummel3 | 6:e4aba042979b | 14 | #include "uLCD_4DGL.h" |
nhummel3 | 6:e4aba042979b | 15 | #include <mpr121.h> |
dreschpe | 0:f6a57b843f79 | 16 | |
nhummel3 | 5:18b2796e4847 | 17 | #define sample_freq 11025.0 |
nhummel3 | 5:18b2796e4847 | 18 | //get and set the frequency from wav conversion tool GUI |
4180_1 | 4:79863d2ea5a0 | 19 | |
nhummel3 | 6:e4aba042979b | 20 | //set variables used to track throughout program |
nhummel3 | 6:e4aba042979b | 21 | int reset = 1; //check if coffee pot is reset and ready to brew again |
nhummel3 | 6:e4aba042979b | 22 | volatile int blink = 0; // check if blinking LED |
nhummel3 | 6:e4aba042979b | 23 | int tmp_blue = 0; // check if bluetooth has been triggered at least 2 times |
nhummel3 | 6:e4aba042979b | 24 | int noise = 0; // check if still playing alarm |
nhummel3 | 6:e4aba042979b | 25 | int i=0; //index for audio array |
nhummel3 | 6:e4aba042979b | 26 | int d = 0; |
nhummel3 | 6:e4aba042979b | 27 | volatile int trigger = 0; // check if coffeepot has been triggered via sound or bluetooth |
nhummel3 | 8:a9b5acc63388 | 28 | volatile int menu = 1; // check if coffeepot has been triggered via sound or bluetooth |
nhummel3 | 8:a9b5acc63388 | 29 | int coffee = 0; |
nhummel3 | 8:a9b5acc63388 | 30 | int tolerance = 600; |
nhummel3 | 8:a9b5acc63388 | 31 | int brew_count = 0; |
nhummel3 | 6:e4aba042979b | 32 | |
nhummel3 | 6:e4aba042979b | 33 | //Declarations |
nhummel3 | 6:e4aba042979b | 34 | Ticker sampletick; //audio interrupt |
nhummel3 | 6:e4aba042979b | 35 | uLCD_4DGL uLCD(p9,p10,p11); |
nhummel3 | 6:e4aba042979b | 36 | AnalogIn Therm0(p18); |
nhummel3 | 6:e4aba042979b | 37 | AnalogIn Therm1(p19); |
nhummel3 | 6:e4aba042979b | 38 | DigitalOut key(p20); |
nhummel3 | 6:e4aba042979b | 39 | |
nhummel3 | 8:a9b5acc63388 | 40 | Serial pc(USBTX,USBRX); |
4180_1 | 4:79863d2ea5a0 | 41 | PwmOut Speaker(p26); |
4180_1 | 4:79863d2ea5a0 | 42 | PwmOut RGBLED_r(p23); |
nhummel3 | 6:e4aba042979b | 43 | PwmOut RGBLED_g(p22); |
nhummel3 | 6:e4aba042979b | 44 | PwmOut RGBLED_b(p21); |
nhummel3 | 6:e4aba042979b | 45 | DigitalIn user_button(p12); |
nhummel3 | 6:e4aba042979b | 46 | Serial blue(p28,p27); |
dreschpe | 2:a69c8c5f5b03 | 47 | |
nhummel3 | 8:a9b5acc63388 | 48 | PwmOut pump(p24); |
nhummel3 | 8:a9b5acc63388 | 49 | DigitalOut heater(p14); //inverted logic |
nhummel3 | 8:a9b5acc63388 | 50 | AnalogIn therm_out(p18); |
nhummel3 | 8:a9b5acc63388 | 51 | AnalogIn therm_heater(p19); |
nhummel3 | 8:a9b5acc63388 | 52 | |
nhummel3 | 6:e4aba042979b | 53 | // Mutex declarations |
dreschpe | 1:1c6a9eaf55b5 | 54 | Mutex lcd_mutex; |
nhummel3 | 6:e4aba042979b | 55 | Mutex leds_mutex; |
nhummel3 | 6:e4aba042979b | 56 | Mutex bmut; |
nhummel3 | 6:e4aba042979b | 57 | Mutex mic_mutex; |
nhummel3 | 6:e4aba042979b | 58 | Mutex trigger_mutex; |
dreschpe | 0:f6a57b843f79 | 59 | |
nhummel3 | 8:a9b5acc63388 | 60 | int out_temp; |
nhummel3 | 8:a9b5acc63388 | 61 | int heater_temp; |
nhummel3 | 8:a9b5acc63388 | 62 | int out_therm; |
nhummel3 | 8:a9b5acc63388 | 63 | int heater_therm; |
nhummel3 | 8:a9b5acc63388 | 64 | int target_temp = 190; |
nhummel3 | 8:a9b5acc63388 | 65 | |
nhummel3 | 6:e4aba042979b | 66 | volatile float r = 0.0; |
nhummel3 | 6:e4aba042979b | 67 | volatile float g = 0.0; |
nhummel3 | 6:e4aba042979b | 68 | volatile float b = 0.0; |
nhummel3 | 6:e4aba042979b | 69 | |
nhummel3 | 8:a9b5acc63388 | 70 | float a_coeff = 93.6469; |
nhummel3 | 8:a9b5acc63388 | 71 | float b_coeff = 4681.5; |
nhummel3 | 8:a9b5acc63388 | 72 | |
nhummel3 | 8:a9b5acc63388 | 73 | int therm_to_temp(int therm) { |
nhummel3 | 8:a9b5acc63388 | 74 | return int(a_coeff*log(therm/b_coeff)); |
nhummel3 | 8:a9b5acc63388 | 75 | } |
nhummel3 | 8:a9b5acc63388 | 76 | |
nhummel3 | 8:a9b5acc63388 | 77 | int temp_to_therm(int temp) { |
nhummel3 | 8:a9b5acc63388 | 78 | return int(b_coeff*exp((temp)/a_coeff)); |
nhummel3 | 8:a9b5acc63388 | 79 | } |
nhummel3 | 8:a9b5acc63388 | 80 | |
nhummel3 | 6:e4aba042979b | 81 | //Audio ISR |
nhummel3 | 5:18b2796e4847 | 82 | void audio_sample () |
dreschpe | 0:f6a57b843f79 | 83 | { |
nhummel3 | 6:e4aba042979b | 84 | //if noise flag set, play audio |
nhummel3 | 6:e4aba042979b | 85 | if(noise){ |
nhummel3 | 6:e4aba042979b | 86 | Speaker = fire_truck_data[i%NUM_ELEMENTS_FIRE]/255.0;//scale to 0.0 to 1.0 for PWM |
nhummel3 | 6:e4aba042979b | 87 | i++; |
nhummel3 | 6:e4aba042979b | 88 | } else { |
nhummel3 | 6:e4aba042979b | 89 | Speaker = 0; |
dreschpe | 0:f6a57b843f79 | 90 | } |
dreschpe | 0:f6a57b843f79 | 91 | } |
dreschpe | 0:f6a57b843f79 | 92 | |
nhummel3 | 6:e4aba042979b | 93 | // Temporary debug variables |
nhummel3 | 6:e4aba042979b | 94 | int tmp = 0; |
nhummel3 | 8:a9b5acc63388 | 95 | int tmp_button = 0; |
nhummel3 | 6:e4aba042979b | 96 | int tmp2 = 0; |
nhummel3 | 8:a9b5acc63388 | 97 | int tmp0 = 0; |
nhummel3 | 8:a9b5acc63388 | 98 | int tmp1 = 0; |
nhummel3 | 8:a9b5acc63388 | 99 | int tmpM = 0; |
nhummel3 | 8:a9b5acc63388 | 100 | |
nhummel3 | 6:e4aba042979b | 101 | int num_trig = 0; |
nhummel3 | 6:e4aba042979b | 102 | |
nhummel3 | 6:e4aba042979b | 103 | //Microphone class |
nhummel3 | 6:e4aba042979b | 104 | class microphone |
nhummel3 | 6:e4aba042979b | 105 | { |
nhummel3 | 6:e4aba042979b | 106 | public : |
nhummel3 | 6:e4aba042979b | 107 | microphone(PinName pin); |
nhummel3 | 6:e4aba042979b | 108 | float read(); |
nhummel3 | 6:e4aba042979b | 109 | operator float (); |
nhummel3 | 6:e4aba042979b | 110 | private : |
nhummel3 | 6:e4aba042979b | 111 | AnalogIn _pin; |
nhummel3 | 6:e4aba042979b | 112 | }; |
nhummel3 | 6:e4aba042979b | 113 | microphone::microphone (PinName pin): |
nhummel3 | 6:e4aba042979b | 114 | _pin(pin) |
nhummel3 | 6:e4aba042979b | 115 | { |
nhummel3 | 6:e4aba042979b | 116 | } |
nhummel3 | 6:e4aba042979b | 117 | float microphone::read() |
nhummel3 | 6:e4aba042979b | 118 | { |
nhummel3 | 6:e4aba042979b | 119 | return _pin.read(); |
nhummel3 | 6:e4aba042979b | 120 | } |
nhummel3 | 6:e4aba042979b | 121 | inline microphone::operator float () |
nhummel3 | 6:e4aba042979b | 122 | { |
nhummel3 | 6:e4aba042979b | 123 | return _pin.read(); |
nhummel3 | 6:e4aba042979b | 124 | } |
nhummel3 | 6:e4aba042979b | 125 | |
nhummel3 | 6:e4aba042979b | 126 | microphone mymicrophone(p16); |
nhummel3 | 6:e4aba042979b | 127 | |
nhummel3 | 6:e4aba042979b | 128 | |
nhummel3 | 5:18b2796e4847 | 129 | // |
nhummel3 | 6:e4aba042979b | 130 | // Thread 1 |
nhummel3 | 6:e4aba042979b | 131 | // Debug thread for microphone on LCD |
nhummel3 | 6:e4aba042979b | 132 | void thread1(void const *args) |
nhummel3 | 6:e4aba042979b | 133 | { |
nhummel3 | 8:a9b5acc63388 | 134 | lcd_mutex.lock(); |
nhummel3 | 8:a9b5acc63388 | 135 | uLCD.filled_rectangle(0, 32, 128, 64, 0x00FF00); |
nhummel3 | 8:a9b5acc63388 | 136 | uLCD.locate(0,4); |
nhummel3 | 8:a9b5acc63388 | 137 | uLCD.color(WHITE); |
nhummel3 | 8:a9b5acc63388 | 138 | uLCD.textbackground_color(BLUE); |
nhummel3 | 8:a9b5acc63388 | 139 | uLCD.set_font(FONT_7X8); |
nhummel3 | 8:a9b5acc63388 | 140 | uLCD.text_mode(OPAQUE); |
nhummel3 | 8:a9b5acc63388 | 141 | uLCD.printf("Sensor Readings:\n\rMic:\n\rT0: \n\rT1: "); |
nhummel3 | 8:a9b5acc63388 | 142 | lcd_mutex.unlock(); |
nhummel3 | 6:e4aba042979b | 143 | while(true) { // thread loop |
nhummel3 | 6:e4aba042979b | 144 | //uLCD.cls(); |
nhummel3 | 6:e4aba042979b | 145 | mic_mutex.lock(); |
nhummel3 | 8:a9b5acc63388 | 146 | tmpM = int(abs((mymicrophone - (0.67/3.3)))*500.0); |
nhummel3 | 8:a9b5acc63388 | 147 | tmp0 = therm_to_temp(Therm0.read_u16()); |
nhummel3 | 8:a9b5acc63388 | 148 | tmp1 = therm_to_temp(Therm1.read_u16()); |
nhummel3 | 6:e4aba042979b | 149 | //tmp2 = user_button; |
nhummel3 | 6:e4aba042979b | 150 | mic_mutex.unlock(); |
nhummel3 | 8:a9b5acc63388 | 151 | lcd_mutex.lock(); |
nhummel3 | 8:a9b5acc63388 | 152 | |
nhummel3 | 8:a9b5acc63388 | 153 | uLCD.filled_rectangle(32, 40, 72, 63, 0x00FF00); |
nhummel3 | 8:a9b5acc63388 | 154 | uLCD.locate(4,5); |
nhummel3 | 8:a9b5acc63388 | 155 | uLCD.printf("%d",tmpM); |
nhummel3 | 8:a9b5acc63388 | 156 | uLCD.locate(4,6); |
nhummel3 | 8:a9b5acc63388 | 157 | uLCD.printf("%d", tmp0); |
nhummel3 | 8:a9b5acc63388 | 158 | uLCD.locate(4,7); |
nhummel3 | 8:a9b5acc63388 | 159 | uLCD.printf("%d", tmp1); |
nhummel3 | 6:e4aba042979b | 160 | |
nhummel3 | 6:e4aba042979b | 161 | lcd_mutex.unlock(); |
nhummel3 | 6:e4aba042979b | 162 | Thread::wait(100); |
nhummel3 | 6:e4aba042979b | 163 | } |
nhummel3 | 6:e4aba042979b | 164 | } |
nhummel3 | 8:a9b5acc63388 | 165 | |
nhummel3 | 5:18b2796e4847 | 166 | // |
nhummel3 | 8:a9b5acc63388 | 167 | // Thread 2 |
nhummel3 | 8:a9b5acc63388 | 168 | // Debug thread for microphone on LCD |
nhummel3 | 8:a9b5acc63388 | 169 | void thread2(void const *args) |
nhummel3 | 8:a9b5acc63388 | 170 | { |
nhummel3 | 8:a9b5acc63388 | 171 | lcd_mutex.lock(); |
nhummel3 | 8:a9b5acc63388 | 172 | uLCD.filled_rectangle(0, 0, 128, 31, 0x00FF00); |
nhummel3 | 8:a9b5acc63388 | 173 | uLCD.locate(0,0); |
nhummel3 | 8:a9b5acc63388 | 174 | uLCD.color(WHITE); |
nhummel3 | 8:a9b5acc63388 | 175 | uLCD.textbackground_color(BLUE); |
nhummel3 | 8:a9b5acc63388 | 176 | uLCD.set_font(FONT_7X8); |
nhummel3 | 8:a9b5acc63388 | 177 | uLCD.text_mode(OPAQUE); |
nhummel3 | 8:a9b5acc63388 | 178 | uLCD.printf("Select Mode:"); |
nhummel3 | 8:a9b5acc63388 | 179 | uLCD.locate(0,1); |
nhummel3 | 8:a9b5acc63388 | 180 | uLCD.printf(">COFFEE\n\r TEA"); |
nhummel3 | 8:a9b5acc63388 | 181 | lcd_mutex.unlock(); |
nhummel3 | 8:a9b5acc63388 | 182 | while(true) { // thread loop |
nhummel3 | 8:a9b5acc63388 | 183 | if(menu) { |
nhummel3 | 8:a9b5acc63388 | 184 | if(user_button == 0) { |
nhummel3 | 8:a9b5acc63388 | 185 | coffee = !coffee; |
nhummel3 | 8:a9b5acc63388 | 186 | if(coffee) { |
nhummel3 | 8:a9b5acc63388 | 187 | lcd_mutex.lock(); |
nhummel3 | 8:a9b5acc63388 | 188 | uLCD.filled_rectangle(0, 8, 7, 31, 0x00FF00); |
nhummel3 | 8:a9b5acc63388 | 189 | uLCD.locate(0,1); |
nhummel3 | 8:a9b5acc63388 | 190 | uLCD.color(WHITE); |
nhummel3 | 8:a9b5acc63388 | 191 | uLCD.textbackground_color(BLUE); |
nhummel3 | 8:a9b5acc63388 | 192 | uLCD.set_font(FONT_7X8); |
nhummel3 | 8:a9b5acc63388 | 193 | uLCD.text_mode(OPAQUE); |
nhummel3 | 8:a9b5acc63388 | 194 | uLCD.printf(">\n\r "); |
nhummel3 | 8:a9b5acc63388 | 195 | lcd_mutex.unlock(); |
nhummel3 | 8:a9b5acc63388 | 196 | Thread::wait(500); |
nhummel3 | 8:a9b5acc63388 | 197 | } else { |
nhummel3 | 8:a9b5acc63388 | 198 | lcd_mutex.lock(); |
nhummel3 | 8:a9b5acc63388 | 199 | uLCD.filled_rectangle(0, 8, 7, 31, 0x00FF00); |
nhummel3 | 8:a9b5acc63388 | 200 | uLCD.locate(0,1); |
nhummel3 | 8:a9b5acc63388 | 201 | uLCD.color(WHITE); |
nhummel3 | 8:a9b5acc63388 | 202 | uLCD.textbackground_color(BLUE); |
nhummel3 | 8:a9b5acc63388 | 203 | uLCD.set_font(FONT_7X8); |
nhummel3 | 8:a9b5acc63388 | 204 | uLCD.text_mode(OPAQUE); |
nhummel3 | 8:a9b5acc63388 | 205 | uLCD.printf(" \n\r>"); |
nhummel3 | 8:a9b5acc63388 | 206 | lcd_mutex.unlock(); |
nhummel3 | 8:a9b5acc63388 | 207 | Thread::wait(500); |
nhummel3 | 8:a9b5acc63388 | 208 | } |
nhummel3 | 8:a9b5acc63388 | 209 | } |
nhummel3 | 8:a9b5acc63388 | 210 | } |
nhummel3 | 8:a9b5acc63388 | 211 | } |
nhummel3 | 8:a9b5acc63388 | 212 | } |
nhummel3 | 8:a9b5acc63388 | 213 | |
nhummel3 | 8:a9b5acc63388 | 214 | |
nhummel3 | 8:a9b5acc63388 | 215 | // Thread 3 |
nhummel3 | 8:a9b5acc63388 | 216 | // Blink LED |
nhummel3 | 6:e4aba042979b | 217 | void thread3(void const *args) |
nhummel3 | 6:e4aba042979b | 218 | { |
nhummel3 | 6:e4aba042979b | 219 | while(1) { |
nhummel3 | 6:e4aba042979b | 220 | // if blink flag is set then blink red LED with PWM fade |
nhummel3 | 6:e4aba042979b | 221 | if(blink) { |
nhummel3 | 6:e4aba042979b | 222 | leds_mutex.lock(); |
nhummel3 | 6:e4aba042979b | 223 | RGBLED_g = 0; |
nhummel3 | 6:e4aba042979b | 224 | RGBLED_b = 0; |
nhummel3 | 6:e4aba042979b | 225 | leds_mutex.unlock(); |
nhummel3 | 6:e4aba042979b | 226 | for(float j = 0.2; j<= 1.0; j+=0.1) { |
nhummel3 | 6:e4aba042979b | 227 | leds_mutex.lock(); |
nhummel3 | 6:e4aba042979b | 228 | RGBLED_r = j; |
nhummel3 | 6:e4aba042979b | 229 | leds_mutex.unlock(); |
nhummel3 | 6:e4aba042979b | 230 | Thread::wait(20); |
nhummel3 | 6:e4aba042979b | 231 | } |
nhummel3 | 6:e4aba042979b | 232 | Thread::wait(100); |
nhummel3 | 6:e4aba042979b | 233 | for(float j = 1.0; j>= 0.2; j-=0.1) { |
nhummel3 | 6:e4aba042979b | 234 | leds_mutex.lock(); |
nhummel3 | 6:e4aba042979b | 235 | RGBLED_r = j; |
nhummel3 | 6:e4aba042979b | 236 | leds_mutex.unlock(); |
nhummel3 | 6:e4aba042979b | 237 | Thread::wait(10); |
nhummel3 | 6:e4aba042979b | 238 | } |
nhummel3 | 6:e4aba042979b | 239 | Thread::wait(100); // value of pot1 / 100 |
nhummel3 | 6:e4aba042979b | 240 | } |
nhummel3 | 6:e4aba042979b | 241 | } |
nhummel3 | 6:e4aba042979b | 242 | } |
nhummel3 | 5:18b2796e4847 | 243 | // |
nhummel3 | 5:18b2796e4847 | 244 | //// Thread 4 |
nhummel3 | 6:e4aba042979b | 245 | //// Microphone parser, triggers coffee pot when over certain value |
nhummel3 | 6:e4aba042979b | 246 | void thread4(void const *args) |
nhummel3 | 6:e4aba042979b | 247 | { |
nhummel3 | 6:e4aba042979b | 248 | while(1) { |
nhummel3 | 6:e4aba042979b | 249 | //read in, subtract 0.67 DC bias, take absolute value, and scale up .1Vpp to 15 for builtin LED display |
nhummel3 | 6:e4aba042979b | 250 | mic_mutex.lock(); |
nhummel3 | 6:e4aba042979b | 251 | tmp = int(abs((mymicrophone - (0.67/3.3)))*500.0); |
nhummel3 | 6:e4aba042979b | 252 | mic_mutex.unlock(); |
nhummel3 | 8:a9b5acc63388 | 253 | if(tmp > 15) { |
nhummel3 | 6:e4aba042979b | 254 | num_trig = num_trig+1; |
nhummel3 | 6:e4aba042979b | 255 | //only trigger if get sample above value 3 times in a row |
nhummel3 | 8:a9b5acc63388 | 256 | if(num_trig == 2) { |
nhummel3 | 6:e4aba042979b | 257 | trigger_mutex.lock(); |
nhummel3 | 6:e4aba042979b | 258 | trigger = 1; |
nhummel3 | 6:e4aba042979b | 259 | trigger_mutex.unlock(); |
nhummel3 | 6:e4aba042979b | 260 | tmp = 0; |
nhummel3 | 6:e4aba042979b | 261 | Thread::wait(200); |
nhummel3 | 6:e4aba042979b | 262 | } |
nhummel3 | 6:e4aba042979b | 263 | } else { |
nhummel3 | 6:e4aba042979b | 264 | num_trig = 0; |
nhummel3 | 6:e4aba042979b | 265 | } |
nhummel3 | 6:e4aba042979b | 266 | } |
nhummel3 | 6:e4aba042979b | 267 | } |
nhummel3 | 6:e4aba042979b | 268 | // Thread 5 |
nhummel3 | 6:e4aba042979b | 269 | // Bluetooth parser to trigger coffee pot |
nhummel3 | 6:e4aba042979b | 270 | void thread5(void const *args) |
nhummel3 | 6:e4aba042979b | 271 | { |
nhummel3 | 6:e4aba042979b | 272 | while(true) { |
nhummel3 | 6:e4aba042979b | 273 | //if recieve any button from bluetooth, increment tmp_blue |
nhummel3 | 6:e4aba042979b | 274 | if (blue.readable()){ |
nhummel3 | 6:e4aba042979b | 275 | bmut.lock(); |
nhummel3 | 6:e4aba042979b | 276 | if (blue.getc() == '!') { |
nhummel3 | 6:e4aba042979b | 277 | if (blue.getc() == 'B') { |
nhummel3 | 6:e4aba042979b | 278 | tmp_blue++; |
nhummel3 | 6:e4aba042979b | 279 | } |
nhummel3 | 6:e4aba042979b | 280 | } |
nhummel3 | 6:e4aba042979b | 281 | |
nhummel3 | 6:e4aba042979b | 282 | bmut.unlock(); |
nhummel3 | 6:e4aba042979b | 283 | } |
nhummel3 | 6:e4aba042979b | 284 | // if receive message from bluetooth twice in a row, trigger coffee pot |
nhummel3 | 6:e4aba042979b | 285 | if(tmp_blue == 2) { |
nhummel3 | 6:e4aba042979b | 286 | trigger_mutex.lock(); |
nhummel3 | 6:e4aba042979b | 287 | trigger = 1; |
nhummel3 | 6:e4aba042979b | 288 | trigger_mutex.unlock(); |
nhummel3 | 6:e4aba042979b | 289 | tmp_blue = 0; |
nhummel3 | 6:e4aba042979b | 290 | } |
nhummel3 | 6:e4aba042979b | 291 | } |
nhummel3 | 6:e4aba042979b | 292 | } |
4180_1 | 4:79863d2ea5a0 | 293 | |
nhummel3 | 8:a9b5acc63388 | 294 | void brew() { |
nhummel3 | 8:a9b5acc63388 | 295 | if(coffee) { |
nhummel3 | 8:a9b5acc63388 | 296 | target_temp = 190; |
nhummel3 | 8:a9b5acc63388 | 297 | } else { |
nhummel3 | 8:a9b5acc63388 | 298 | target_temp = 180; |
nhummel3 | 8:a9b5acc63388 | 299 | } |
nhummel3 | 8:a9b5acc63388 | 300 | brew_count = 0; |
nhummel3 | 8:a9b5acc63388 | 301 | while(1) { |
nhummel3 | 8:a9b5acc63388 | 302 | out_therm = therm_out.read_u16(); |
nhummel3 | 8:a9b5acc63388 | 303 | heater_therm = therm_heater.read_u16(); |
nhummel3 | 8:a9b5acc63388 | 304 | //pc.printf("Iteration:%d; Output Temp:%d, Heater Temp:%d\n\r", i, therm_to_temp(out_therm), therm_to_temp(heater_therm)); |
nhummel3 | 8:a9b5acc63388 | 305 | if(brew_count > 20 && out_therm > 40600) { |
nhummel3 | 8:a9b5acc63388 | 306 | //Done |
nhummel3 | 8:a9b5acc63388 | 307 | heater = 1; |
nhummel3 | 8:a9b5acc63388 | 308 | pump = 1; |
nhummel3 | 8:a9b5acc63388 | 309 | wait(3); |
nhummel3 | 8:a9b5acc63388 | 310 | pump = 0; |
nhummel3 | 8:a9b5acc63388 | 311 | return; |
nhummel3 | 8:a9b5acc63388 | 312 | } else { |
nhummel3 | 8:a9b5acc63388 | 313 | if(out_therm - temp_to_therm(target_temp) > tolerance) { |
nhummel3 | 8:a9b5acc63388 | 314 | //cool down |
nhummel3 | 8:a9b5acc63388 | 315 | pump = 0.8; |
nhummel3 | 8:a9b5acc63388 | 316 | heater = 1; //off bc inverted |
nhummel3 | 8:a9b5acc63388 | 317 | } else if(out_therm - temp_to_therm(target_temp) < tolerance) { |
nhummel3 | 8:a9b5acc63388 | 318 | //heat up |
nhummel3 | 8:a9b5acc63388 | 319 | pump = 0.25; |
nhummel3 | 8:a9b5acc63388 | 320 | heater = 0; //on |
nhummel3 | 8:a9b5acc63388 | 321 | } else { |
nhummel3 | 8:a9b5acc63388 | 322 | pump = 0.4; |
nhummel3 | 8:a9b5acc63388 | 323 | heater = 0; //on |
nhummel3 | 8:a9b5acc63388 | 324 | } |
nhummel3 | 8:a9b5acc63388 | 325 | wait(0.5); |
nhummel3 | 8:a9b5acc63388 | 326 | } |
nhummel3 | 8:a9b5acc63388 | 327 | brew_count++; |
nhummel3 | 8:a9b5acc63388 | 328 | } |
nhummel3 | 8:a9b5acc63388 | 329 | } |
nhummel3 | 8:a9b5acc63388 | 330 | |
dreschpe | 0:f6a57b843f79 | 331 | int main() |
dreschpe | 0:f6a57b843f79 | 332 | { |
nhummel3 | 6:e4aba042979b | 333 | // ---------Enter Initialize State--------- |
nhummel3 | 6:e4aba042979b | 334 | //set key to high initially to not trigger coffee pot |
nhummel3 | 6:e4aba042979b | 335 | key = 1; |
nhummel3 | 8:a9b5acc63388 | 336 | pump = 0; |
nhummel3 | 8:a9b5acc63388 | 337 | heater = 0; |
nhummel3 | 6:e4aba042979b | 338 | RGBLED_r = 0.5; |
nhummel3 | 6:e4aba042979b | 339 | wait(0.1); |
nhummel3 | 6:e4aba042979b | 340 | //set baud rate |
nhummel3 | 6:e4aba042979b | 341 | blue.baud(9600); |
nhummel3 | 6:e4aba042979b | 342 | //set speaker period |
nhummel3 | 5:18b2796e4847 | 343 | Speaker.period(1.0/250000.0); |
nhummel3 | 8:a9b5acc63388 | 344 | Thread t1(thread1); //start thread1 |
nhummel3 | 8:a9b5acc63388 | 345 | Thread t2(thread2); //start thread3 |
nhummel3 | 6:e4aba042979b | 346 | Thread t3(thread3); //start thread3 |
nhummel3 | 6:e4aba042979b | 347 | Thread t4(thread4); //start thread4 |
nhummel3 | 6:e4aba042979b | 348 | Thread t5(thread5); //start thread5 |
nhummel3 | 6:e4aba042979b | 349 | |
nhummel3 | 6:e4aba042979b | 350 | //set button mode |
nhummel3 | 6:e4aba042979b | 351 | user_button.mode(PullUp); |
nhummel3 | 6:e4aba042979b | 352 | wait(0.001); |
nhummel3 | 6:e4aba042979b | 353 | |
nhummel3 | 6:e4aba042979b | 354 | //---------Enter Ready State--------- |
nhummel3 | 8:a9b5acc63388 | 355 | lcd_mutex.lock(); |
nhummel3 | 6:e4aba042979b | 356 | uLCD.cls(); |
nhummel3 | 8:a9b5acc63388 | 357 | uLCD.filled_rectangle(0, 79, 128, 112, 0x00FF00); |
nhummel3 | 8:a9b5acc63388 | 358 | uLCD.locate(0,10); |
nhummel3 | 8:a9b5acc63388 | 359 | uLCD.color(WHITE); |
nhummel3 | 8:a9b5acc63388 | 360 | uLCD.textbackground_color(BLUE); |
nhummel3 | 8:a9b5acc63388 | 361 | uLCD.set_font(FONT_7X8); |
nhummel3 | 8:a9b5acc63388 | 362 | uLCD.text_mode(OPAQUE); |
nhummel3 | 8:a9b5acc63388 | 363 | uLCD.printf("State:"); |
nhummel3 | 8:a9b5acc63388 | 364 | uLCD.locate(0,11); |
nhummel3 | 6:e4aba042979b | 365 | uLCD.printf("READY"); |
nhummel3 | 8:a9b5acc63388 | 366 | lcd_mutex.unlock(); |
nhummel3 | 6:e4aba042979b | 367 | |
nhummel3 | 6:e4aba042979b | 368 | //attach audio interrupt |
nhummel3 | 6:e4aba042979b | 369 | sampletick.attach(&audio_sample, 1.0 / sample_freq); |
nhummel3 | 5:18b2796e4847 | 370 | |
nhummel3 | 6:e4aba042979b | 371 | while(1){ |
nhummel3 | 8:a9b5acc63388 | 372 | Thread::wait(50); |
nhummel3 | 6:e4aba042979b | 373 | //set mutex lock for trigger |
nhummel3 | 8:a9b5acc63388 | 374 | //pc.printf("%d\n\n\r",Therm0.read_u16()); |
nhummel3 | 6:e4aba042979b | 375 | //if trigger flag is set, start coffee pot |
nhummel3 | 6:e4aba042979b | 376 | if(trigger) { |
nhummel3 | 6:e4aba042979b | 377 | // ---------Enter Brewing State--------- |
nhummel3 | 8:a9b5acc63388 | 378 | menu = 0; |
nhummel3 | 8:a9b5acc63388 | 379 | trigger_mutex.lock(); |
nhummel3 | 6:e4aba042979b | 380 | //write to LCD |
nhummel3 | 8:a9b5acc63388 | 381 | lcd_mutex.lock(); |
nhummel3 | 8:a9b5acc63388 | 382 | uLCD.filled_rectangle(0, 88, 100, 111, 0x00FF00); |
nhummel3 | 8:a9b5acc63388 | 383 | uLCD.locate(0,11); |
nhummel3 | 6:e4aba042979b | 384 | uLCD.printf("BREWING..."); |
nhummel3 | 8:a9b5acc63388 | 385 | lcd_mutex.unlock(); |
nhummel3 | 6:e4aba042979b | 386 | |
nhummel3 | 6:e4aba042979b | 387 | //set microphone mutex lock to prevent false trigger while running |
nhummel3 | 6:e4aba042979b | 388 | mic_mutex.lock(); |
nhummel3 | 6:e4aba042979b | 389 | |
nhummel3 | 6:e4aba042979b | 390 | // set coffee pot trigger to low |
nhummel3 | 8:a9b5acc63388 | 391 | //key = 0; |
nhummel3 | 6:e4aba042979b | 392 | //changing LED to indicate trigger |
nhummel3 | 6:e4aba042979b | 393 | leds_mutex.lock(); |
nhummel3 | 6:e4aba042979b | 394 | RGBLED_r = 0; |
nhummel3 | 6:e4aba042979b | 395 | RGBLED_b = 0; |
nhummel3 | 6:e4aba042979b | 396 | RGBLED_g = 1; |
nhummel3 | 6:e4aba042979b | 397 | leds_mutex.unlock(); |
nhummel3 | 6:e4aba042979b | 398 | Thread::wait(200); |
nhummel3 | 6:e4aba042979b | 399 | |
nhummel3 | 6:e4aba042979b | 400 | // set coffee pot trigger back to high after pulse |
nhummel3 | 8:a9b5acc63388 | 401 | //key = 1; |
nhummel3 | 6:e4aba042979b | 402 | |
nhummel3 | 6:e4aba042979b | 403 | // change LED to indicate running |
nhummel3 | 6:e4aba042979b | 404 | leds_mutex.lock(); |
nhummel3 | 6:e4aba042979b | 405 | RGBLED_g = 0; |
nhummel3 | 6:e4aba042979b | 406 | RGBLED_b = 1; |
nhummel3 | 6:e4aba042979b | 407 | leds_mutex.unlock(); |
nhummel3 | 8:a9b5acc63388 | 408 | |
nhummel3 | 8:a9b5acc63388 | 409 | // Brew Drink |
nhummel3 | 8:a9b5acc63388 | 410 | //wait(3); |
nhummel3 | 8:a9b5acc63388 | 411 | brew(); |
nhummel3 | 6:e4aba042979b | 412 | |
nhummel3 | 6:e4aba042979b | 413 | // ---------Enter Reset State--------- |
nhummel3 | 6:e4aba042979b | 414 | |
nhummel3 | 6:e4aba042979b | 415 | // set noise flag high |
nhummel3 | 6:e4aba042979b | 416 | noise = 1; |
nhummel3 | 8:a9b5acc63388 | 417 | lcd_mutex.lock(); |
nhummel3 | 8:a9b5acc63388 | 418 | uLCD.filled_rectangle(0, 88, 100, 111, 0x00FF00); |
nhummel3 | 8:a9b5acc63388 | 419 | uLCD.locate(0,11); |
nhummel3 | 8:a9b5acc63388 | 420 | uLCD.printf("COFFEE READY\n\rPLEASE PRESS\n\rRESET"); |
nhummel3 | 8:a9b5acc63388 | 421 | lcd_mutex.unlock(); |
nhummel3 | 6:e4aba042979b | 422 | |
nhummel3 | 6:e4aba042979b | 423 | // set led blink flag high |
nhummel3 | 6:e4aba042979b | 424 | blink = 1; |
nhummel3 | 6:e4aba042979b | 425 | |
nhummel3 | 6:e4aba042979b | 426 | //wait for reset button press from user |
nhummel3 | 6:e4aba042979b | 427 | while(reset) { |
nhummel3 | 6:e4aba042979b | 428 | reset = user_button; |
nhummel3 | 6:e4aba042979b | 429 | Thread::wait(1); |
nhummel3 | 6:e4aba042979b | 430 | } |
nhummel3 | 8:a9b5acc63388 | 431 | //---------Enter User Restock--------- |
nhummel3 | 8:a9b5acc63388 | 432 | blink = 0; |
nhummel3 | 8:a9b5acc63388 | 433 | // set noise flag low |
nhummel3 | 8:a9b5acc63388 | 434 | noise = 0; |
nhummel3 | 8:a9b5acc63388 | 435 | lcd_mutex.lock(); |
nhummel3 | 8:a9b5acc63388 | 436 | uLCD.filled_rectangle(0, 88, 100, 111, 0x00FF00); |
nhummel3 | 8:a9b5acc63388 | 437 | uLCD.locate(0,11); |
nhummel3 | 8:a9b5acc63388 | 438 | uLCD.printf("Please Restock\n\rThe Machine"); |
nhummel3 | 8:a9b5acc63388 | 439 | lcd_mutex.unlock(); |
nhummel3 | 8:a9b5acc63388 | 440 | |
nhummel3 | 8:a9b5acc63388 | 441 | wait(0.5); |
nhummel3 | 8:a9b5acc63388 | 442 | reset = user_button; |
nhummel3 | 8:a9b5acc63388 | 443 | while(reset) { |
nhummel3 | 8:a9b5acc63388 | 444 | reset = user_button; |
nhummel3 | 8:a9b5acc63388 | 445 | Thread::wait(1); |
nhummel3 | 8:a9b5acc63388 | 446 | } |
nhummel3 | 6:e4aba042979b | 447 | |
nhummel3 | 6:e4aba042979b | 448 | //---------Enter Ready State--------- |
nhummel3 | 6:e4aba042979b | 449 | |
nhummel3 | 6:e4aba042979b | 450 | // set led blink flag low |
nhummel3 | 6:e4aba042979b | 451 | blink = 0; |
nhummel3 | 8:a9b5acc63388 | 452 | lcd_mutex.lock(); |
nhummel3 | 8:a9b5acc63388 | 453 | uLCD.filled_rectangle(0, 88, 100, 111, 0x00FF00); |
nhummel3 | 8:a9b5acc63388 | 454 | uLCD.locate(0,11); |
nhummel3 | 6:e4aba042979b | 455 | uLCD.printf("READY"); |
nhummel3 | 8:a9b5acc63388 | 456 | lcd_mutex.unlock(); |
nhummel3 | 6:e4aba042979b | 457 | |
nhummel3 | 6:e4aba042979b | 458 | // set noise flag low |
nhummel3 | 6:e4aba042979b | 459 | noise = 0; |
nhummel3 | 6:e4aba042979b | 460 | // set reset flag high |
nhummel3 | 6:e4aba042979b | 461 | reset = 1; |
nhummel3 | 6:e4aba042979b | 462 | |
nhummel3 | 6:e4aba042979b | 463 | // change leds to indicate ready state |
nhummel3 | 6:e4aba042979b | 464 | leds_mutex.lock(); |
nhummel3 | 6:e4aba042979b | 465 | RGBLED_g = 0; |
nhummel3 | 6:e4aba042979b | 466 | RGBLED_b = 0; |
nhummel3 | 6:e4aba042979b | 467 | RGBLED_r = 1; |
nhummel3 | 6:e4aba042979b | 468 | leds_mutex.unlock(); |
nhummel3 | 6:e4aba042979b | 469 | |
nhummel3 | 6:e4aba042979b | 470 | // set trigger flag low |
nhummel3 | 6:e4aba042979b | 471 | trigger = 0; |
nhummel3 | 8:a9b5acc63388 | 472 | wait(0.4); |
nhummel3 | 6:e4aba042979b | 473 | mic_mutex.unlock(); |
nhummel3 | 8:a9b5acc63388 | 474 | trigger_mutex.unlock(); |
nhummel3 | 8:a9b5acc63388 | 475 | menu = 1; |
nhummel3 | 6:e4aba042979b | 476 | } |
dreschpe | 0:f6a57b843f79 | 477 | } |
nhummel3 | 6:e4aba042979b | 478 | } |