Final project code

Dependencies:   4DGL-uLCD-SE MAX31855 mbed-rtos mbed

Fork of Coffee_Roaster_Threads by Eric Patterson

Revision:
8:1db15ab871a4
Parent:
7:8a0223a951c1
Child:
9:d7300899fd85
--- a/main.cpp	Thu Nov 20 04:01:51 2014 +0000
+++ b/main.cpp	Thu Nov 20 20:30:51 2014 +0000
@@ -4,6 +4,7 @@
 #include "stdio.h"
 #include "max31855.h"
 #include <mpr121.h>
+#include "Stopwatch.h"
 
 DigitalOut led1(LED1);
 DigitalOut led2(LED2);
@@ -24,9 +25,17 @@
 int settemp = 85; // initial temperature set
 float ftemperature = 0; // float variable for temperature
 float ctemperature = 0; // float variable for temperature
-int temptemp=0;
+int temptemp=85;
+
+int key_code=-1;
+int a =0;
+int place= 100;
 
-int key_code=0;
+int s = 00;
+int m = 00;
+int h = 00;
+
+Stopwatch watch = Stopwatch();
 
 // Create the interrupt receiver object on pin 26
 InterruptIn interrupt(p26);
@@ -41,30 +50,7 @@
 // Thread 1: print the elapsed time on line 1 of the uLCD (i.e., HH:MM:SS)
 void elapsedtime(void const *args)   //elapsed timer for when program starts
 {
-    int s = 00; //seconds
-    int m = 00; //minutes
-    int h = 00; //hours
-
-    while(true) {
-        s++;
-        if(s>59) {
-            s=00;
-            m++;
-        }
-        if(m>59) {
-            m=00;
-            h++;
-        }
-        four_slots.wait(); // lock screen resource
-        //lcd_mutex.lock();
-        uLCD.color(0xFFFF00); //set text color
-        uLCD.locate(0,0); //col,row location of text
-        wait(0.1);
-        uLCD.printf("Time    | %2d:%2d:%2d", h,m,s); // display time
-        //lcd_mutex.unlock();
-        four_slots.release(); //unlock screen resource
-        Thread::wait(1000); // update once per secon
-    }
+    watch.start();
 }
 
 
@@ -75,14 +61,29 @@
         if (max1.ready()==1) {
             ctemperature = max1.read_temp(); //Get the reading
             ftemperature = (ctemperature)*(9.0/5.0)+32.0;
-            four_slots.wait();
-            uLCD.color(0xFFFF00);
-            //uLCD.locate(0,2); //col,row
-            //wait(0.1);
-            // uLCD.printf("Current Temp. F");
-            uLCD.locate(11,6); //col,row
-            uLCD.printf("%4.2f", ftemperature);
-            four_slots.release();
+        }
+    }
+}
+
+
+// Thread 3: reset temp
+void thermoset(void const *args)   //line 2
+{
+    while(true) {
+        if(up == 1) {
+             a = 0;
+             temptemp=0;
+             while(a<3) {
+                 if(key_code>=0 && key_code<=9) {
+                     wait(.1);
+                     if(a==0)temptemp=key_code*100;
+                     if(a==1)temptemp+=key_code*10;
+                     if(a==2)temptemp+=key_code*1;
+                     a++;
+                     key_code = -1;
+                 }
+             }
+             settemp=temptemp;
         }
     }
 }
@@ -102,15 +103,13 @@
 
     if(key_code== 10) {
         motor = 0;
-
-
     }
     if(key_code== 11) {
         motor = 1;
     }
 }
 
