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:
Wed Nov 19 23:34:27 2014 +0000
Revision:
6:076572f58a5e
Parent:
5:4c4a3e2b8582
Child:
7:8a0223a951c1
progress on controls, motor not working

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 2:c94244d1cab4 7
ericspatterson 2:c94244d1cab4 8 DigitalOut led1(LED1);
ericspatterson 2:c94244d1cab4 9 DigitalOut led2(LED2);
ericspatterson 2:c94244d1cab4 10 DigitalOut led3(LED3);
ericspatterson 6:076572f58a5e 11 DigitalOut led4(LED4);
ericspatterson 4:85eb29963721 12 DigitalOut motor(p22); // motor control
ericspatterson 0:4fbaafa6574c 13
ericspatterson 0:4fbaafa6574c 14 uLCD_4DGL uLCD(p9,p10,p11); // serial tx, serial rx, reset pin;
ericspatterson 0:4fbaafa6574c 15 SPI thermoSPI(p5,p6,p7); // setup SPI interface
ericspatterson 0:4fbaafa6574c 16 max31855 max1(thermoSPI, p20); //setup max31855 interface
ericspatterson 0:4fbaafa6574c 17 Mutex lcd_mutex; // mutex to make the lcd lib thread safe
ericspatterson 0:4fbaafa6574c 18 Semaphore four_slots(1); //Activate semaphore
ericspatterson 1:26267401354a 19 DigitalOut toggle(p30); // toggle for Nichrome wire SSR
ericspatterson 1:26267401354a 20 DigitalIn up(p19); // User pushbutton up controller
ericspatterson 1:26267401354a 21 DigitalIn down(p18); // User pushbutton down button controller
ericspatterson 0:4fbaafa6574c 22
ericspatterson 4:85eb29963721 23
ericspatterson 2:c94244d1cab4 24 int settemp = 85; // initial temperature set
ericspatterson 0:4fbaafa6574c 25 float ftemperature = 0; // float variable for temperature
ericspatterson 0:4fbaafa6574c 26 float ctemperature = 0; // float variable for temperature
ericspatterson 6:076572f58a5e 27 int temptemp=0;
ericspatterson 0:4fbaafa6574c 28
ericspatterson 2:c94244d1cab4 29 int key_code=0;
ericspatterson 2:c94244d1cab4 30
ericspatterson 4:85eb29963721 31
ericspatterson 2:c94244d1cab4 32 // Create the interrupt receiver object on pin 26
ericspatterson 2:c94244d1cab4 33 InterruptIn interrupt(p26);
ericspatterson 2:c94244d1cab4 34
ericspatterson 2:c94244d1cab4 35 // Setup the i2c bus on pins 28 and 27
ericspatterson 2:c94244d1cab4 36 I2C i2c(p28, p27);
ericspatterson 2:c94244d1cab4 37
ericspatterson 2:c94244d1cab4 38 // Setup the Mpr121:
ericspatterson 2:c94244d1cab4 39 // constructor(i2c object, i2c address of the mpr121)
ericspatterson 2:c94244d1cab4 40 Mpr121 mpr121(&i2c, Mpr121::ADD_VSS);
ericspatterson 2:c94244d1cab4 41
ericspatterson 1:26267401354a 42 // Thread 1: print the elapsed time on line 1 of the uLCD (i.e., HH:MM:SS)
ericspatterson 6:076572f58a5e 43 void elapsedtime(void const *args) //elapsed timer for when program starts
ericspatterson 6:076572f58a5e 44 {
ericspatterson 1:26267401354a 45 int s = 00; //seconds
ericspatterson 6:076572f58a5e 46 int m = 00; //minutes
ericspatterson 1:26267401354a 47 int h = 00; //hours
ericspatterson 6:076572f58a5e 48
ericspatterson 6:076572f58a5e 49 while(true) {
ericspatterson 6:076572f58a5e 50 s++;
ericspatterson 6:076572f58a5e 51 if(s>59) {
ericspatterson 6:076572f58a5e 52 s=00;
ericspatterson 6:076572f58a5e 53 m++;
ericspatterson 6:076572f58a5e 54 }
ericspatterson 6:076572f58a5e 55 if(m>59) {
ericspatterson 6:076572f58a5e 56 m=00;
ericspatterson 6:076572f58a5e 57 h++;
ericspatterson 6:076572f58a5e 58 }
ericspatterson 6:076572f58a5e 59 four_slots.wait(); // lock screen resource
ericspatterson 6:076572f58a5e 60 //lcd_mutex.lock();
ericspatterson 1:26267401354a 61 uLCD.color(0xFFFF00); //set text color
ericspatterson 1:26267401354a 62 uLCD.locate(0,0); //col,row location of text
ericspatterson 6:076572f58a5e 63 wait(0.1);
ericspatterson 5:4c4a3e2b8582 64 uLCD.printf("Time | %2d:%2d:%2d", h,m,s); // display time
ericspatterson 0:4fbaafa6574c 65 //lcd_mutex.unlock();
ericspatterson 1:26267401354a 66 four_slots.release(); //unlock screen resource
ericspatterson 0:4fbaafa6574c 67 Thread::wait(1000); // update once per secon
ericspatterson 0:4fbaafa6574c 68 }
ericspatterson 0:4fbaafa6574c 69 }
ericspatterson 0:4fbaafa6574c 70
ericspatterson 0:4fbaafa6574c 71
ericspatterson 1:26267401354a 72 // Thread 2: print the temperature from the thermocouple - MAX31855 device
ericspatterson 6:076572f58a5e 73 void thermoread(void const *args) //line 2
ericspatterson 6:076572f58a5e 74 {
ericspatterson 6:076572f58a5e 75 while(true) {
ericspatterson 6:076572f58a5e 76 if (max1.ready()==1) {
ericspatterson 0:4fbaafa6574c 77 ctemperature = max1.read_temp(); //Get the reading
ericspatterson 0:4fbaafa6574c 78 ftemperature = (ctemperature)*(9.0/5.0)+32.0;
ericspatterson 6:076572f58a5e 79 four_slots.wait();
ericspatterson 0:4fbaafa6574c 80 uLCD.color(0xFFFF00);
ericspatterson 5:4c4a3e2b8582 81 //uLCD.locate(0,2); //col,row
ericspatterson 5:4c4a3e2b8582 82 //wait(0.1);
ericspatterson 6:076572f58a5e 83 // uLCD.printf("Current Temp. F");
ericspatterson 5:4c4a3e2b8582 84 uLCD.locate(11,6); //col,row
ericspatterson 0:4fbaafa6574c 85 uLCD.printf("%4.2f", ftemperature);
ericspatterson 6:076572f58a5e 86 four_slots.release();
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
ericspatterson 6:076572f58a5e 108 }
ericspatterson 6:076572f58a5e 109 if(key_code== 11) {
ericspatterson 6:076572f58a5e 110 motor = 1;
ericspatterson 6:076572f58a5e 111 }
ericspatterson 2:c94244d1cab4 112 }
ericspatterson 2:c94244d1cab4 113
ericspatterson 6:076572f58a5e 114 int a =0;
ericspatterson 6:076572f58a5e 115 int main()
ericspatterson 6:076572f58a5e 116 {
ericspatterson 6:076572f58a5e 117 max1.initialise(); //initialize thermocouple IC
ericspatterson 6:076572f58a5e 118 uLCD.baudrate(3000000); //set LCD baudrate
ericspatterson 2:c94244d1cab4 119
ericspatterson 6:076572f58a5e 120
ericspatterson 6:076572f58a5e 121
ericspatterson 6:076572f58a5e 122 interrupt.fall(&fallInterrupt);
ericspatterson 6:076572f58a5e 123 interrupt.mode(PullUp);
ericspatterson 6:076572f58a5e 124
ericspatterson 6:076572f58a5e 125 Thread t1(elapsedtime); //run elapsed time counter
ericspatterson 6:076572f58a5e 126 Thread t2(thermoread); //read and display temperature from thermocouple
ericspatterson 6:076572f58a5e 127
ericspatterson 6:076572f58a5e 128 while(1) { //hystersis program
ericspatterson 6:076572f58a5e 129 key_code = 0;
ericspatterson 6:076572f58a5e 130
ericspatterson 6:076572f58a5e 131 four_slots.wait();
ericspatterson 5:4c4a3e2b8582 132 uLCD.color(0xFFFF00);
ericspatterson 5:4c4a3e2b8582 133 uLCD.locate(0,5); //col,row
ericspatterson 5:4c4a3e2b8582 134 uLCD.printf("Set | ");
ericspatterson 5:4c4a3e2b8582 135 uLCD.locate(0,6); //col,row
ericspatterson 5:4c4a3e2b8582 136 uLCD.printf("Current | ");
ericspatterson 5:4c4a3e2b8582 137 uLCD.locate(0,2); //col,row
ericspatterson 6:076572f58a5e 138 uLCD.printf("Motor | ");
ericspatterson 6:076572f58a5e 139 four_slots.release();
ericspatterson 6:076572f58a5e 140
ericspatterson 6:076572f58a5e 141 int place= 100;
ericspatterson 6:076572f58a5e 142
ericspatterson 6:076572f58a5e 143 if(motor==0) {
ericspatterson 6:076572f58a5e 144 four_slots.wait();
ericspatterson 6:076572f58a5e 145 uLCD.color(0xFFFF00);
ericspatterson 5:4c4a3e2b8582 146 uLCD.locate(11,2); //col,row
ericspatterson 5:4c4a3e2b8582 147 uLCD.printf("OFF");
ericspatterson 4:85eb29963721 148 four_slots.release();
ericspatterson 4:85eb29963721 149 }
ericspatterson 6:076572f58a5e 150 if(motor==1) {
ericspatterson 6:076572f58a5e 151 four_slots.wait();
ericspatterson 6:076572f58a5e 152 uLCD.color(0xFFFF00);
ericspatterson 5:4c4a3e2b8582 153 uLCD.locate(11,2); //col,row
ericspatterson 6:076572f58a5e 154 uLCD.printf("ON ");
ericspatterson 4:85eb29963721 155 four_slots.release();
ericspatterson 0:4fbaafa6574c 156 }
ericspatterson 6:076572f58a5e 157
ericspatterson 6:076572f58a5e 158
ericspatterson 6:076572f58a5e 159 if(a<3) {
ericspatterson 6:076572f58a5e 160 //key_code = -1;
ericspatterson 6:076572f58a5e 161 if(key_code>=0 && key_code<=9) {
ericspatterson 6:076572f58a5e 162 temptemp+=place*key_code;
ericspatterson 6:076572f58a5e 163 place=place/10;
ericspatterson 6:076572f58a5e 164 four_slots.wait();
ericspatterson 6:076572f58a5e 165 uLCD.color(0xFFFF00);
ericspatterson 6:076572f58a5e 166 uLCD.locate(0,13); //col,row
ericspatterson 6:076572f58a5e 167 uLCD.printf("intermediate value");
ericspatterson 6:076572f58a5e 168 uLCD.locate(0,14); //col,row
ericspatterson 6:076572f58a5e 169 uLCD.printf("%3d",temptemp);
ericspatterson 6:076572f58a5e 170 four_slots.release();
ericspatterson 6:076572f58a5e 171 a++;
ericspatterson 6:076572f58a5e 172 wait(2);
ericspatterson 6:076572f58a5e 173 }
ericspatterson 6:076572f58a5e 174 }
ericspatterson 6:076572f58a5e 175 if(up == 1) {
ericspatterson 6:076572f58a5e 176 a = 0;
ericspatterson 6:076572f58a5e 177 }
ericspatterson 6:076572f58a5e 178 settemp=temptemp;
ericspatterson 6:076572f58a5e 179 temptemp=0;
ericspatterson 6:076572f58a5e 180 four_slots.wait();
ericspatterson 6:076572f58a5e 181 uLCD.color(0xFFFF00);
ericspatterson 6:076572f58a5e 182 uLCD.locate(11,5); //col,row
ericspatterson 6:076572f58a5e 183 uLCD.printf("%3d",settemp);
ericspatterson 6:076572f58a5e 184 four_slots.release();
ericspatterson 6:076572f58a5e 185 wait(2);
ericspatterson 6:076572f58a5e 186
ericspatterson 6:076572f58a5e 187
ericspatterson 6:076572f58a5e 188
ericspatterson 6:076572f58a5e 189 if(ftemperature < settemp-1) { //condition for 1 degree under
ericspatterson 2:c94244d1cab4 190 toggle=1; //turn on ssr for nichrome wire
ericspatterson 5:4c4a3e2b8582 191 toggle=0;
ericspatterson 2:c94244d1cab4 192 }
ericspatterson 6:076572f58a5e 193 // if(ftemperature > settemp-1){ //condition for 1 degree over
ericspatterson 5:4c4a3e2b8582 194 // toggle=0; //turn off ssr for nichrome wire
ericspatterson 6:076572f58a5e 195 // }
ericspatterson 2:c94244d1cab4 196 // wait(.1); //check every 1 second
ericspatterson 0:4fbaafa6574c 197 }
ericspatterson 0:4fbaafa6574c 198 }