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

Committer:
ericspatterson
Date:
Thu Nov 20 20:30:51 2014 +0000
Revision:
8:1db15ab871a4
Parent:
7:8a0223a951c1
Child:
9:d7300899fd85
Working better, might need to improve "temptemp" variable display a bit

Who changed what in which revision?

UserRevisionLine numberNew contents of line
ericspatterson 0:4fbaafa6574c 1 #include "mbed.h"
ericspatterson 0:4fbaafa6574c 2 #include "uLCD_4DGL.h"
ericspatterson 0:4fbaafa6574c 3 #include "rtos.h"
ericspatterson 0:4fbaafa6574c 4 #include "stdio.h"
ericspatterson 0:4fbaafa6574c 5 #include "max31855.h"
ericspatterson 2:c94244d1cab4 6 #include <mpr121.h>
ericspatterson 8:1db15ab871a4 7 #include "Stopwatch.h"
ericspatterson 2:c94244d1cab4 8
ericspatterson 2:c94244d1cab4 9 DigitalOut led1(LED1);
ericspatterson 2:c94244d1cab4 10 DigitalOut led2(LED2);
ericspatterson 2:c94244d1cab4 11 DigitalOut led3(LED3);
ericspatterson 6:076572f58a5e 12 DigitalOut led4(LED4);
ericspatterson 4:85eb29963721 13 DigitalOut motor(p22); // motor control
ericspatterson 0:4fbaafa6574c 14
ericspatterson 0:4fbaafa6574c 15 uLCD_4DGL uLCD(p9,p10,p11); // serial tx, serial rx, reset pin;
ericspatterson 0:4fbaafa6574c 16 SPI thermoSPI(p5,p6,p7); // setup SPI interface
ericspatterson 0:4fbaafa6574c 17 max31855 max1(thermoSPI, p20); //setup max31855 interface
ericspatterson 0:4fbaafa6574c 18 Mutex lcd_mutex; // mutex to make the lcd lib thread safe
ericspatterson 0:4fbaafa6574c 19 Semaphore four_slots(1); //Activate semaphore
ericspatterson 1:26267401354a 20 DigitalOut toggle(p30); // toggle for Nichrome wire SSR
ericspatterson 1:26267401354a 21 DigitalIn up(p19); // User pushbutton up controller
ericspatterson 1:26267401354a 22 DigitalIn down(p18); // User pushbutton down button controller
ericspatterson 0:4fbaafa6574c 23
ericspatterson 4:85eb29963721 24
ericspatterson 2:c94244d1cab4 25 int settemp = 85; // initial temperature set
ericspatterson 0:4fbaafa6574c 26 float ftemperature = 0; // float variable for temperature
ericspatterson 0:4fbaafa6574c 27 float ctemperature = 0; // float variable for temperature
ericspatterson 8:1db15ab871a4 28 int temptemp=85;
ericspatterson 8:1db15ab871a4 29
ericspatterson 8:1db15ab871a4 30 int key_code=-1;
ericspatterson 8:1db15ab871a4 31 int a =0;
ericspatterson 8:1db15ab871a4 32 int place= 100;
ericspatterson 0:4fbaafa6574c 33
ericspatterson 8:1db15ab871a4 34 int s = 00;
ericspatterson 8:1db15ab871a4 35 int m = 00;
ericspatterson 8:1db15ab871a4 36 int h = 00;
ericspatterson 8:1db15ab871a4 37
ericspatterson 8:1db15ab871a4 38 Stopwatch watch = Stopwatch();
ericspatterson 2:c94244d1cab4 39
ericspatterson 2:c94244d1cab4 40 // Create the interrupt receiver object on pin 26
ericspatterson 2:c94244d1cab4 41 InterruptIn interrupt(p26);
ericspatterson 2:c94244d1cab4 42
ericspatterson 2:c94244d1cab4 43 // Setup the i2c bus on pins 28 and 27
ericspatterson 2:c94244d1cab4 44 I2C i2c(p28, p27);
ericspatterson 2:c94244d1cab4 45
ericspatterson 2:c94244d1cab4 46 // Setup the Mpr121:
ericspatterson 2:c94244d1cab4 47 // constructor(i2c object, i2c address of the mpr121)
ericspatterson 2:c94244d1cab4 48 Mpr121 mpr121(&i2c, Mpr121::ADD_VSS);
ericspatterson 2:c94244d1cab4 49
ericspatterson 1:26267401354a 50 // Thread 1: print the elapsed time on line 1 of the uLCD (i.e., HH:MM:SS)
ericspatterson 6:076572f58a5e 51 void elapsedtime(void const *args) //elapsed timer for when program starts
ericspatterson 6:076572f58a5e 52 {
ericspatterson 8:1db15ab871a4 53 watch.start();
ericspatterson 0:4fbaafa6574c 54 }
ericspatterson 0:4fbaafa6574c 55
ericspatterson 0:4fbaafa6574c 56
ericspatterson 1:26267401354a 57 // Thread 2: print the temperature from the thermocouple - MAX31855 device
ericspatterson 6:076572f58a5e 58 void thermoread(void const *args) //line 2
ericspatterson 6:076572f58a5e 59 {
ericspatterson 6:076572f58a5e 60 while(true) {
ericspatterson 6:076572f58a5e 61 if (max1.ready()==1) {
ericspatterson 0:4fbaafa6574c 62 ctemperature = max1.read_temp(); //Get the reading
ericspatterson 0:4fbaafa6574c 63 ftemperature = (ctemperature)*(9.0/5.0)+32.0;
ericspatterson 8:1db15ab871a4 64 }
ericspatterson 8:1db15ab871a4 65 }
ericspatterson 8:1db15ab871a4 66 }
ericspatterson 8:1db15ab871a4 67
ericspatterson 8:1db15ab871a4 68
ericspatterson 8:1db15ab871a4 69 // Thread 3: reset temp
ericspatterson 8:1db15ab871a4 70 void thermoset(void const *args) //line 2
ericspatterson 8:1db15ab871a4 71 {
ericspatterson 8:1db15ab871a4 72 while(true) {
ericspatterson 8:1db15ab871a4 73 if(up == 1) {
ericspatterson 8:1db15ab871a4 74 a = 0;
ericspatterson 8:1db15ab871a4 75 temptemp=0;
ericspatterson 8:1db15ab871a4 76 while(a<3) {
ericspatterson 8:1db15ab871a4 77 if(key_code>=0 && key_code<=9) {
ericspatterson 8:1db15ab871a4 78 wait(.1);
ericspatterson 8:1db15ab871a4 79 if(a==0)temptemp=key_code*100;
ericspatterson 8:1db15ab871a4 80 if(a==1)temptemp+=key_code*10;
ericspatterson 8:1db15ab871a4 81 if(a==2)temptemp+=key_code*1;
ericspatterson 8:1db15ab871a4 82 a++;
ericspatterson 8:1db15ab871a4 83 key_code = -1;
ericspatterson 8:1db15ab871a4 84 }
ericspatterson 8:1db15ab871a4 85 }
ericspatterson 8:1db15ab871a4 86 settemp=temptemp;
ericspatterson 0:4fbaafa6574c 87 }
ericspatterson 0:4fbaafa6574c 88 }
ericspatterson 0:4fbaafa6574c 89 }
ericspatterson 0:4fbaafa6574c 90
ericspatterson 2:c94244d1cab4 91
ericspatterson 6:076572f58a5e 92 void fallInterrupt()
ericspatterson 6:076572f58a5e 93 {
ericspatterson 6:076572f58a5e 94 int i=0;
ericspatterson 6:076572f58a5e 95 int value=mpr121.read(0x00);
ericspatterson 6:076572f58a5e 96 value +=mpr121.read(0x01)<<8;
ericspatterson 6:076572f58a5e 97 // LED demo mod
ericspatterson 6:076572f58a5e 98 i=0;
ericspatterson 6:076572f58a5e 99 // puts key number out to LEDs for demo
ericspatterson 6:076572f58a5e 100 for (i=0; i<12; i++) {
ericspatterson 6:076572f58a5e 101 if (((value>>i)&0x01)==1) key_code=i;
ericspatterson 6:076572f58a5e 102 }
ericspatterson 6:076572f58a5e 103
ericspatterson 6:076572f58a5e 104 if(key_code== 10) {
ericspatterson 6:076572f58a5e 105 motor = 0;
ericspatterson 6:076572f58a5e 106 }
ericspatterson 6:076572f58a5e 107 if(key_code== 11) {
ericspatterson 6:076572f58a5e 108 motor = 1;
ericspatterson 6:076572f58a5e 109 }
ericspatterson 2:c94244d1cab4 110 }
ericspatterson 2:c94244d1cab4 111
ericspatterson 8:1db15ab871a4 112
ericspatterson 6:076572f58a5e 113 int main()
ericspatterson 6:076572f58a5e 114 {
ericspatterson 6:076572f58a5e 115 max1.initialise(); //initialize thermocouple IC
ericspatterson 6:076572f58a5e 116 uLCD.baudrate(3000000); //set LCD baudrate
ericspatterson 2:c94244d1cab4 117
ericspatterson 6:076572f58a5e 118 interrupt.fall(&fallInterrupt);
ericspatterson 6:076572f58a5e 119 interrupt.mode(PullUp);
ericspatterson 6:076572f58a5e 120
ericspatterson 6:076572f58a5e 121 Thread t1(elapsedtime); //run elapsed time counter
ericspatterson 6:076572f58a5e 122 Thread t2(thermoread); //read and display temperature from thermocouple
ericspatterson 8:1db15ab871a4 123 Thread t3(thermoset); // user set temperature
ericspatterson 6:076572f58a5e 124
ericspatterson 6:076572f58a5e 125 while(1) { //hystersis program
ericspatterson 8:1db15ab871a4 126 char* time = watch.getTime();
ericspatterson 8:1db15ab871a4 127 four_slots.wait(); //lock screen
ericspatterson 8:1db15ab871a4 128 uLCD.color(0xFFFF00); //set color
ericspatterson 6:076572f58a5e 129
ericspatterson 8:1db15ab871a4 130 uLCD.locate(0,0); //col,row for time
ericspatterson 8:1db15ab871a4 131 uLCD.printf("Time | %s",time);//%2d:%2d:%2d", h,m,s); // display time
ericspatterson 6:076572f58a5e 132
ericspatterson 8:1db15ab871a4 133 uLCD.locate(0,5);
ericspatterson 8:1db15ab871a4 134 uLCD.printf("Set | %3d",settemp); // current set temperature
ericspatterson 6:076572f58a5e 135
ericspatterson 8:1db15ab871a4 136 uLCD.locate(0,6);
ericspatterson 8:1db15ab871a4 137 uLCD.printf("Current | %4.2f", ftemperature); // temperature reading
ericspatterson 8:1db15ab871a4 138
ericspatterson 8:1db15ab871a4 139 uLCD.locate(0,2);
ericspatterson 8:1db15ab871a4 140 uLCD.printf("Motor | "); //motor state in "on" or "off" state
ericspatterson 8:1db15ab871a4 141
ericspatterson 8:1db15ab871a4 142 uLCD.locate(0,13);
ericspatterson 8:1db15ab871a4 143 uLCD.printf("New Temp| %3d",temptemp); //new user temperature
ericspatterson 8:1db15ab871a4 144
ericspatterson 8:1db15ab871a4 145 /*if(a==0){
ericspatterson 8:1db15ab871a4 146 uLCD.locate(11,13);
ericspatterson 8:1db15ab871a4 147 uLCD.printf("%d",temptemp); //new user temperature
ericspatterson 0:4fbaafa6574c 148 }
ericspatterson 6:076572f58a5e 149
ericspatterson 8:1db15ab871a4 150 if(a==1){
ericspatterson 8:1db15ab871a4 151 uLCD.locate(11,13);
ericspatterson 8:1db15ab871a4 152 uLCD.printf("%d",temptemp); //new user temperature
ericspatterson 8:1db15ab871a4 153 }
ericspatterson 8:1db15ab871a4 154
ericspatterson 8:1db15ab871a4 155 if(a==2){
ericspatterson 8:1db15ab871a4 156 uLCD.locate(11,13);
ericspatterson 8:1db15ab871a4 157 uLCD.printf("%d",temptemp); //new user temperature
ericspatterson 8:1db15ab871a4 158 }*/
ericspatterson 6:076572f58a5e 159
ericspatterson 8:1db15ab871a4 160 if(motor==0) {
ericspatterson 8:1db15ab871a4 161 uLCD.locate(11,2); //col,row
ericspatterson 8:1db15ab871a4 162 uLCD.printf("OFF");
ericspatterson 6:076572f58a5e 163 }
ericspatterson 8:1db15ab871a4 164 if(motor==1) {
ericspatterson 8:1db15ab871a4 165 uLCD.locate(11,2); //col,row
ericspatterson 8:1db15ab871a4 166 uLCD.printf("ON ");
ericspatterson 8:1db15ab871a4 167 }
ericspatterson 6:076572f58a5e 168
ericspatterson 8:1db15ab871a4 169 four_slots.release(); // release screen resource
ericspatterson 6:076572f58a5e 170
ericspatterson 6:076572f58a5e 171 if(ftemperature < settemp-1) { //condition for 1 degree under
ericspatterson 2:c94244d1cab4 172 toggle=1; //turn on ssr for nichrome wire
ericspatterson 7:8a0223a951c1 173 //toggle=0;
ericspatterson 2:c94244d1cab4 174 }
ericspatterson 8:1db15ab871a4 175 if(ftemperature > settemp-1) {
ericspatterson 7:8a0223a951c1 176 toggle=0; //turn off ssr for nichrome wire
ericspatterson 7:8a0223a951c1 177 }
ericspatterson 0:4fbaafa6574c 178 }
ericspatterson 0:4fbaafa6574c 179 }