-int a =0;
+
 int main()
 {
     max1.initialise(); //initialize thermocouple IC
@@ -121,82 +120,60 @@
 
     Thread t1(elapsedtime); //run elapsed time counter
     Thread t2(thermoread); //read and display temperature from thermocouple
+    Thread t3(thermoset); // user set temperature
 
     while(1) { //hystersis program
-        key_code = 0;
+        char* time = watch.getTime();
+        four_slots.wait(); //lock screen
+        uLCD.color(0xFFFF00); //set color
 
-        four_slots.wait();
-        uLCD.color(0xFFFF00);
-        uLCD.locate(0,5); //col,row
-        uLCD.printf("Set     | ");
-        uLCD.locate(0,6); //col,row
-        uLCD.printf("Current | ");
-        uLCD.locate(0,2); //col,row
-        uLCD.printf("Motor   | ");
-        four_slots.release();
+        uLCD.locate(0,0); //col,row for time
+        uLCD.printf("Time    | %s",time);//%2d:%2d:%2d", h,m,s); // display time
 
-        int place= 100;
+        uLCD.locate(0,5);
+        uLCD.printf("Set     | %3d",settemp); // current set temperature
 
-        if(motor==0) {
-            four_slots.wait();
-            uLCD.color(0xFFFF00);
-            uLCD.locate(11,2); //col,row
-            uLCD.printf("OFF");
-            four_slots.release();
-        }
-        if(motor==1) {
-            four_slots.wait();
-            uLCD.color(0xFFFF00);
-            uLCD.locate(11,2); //col,row
-            uLCD.printf("ON ");
-            four_slots.release();
+        uLCD.locate(0,6);
+        uLCD.printf("Current | %4.2f", ftemperature); // temperature reading
+
+        uLCD.locate(0,2);
+        uLCD.printf("Motor   | "); //motor state in "on" or "off" state
+
+        uLCD.locate(0,13);
+        uLCD.printf("New Temp| %3d",temptemp); //new user temperature
+
+        /*if(a==0){
+             uLCD.locate(11,13);
+            uLCD.printf("%d",temptemp); //new user temperature
         }
 
-        if(up == 1) {
-            wait(.5);
-            a = 0;
-            temptemp=0;
-            four_slots.wait();
-            uLCD.color(0xFFFF00);
-            uLCD.locate(0,14); //col,row
-            uLCD.printf("%3d",temptemp);
-            four_slots.release();
+        if(a==1){
+             uLCD.locate(11,13);
+            uLCD.printf("%d",temptemp); //new user temperature
+        }
+
+        if(a==2){
+             uLCD.locate(11,13);
+            uLCD.printf("%d",temptemp); //new user temperature
+        }*/
 
-            while(a<3) {       
-                if(key_code>=0 && key_code<=9) {
-                    wait(1);
-                    temptemp+=place*key_code;
-                    place=place/10;
-                    four_slots.wait();
-                    uLCD.color(0xFFFF00);
-                    uLCD.locate(0,13); //col,row
-                    uLCD.printf("intermediate value");
-                    uLCD.locate(0,14); //col,row
-                    uLCD.printf("%3d",temptemp);
-                    four_slots.release();
-                    a++;
-                    key_code = -1;
-                    //wait(2);
-                }
-            }
+        if(motor==0) {
+            uLCD.locate(11,2); //col,row
+            uLCD.printf("OFF");
         }
-        settemp=temptemp;
-        four_slots.wait();
-        uLCD.color(0xFFFF00);
-        uLCD.locate(11,5); //col,row
-        uLCD.printf("%3d",settemp);
-        four_slots.release();
-        //wait(2);
+        if(motor==1) {
+            uLCD.locate(11,2); //col,row
+            uLCD.printf("ON ");
+        }
 
-
+        four_slots.release(); // release screen resource
 
         if(ftemperature < settemp-1) { //condition for 1 degree under
             toggle=1; //turn on ssr for nichrome wire
             //toggle=0;
         }
-        if(ftemperature > settemp-1) { //condition for 1 degree over
+        if(ftemperature > settemp-1) {
             toggle=0; //turn off ssr for nichrome wire
         }
-        // wait(.1); //check every 1 second
     }
 }