Working better. Maybe a little more improvement needs to be made to the display algorithm.

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

Fork of Coffee_Roaster_testing by Eric Patterson

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "uLCD_4DGL.h"
00003 #include "rtos.h"
00004 #include "stdio.h"
00005 #include "max31855.h"
00006 #include <mpr121.h>
00007 #include "Stopwatch.h"
00008 
00009 
00010 DigitalOut toggle(p30); // toggle for Nichrome wire SSR
00011 DigitalIn up(p19); // User reset button
00012 DigitalOut motor(p22); // motor control
00013 
00014 uLCD_4DGL uLCD(p9,p10,p11); // serial tx, serial rx, reset pin;
00015 SPI thermoSPI(p5,p6,p7); // setup SPI interface
00016 max31855 max1(thermoSPI, p20); //setup max31855 interface
00017 InterruptIn interrupt(p26); // Create the interrupt receiver object on pin 26
00018 I2C i2c(p28, p27); // Setup the i2c bus on pins 28 and 27
00019 Mpr121 mpr121(&i2c, Mpr121::ADD_VSS); // Setup the Mpr121: // constructor(i2c object, i2c address of the mpr121)
00020 
00021 Mutex lcd_mutex; // mutex to make the lcd lib thread safe
00022 Semaphore four_slots(1); //Activate semaphore
00023 Stopwatch watch = Stopwatch();
00024 
00025 int settemp = 1; // initial temperature set
00026 float ftemperature = 0; // float variable for temperature
00027 float ctemperature = 0; // float variable for temperature
00028 int temptemp=1;
00029 int key_code=-1;
00030 int a =0;
00031 int place= 100;
00032 int s = 00;
00033 int m = 00;
00034 int h = 00;
00035 
00036 
00037 // Thread 1: run stopwatch timer
00038 void elapsedtime(void const *args)   //elapsed timer for when program starts
00039 {
00040     watch.start();
00041 }
00042 
00043 
00044 // Thread 2: print the temperature from the thermocouple - MAX31855 device
00045 void thermoread(void const *args)   //line 2
00046 {
00047     while(true) {
00048         if (max1.ready()==1) {
00049             ctemperature = max1.read_temp(); //Get the reading in celsius 
00050             ftemperature = (ctemperature)*(9.0/5.0)+32.0; //Get the reading in fahrenheit
00051         }
00052     }
00053 }
00054 
00055 
00056 // Thread 3: reset temp
00057 void thermoset(void const *args)   //line 2
00058 {
00059     while(true) {
00060         if(up == 1) {
00061             a = 0;
00062             temptemp=0;
00063             while(a<3) { //USER MUST INPUT 3 NUMBERS
00064                 if(key_code>=0 && key_code<=9) {
00065                     wait(.1);
00066                     if(a==0)temptemp=key_code*100;
00067                     if(a==1)temptemp+=key_code*10;
00068                     if(a==2)temptemp+=key_code*1;
00069                     a++;
00070                     key_code = -1; //clear keycode
00071                 }
00072             }
00073             settemp=temptemp;
00074         }
00075     }
00076 }
00077 
00078 
00079 void fallInterrupt()
00080 {
00081     int i=0;
00082     int value=mpr121.read(0x00);
00083     value +=mpr121.read(0x01)<<8;
00084     i=0;
00085     for (i=0; i<12; i++) {     // puts key number out to 0-12 digits
00086         if (((value>>i)&0x01)==1) key_code=i;
00087     }
00088     if(key_code== 10) {
00089         motor = 0;
00090     }
00091     if(key_code== 11) {
00092         motor = 1;
00093     }
00094 }
00095 
00096 
00097 int main()
00098 {
00099     max1.initialise(); //initialize thermocouple IC
00100     uLCD.baudrate(3000000); //set LCD baudrate
00101     interrupt.fall(&fallInterrupt);
00102     interrupt.mode(PullUp);
00103     Thread t1(elapsedtime); //run elapsed time counter
00104     Thread t2(thermoread); //read and display temperature from thermocouple
00105     Thread t3(thermoset); // user set temperature
00106 
00107     while(1) { //hystersis program
00108         char* time = watch.getTime();
00109         four_slots.wait(); //lock screen
00110         uLCD.color(0xFFFF00); //set color
00111         uLCD.locate(0,0); //col,row for time
00112         uLCD.printf("Time    | %s",time);//%2d:%2d:%2d", h,m,s); // display time
00113         uLCD.locate(0,5);
00114         uLCD.printf("Set     | %3d",settemp); // current set temperature
00115         uLCD.locate(0,6);
00116         uLCD.printf("Current | %4.2f", ftemperature); // temperature reading
00117         uLCD.locate(0,2);
00118         uLCD.printf("Motor   | "); //motor state in "on" or "off" state
00119         uLCD.locate(0,13);
00120         uLCD.printf("New Temp| %3d",temptemp); //new user temperature
00121         if(motor==0) {
00122             uLCD.locate(11,2); //col,row
00123             uLCD.printf("OFF");
00124         }
00125         if(motor==1) {
00126             uLCD.locate(11,2); //col,row
00127             uLCD.printf("ON ");
00128         }
00129         four_slots.release(); // release screen resource
00130         if(ftemperature < settemp-1) { //condition for 1 degree under
00131             toggle=1; //turn on ssr for nichrome wire
00132         }
00133         if(ftemperature > settemp-1) {
00134             toggle=0; //turn off ssr for nichrome wire
00135         }
00136     }
00137 }