Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: mbed wave_player mbed-rtos C12832_lcd 4DGL-uLCD-SE LCD_fonts SDFileSystem
Revision 8:a9b5acc63388, committed 2019-12-06
- Comitter:
- nhummel3
- Date:
- Fri Dec 06 13:10:47 2019 +0000
- Parent:
- 7:2e274cc18797
- Commit message:
- Temperature control and GUI added;
Changed in this revision
| main.cpp | Show annotated file Show diff for this revision Revisions of this file |
--- a/main.cpp Thu Dec 05 17:56:38 2019 +0000
+++ b/main.cpp Fri Dec 06 13:10:47 2019 +0000
@@ -25,6 +25,10 @@
int i=0; //index for audio array
int d = 0;
volatile int trigger = 0; // check if coffeepot has been triggered via sound or bluetooth
+volatile int menu = 1; // check if coffeepot has been triggered via sound or bluetooth
+int coffee = 0;
+int tolerance = 600;
+int brew_count = 0;
//Declarations
Ticker sampletick; //audio interrupt
@@ -33,6 +37,7 @@
AnalogIn Therm1(p19);
DigitalOut key(p20);
+Serial pc(USBTX,USBRX);
PwmOut Speaker(p26);
PwmOut RGBLED_r(p23);
PwmOut RGBLED_g(p22);
@@ -40,6 +45,11 @@
DigitalIn user_button(p12);
Serial blue(p28,p27);
+PwmOut pump(p24);
+DigitalOut heater(p14); //inverted logic
+AnalogIn therm_out(p18);
+AnalogIn therm_heater(p19);
+
// Mutex declarations
Mutex lcd_mutex;
Mutex leds_mutex;
@@ -47,10 +57,27 @@
Mutex mic_mutex;
Mutex trigger_mutex;
+int out_temp;
+int heater_temp;
+int out_therm;
+int heater_therm;
+int target_temp = 190;
+
volatile float r = 0.0;
volatile float g = 0.0;
volatile float b = 0.0;
+float a_coeff = 93.6469;
+float b_coeff = 4681.5;
+
+int therm_to_temp(int therm) {
+ return int(a_coeff*log(therm/b_coeff));
+}
+
+int temp_to_therm(int temp) {
+ return int(b_coeff*exp((temp)/a_coeff));
+}
+
//Audio ISR
void audio_sample ()
{
@@ -65,7 +92,12 @@
// Temporary debug variables
int tmp = 0;
+int tmp_button = 0;
int tmp2 = 0;
+int tmp0 = 0;
+int tmp1 = 0;
+int tmpM = 0;
+
int num_trig = 0;
//Microphone class
@@ -99,25 +131,89 @@
// Debug thread for microphone on LCD
void thread1(void const *args)
{
+ lcd_mutex.lock();
+ uLCD.filled_rectangle(0, 32, 128, 64, 0x00FF00);
+ uLCD.locate(0,4);
+ uLCD.color(WHITE);
+ uLCD.textbackground_color(BLUE);
+ uLCD.set_font(FONT_7X8);
+ uLCD.text_mode(OPAQUE);
+ uLCD.printf("Sensor Readings:\n\rMic:\n\rT0: \n\rT1: ");
+ lcd_mutex.unlock();
while(true) { // thread loop
- lcd_mutex.lock();
-
//uLCD.cls();
mic_mutex.lock();
- tmp = int(abs((mymicrophone - (0.67/3.3)))*500.0);
+ tmpM = int(abs((mymicrophone - (0.67/3.3)))*500.0);
+ tmp0 = therm_to_temp(Therm0.read_u16());
+ tmp1 = therm_to_temp(Therm1.read_u16());
//tmp2 = user_button;
mic_mutex.unlock();
- trigger_mutex.lock();
- uLCD.printf("%d",trigger);
- trigger_mutex.unlock();
+ lcd_mutex.lock();
+
+ uLCD.filled_rectangle(32, 40, 72, 63, 0x00FF00);
+ uLCD.locate(4,5);
+ uLCD.printf("%d",tmpM);
+ uLCD.locate(4,6);
+ uLCD.printf("%d", tmp0);
+ uLCD.locate(4,7);
+ uLCD.printf("%d", tmp1);
lcd_mutex.unlock();
Thread::wait(100);
}
}
+
//
-//// Thread 3
-//// Blink LED
+// Thread 2
+// Debug thread for microphone on LCD
+void thread2(void const *args)
+{
+ lcd_mutex.lock();
+ uLCD.filled_rectangle(0, 0, 128, 31, 0x00FF00);
+ uLCD.locate(0,0);
+ uLCD.color(WHITE);
+ uLCD.textbackground_color(BLUE);
+ uLCD.set_font(FONT_7X8);
+ uLCD.text_mode(OPAQUE);
+ uLCD.printf("Select Mode:");
+ uLCD.locate(0,1);
+ uLCD.printf(">COFFEE\n\r TEA");
+ lcd_mutex.unlock();
+ while(true) { // thread loop
+ if(menu) {
+ if(user_button == 0) {
+ coffee = !coffee;
+ if(coffee) {
+ lcd_mutex.lock();
+ uLCD.filled_rectangle(0, 8, 7, 31, 0x00FF00);
+ uLCD.locate(0,1);
+ uLCD.color(WHITE);
+ uLCD.textbackground_color(BLUE);
+ uLCD.set_font(FONT_7X8);
+ uLCD.text_mode(OPAQUE);
+ uLCD.printf(">\n\r ");
+ lcd_mutex.unlock();
+ Thread::wait(500);
+ } else {
+ lcd_mutex.lock();
+ uLCD.filled_rectangle(0, 8, 7, 31, 0x00FF00);
+ uLCD.locate(0,1);
+ uLCD.color(WHITE);
+ uLCD.textbackground_color(BLUE);
+ uLCD.set_font(FONT_7X8);
+ uLCD.text_mode(OPAQUE);
+ uLCD.printf(" \n\r>");
+ lcd_mutex.unlock();
+ Thread::wait(500);
+ }
+ }
+ }
+ }
+}
+
+
+// Thread 3
+// Blink LED
void thread3(void const *args)
{
while(1) {
@@ -154,10 +250,10 @@
mic_mutex.lock();
tmp = int(abs((mymicrophone - (0.67/3.3)))*500.0);
mic_mutex.unlock();
- if(tmp > 9) {
+ if(tmp > 15) {
num_trig = num_trig+1;
//only trigger if get sample above value 3 times in a row
- if(num_trig == 3){
+ if(num_trig == 2) {
trigger_mutex.lock();
trigger = 1;
trigger_mutex.unlock();
@@ -195,19 +291,58 @@
}
}
+void brew() {
+ if(coffee) {
+ target_temp = 190;
+ } else {
+ target_temp = 180;
+ }
+ brew_count = 0;
+ while(1) {
+ out_therm = therm_out.read_u16();
+ heater_therm = therm_heater.read_u16();
+ //pc.printf("Iteration:%d; Output Temp:%d, Heater Temp:%d\n\r", i, therm_to_temp(out_therm), therm_to_temp(heater_therm));
+ if(brew_count > 20 && out_therm > 40600) {
+ //Done
+ heater = 1;
+ pump = 1;
+ wait(3);
+ pump = 0;
+ return;
+ } else {
+ if(out_therm - temp_to_therm(target_temp) > tolerance) {
+ //cool down
+ pump = 0.8;
+ heater = 1; //off bc inverted
+ } else if(out_therm - temp_to_therm(target_temp) < tolerance) {
+ //heat up
+ pump = 0.25;
+ heater = 0; //on
+ } else {
+ pump = 0.4;
+ heater = 0; //on
+ }
+ wait(0.5);
+ }
+ brew_count++;
+ }
+}
+
int main()
{
// ---------Enter Initialize State---------
//set key to high initially to not trigger coffee pot
key = 1;
+ pump = 0;
+ heater = 0;
RGBLED_r = 0.5;
wait(0.1);
//set baud rate
blue.baud(9600);
//set speaker period
Speaker.period(1.0/250000.0);
- //Thread t1(thread1); //start thread1
-
+ Thread t1(thread1); //start thread1
+ Thread t2(thread2); //start thread3
Thread t3(thread3); //start thread3
Thread t4(thread4); //start thread4
Thread t5(thread5); //start thread5
@@ -217,34 +352,43 @@
wait(0.001);
//---------Enter Ready State---------
-
- //lcd_mutex.lock();
+ lcd_mutex.lock();
uLCD.cls();
+ uLCD.filled_rectangle(0, 79, 128, 112, 0x00FF00);
+ uLCD.locate(0,10);
+ uLCD.color(WHITE);
+ uLCD.textbackground_color(BLUE);
+ uLCD.set_font(FONT_7X8);
+ uLCD.text_mode(OPAQUE);
+ uLCD.printf("State:");
+ uLCD.locate(0,11);
uLCD.printf("READY");
- //lcd_mutex.unlock();
+ lcd_mutex.unlock();
//attach audio interrupt
sampletick.attach(&audio_sample, 1.0 / sample_freq);
while(1){
- Thread::wait(100);
+ Thread::wait(50);
//set mutex lock for trigger
- trigger_mutex.lock();
+ //pc.printf("%d\n\n\r",Therm0.read_u16());
//if trigger flag is set, start coffee pot
if(trigger) {
// ---------Enter Brewing State---------
-
+ menu = 0;
+ trigger_mutex.lock();
//write to LCD
- //lcd_mutex.lock();
- uLCD.cls();
+ lcd_mutex.lock();
+ uLCD.filled_rectangle(0, 88, 100, 111, 0x00FF00);
+ uLCD.locate(0,11);
uLCD.printf("BREWING...");
- //lcd_mutex.unlock();
+ lcd_mutex.unlock();
//set microphone mutex lock to prevent false trigger while running
mic_mutex.lock();
// set coffee pot trigger to low
- key = 0;
+ //key = 0;
//changing LED to indicate trigger
leds_mutex.lock();
RGBLED_r = 0;
@@ -254,23 +398,27 @@
Thread::wait(200);
// set coffee pot trigger back to high after pulse
- key = 1;
+ //key = 1;
// change LED to indicate running
leds_mutex.lock();
RGBLED_g = 0;
RGBLED_b = 1;
leds_mutex.unlock();
- wait(100);
+
+ // Brew Drink
+ //wait(3);
+ brew();
// ---------Enter Reset State---------
// set noise flag high
noise = 1;
- //lcd_mutex.lock();
- uLCD.cls();
- uLCD.printf("COFFEE READY\n\n\rPLEASE RESET\n\rMACHINE");
- //lcd_mutex.unlock();
+ lcd_mutex.lock();
+ uLCD.filled_rectangle(0, 88, 100, 111, 0x00FF00);
+ uLCD.locate(0,11);
+ uLCD.printf("COFFEE READY\n\rPLEASE PRESS\n\rRESET");
+ lcd_mutex.unlock();
// set led blink flag high
blink = 1;
@@ -280,15 +428,32 @@
reset = user_button;
Thread::wait(1);
}
+ //---------Enter User Restock---------
+ blink = 0;
+ // set noise flag low
+ noise = 0;
+ lcd_mutex.lock();
+ uLCD.filled_rectangle(0, 88, 100, 111, 0x00FF00);
+ uLCD.locate(0,11);
+ uLCD.printf("Please Restock\n\rThe Machine");
+ lcd_mutex.unlock();
+
+ wait(0.5);
+ reset = user_button;
+ while(reset) {
+ reset = user_button;
+ Thread::wait(1);
+ }
//---------Enter Ready State---------
// set led blink flag low
blink = 0;
- //lcd_mutex.lock();
- uLCD.cls();
+ lcd_mutex.lock();
+ uLCD.filled_rectangle(0, 88, 100, 111, 0x00FF00);
+ uLCD.locate(0,11);
uLCD.printf("READY");
- //lcd_mutex.unlock();
+ lcd_mutex.unlock();
// set noise flag low
noise = 0;
@@ -304,8 +469,10 @@
// set trigger flag low
trigger = 0;
+ wait(0.4);
mic_mutex.unlock();
+ trigger_mutex.unlock();
+ menu = 1;
}
- trigger_mutex.unlock();
}
}