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
main.cpp@7:8a0223a951c1, 2014-11-20 (annotated)
- Committer:
- ericspatterson
- Date:
- Thu Nov 20 04:01:51 2014 +0000
- Revision:
- 7:8a0223a951c1
- Parent:
- 6:076572f58a5e
- Child:
- 8:1db15ab871a4
a bit slow, but the controls work now...
Who changed what in which revision?
User | Revision | Line number | New 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 | 2:c94244d1cab4 | 31 | // Create the interrupt receiver object on pin 26 |
ericspatterson | 2:c94244d1cab4 | 32 | InterruptIn interrupt(p26); |
ericspatterson | 2:c94244d1cab4 | 33 | |
ericspatterson | 2:c94244d1cab4 | 34 | // Setup the i2c bus on pins 28 and 27 |
ericspatterson | 2:c94244d1cab4 | 35 | I2C i2c(p28, p27); |
ericspatterson | 2:c94244d1cab4 | 36 | |
ericspatterson | 2:c94244d1cab4 | 37 | // Setup the Mpr121: |
ericspatterson | 2:c94244d1cab4 | 38 | // constructor(i2c object, i2c address of the mpr121) |
ericspatterson | 2:c94244d1cab4 | 39 | Mpr121 mpr121(&i2c, Mpr121::ADD_VSS); |
ericspatterson | 2:c94244d1cab4 | 40 | |
ericspatterson | 1:26267401354a | 41 | // Thread 1: print the elapsed time on line 1 of the uLCD (i.e., HH:MM:SS) |
ericspatterson | 6:076572f58a5e | 42 | void elapsedtime(void const *args) //elapsed timer for when program starts |
ericspatterson | 6:076572f58a5e | 43 | { |
ericspatterson | 1:26267401354a | 44 | int s = 00; //seconds |
ericspatterson | 6:076572f58a5e | 45 | int m = 00; //minutes |
ericspatterson | 1:26267401354a | 46 | int h = 00; //hours |
ericspatterson | 6:076572f58a5e | 47 | |
ericspatterson | 6:076572f58a5e | 48 | while(true) { |
ericspatterson | 6:076572f58a5e | 49 | s++; |
ericspatterson | 6:076572f58a5e | 50 | if(s>59) { |
ericspatterson | 6:076572f58a5e | 51 | s=00; |
ericspatterson | 6:076572f58a5e | 52 | m++; |
ericspatterson | 6:076572f58a5e | 53 | } |
ericspatterson | 6:076572f58a5e | 54 | if(m>59) { |
ericspatterson | 6:076572f58a5e | 55 | m=00; |
ericspatterson | 6:076572f58a5e | 56 | h++; |
ericspatterson | 6:076572f58a5e | 57 | } |
ericspatterson | 6:076572f58a5e | 58 | four_slots.wait(); // lock screen resource |
ericspatterson | 6:076572f58a5e | 59 | //lcd_mutex.lock(); |
ericspatterson | 1:26267401354a | 60 | uLCD.color(0xFFFF00); //set text color |
ericspatterson | 1:26267401354a | 61 | uLCD.locate(0,0); //col,row location of text |
ericspatterson | 6:076572f58a5e | 62 | wait(0.1); |
ericspatterson | 5:4c4a3e2b8582 | 63 | uLCD.printf("Time | %2d:%2d:%2d", h,m,s); // display time |
ericspatterson | 0:4fbaafa6574c | 64 | //lcd_mutex.unlock(); |
ericspatterson | 1:26267401354a | 65 | four_slots.release(); //unlock screen resource |
ericspatterson | 0:4fbaafa6574c | 66 | Thread::wait(1000); // update once per secon |
ericspatterson | 0:4fbaafa6574c | 67 | } |
ericspatterson | 0:4fbaafa6574c | 68 | } |
ericspatterson | 0:4fbaafa6574c | 69 | |
ericspatterson | 0:4fbaafa6574c | 70 | |
ericspatterson | 1:26267401354a | 71 | // Thread 2: print the temperature from the thermocouple - MAX31855 device |
ericspatterson | 6:076572f58a5e | 72 | void thermoread(void const *args) //line 2 |
ericspatterson | 6:076572f58a5e | 73 | { |
ericspatterson | 6:076572f58a5e | 74 | while(true) { |
ericspatterson | 6:076572f58a5e | 75 | if (max1.ready()==1) { |
ericspatterson | 0:4fbaafa6574c | 76 | ctemperature = max1.read_temp(); //Get the reading |
ericspatterson | 0:4fbaafa6574c | 77 | ftemperature = (ctemperature)*(9.0/5.0)+32.0; |
ericspatterson | 6:076572f58a5e | 78 | four_slots.wait(); |
ericspatterson | 0:4fbaafa6574c | 79 | uLCD.color(0xFFFF00); |
ericspatterson | 5:4c4a3e2b8582 | 80 | //uLCD.locate(0,2); //col,row |
ericspatterson | 5:4c4a3e2b8582 | 81 | //wait(0.1); |
ericspatterson | 6:076572f58a5e | 82 | // uLCD.printf("Current Temp. F"); |
ericspatterson | 5:4c4a3e2b8582 | 83 | uLCD.locate(11,6); //col,row |
ericspatterson | 0:4fbaafa6574c | 84 | uLCD.printf("%4.2f", ftemperature); |
ericspatterson | 6:076572f58a5e | 85 | four_slots.release(); |
ericspatterson | 0:4fbaafa6574c | 86 | } |
ericspatterson | 0:4fbaafa6574c | 87 | } |
ericspatterson | 0:4fbaafa6574c | 88 | } |
ericspatterson | 0:4fbaafa6574c | 89 | |
ericspatterson | 2:c94244d1cab4 | 90 | |
ericspatterson | 6:076572f58a5e | 91 | void fallInterrupt() |
ericspatterson | 6:076572f58a5e | 92 | { |
ericspatterson | 6:076572f58a5e | 93 | int i=0; |
ericspatterson | 6:076572f58a5e | 94 | int value=mpr121.read(0x00); |
ericspatterson | 6:076572f58a5e | 95 | value +=mpr121.read(0x01)<<8; |
ericspatterson | 6:076572f58a5e | 96 | // LED demo mod |
ericspatterson | 6:076572f58a5e | 97 | i=0; |
ericspatterson | 6:076572f58a5e | 98 | // puts key number out to LEDs for demo |
ericspatterson | 6:076572f58a5e | 99 | for (i=0; i<12; i++) { |
ericspatterson | 6:076572f58a5e | 100 | if (((value>>i)&0x01)==1) key_code=i; |
ericspatterson | 6:076572f58a5e | 101 | } |
ericspatterson | 6:076572f58a5e | 102 | |
ericspatterson | 6:076572f58a5e | 103 | if(key_code== 10) { |
ericspatterson | 6:076572f58a5e | 104 | motor = 0; |
ericspatterson | 6:076572f58a5e | 105 | |
ericspatterson | 6:076572f58a5e | 106 | |
ericspatterson | 6:076572f58a5e | 107 | } |
ericspatterson | 6:076572f58a5e | 108 | if(key_code== 11) { |
ericspatterson | 6:076572f58a5e | 109 | motor = 1; |
ericspatterson | 6:076572f58a5e | 110 | } |
ericspatterson | 2:c94244d1cab4 | 111 | } |
ericspatterson | 2:c94244d1cab4 | 112 | |
ericspatterson | 6:076572f58a5e | 113 | int a =0; |
ericspatterson | 6:076572f58a5e | 114 | int main() |
ericspatterson | 6:076572f58a5e | 115 | { |
ericspatterson | 6:076572f58a5e | 116 | max1.initialise(); //initialize thermocouple IC |
ericspatterson | 6:076572f58a5e | 117 | uLCD.baudrate(3000000); //set LCD baudrate |
ericspatterson | 2:c94244d1cab4 | 118 | |
ericspatterson | 6:076572f58a5e | 119 | interrupt.fall(&fallInterrupt); |
ericspatterson | 6:076572f58a5e | 120 | interrupt.mode(PullUp); |
ericspatterson | 6:076572f58a5e | 121 | |
ericspatterson | 6:076572f58a5e | 122 | Thread t1(elapsedtime); //run elapsed time counter |
ericspatterson | 6:076572f58a5e | 123 | Thread t2(thermoread); //read and display temperature from thermocouple |
ericspatterson | 6:076572f58a5e | 124 | |
ericspatterson | 6:076572f58a5e | 125 | while(1) { //hystersis program |
ericspatterson | 6:076572f58a5e | 126 | key_code = 0; |
ericspatterson | 6:076572f58a5e | 127 | |
ericspatterson | 6:076572f58a5e | 128 | four_slots.wait(); |
ericspatterson | 5:4c4a3e2b8582 | 129 | uLCD.color(0xFFFF00); |
ericspatterson | 5:4c4a3e2b8582 | 130 | uLCD.locate(0,5); //col,row |
ericspatterson | 5:4c4a3e2b8582 | 131 | uLCD.printf("Set | "); |
ericspatterson | 5:4c4a3e2b8582 | 132 | uLCD.locate(0,6); //col,row |
ericspatterson | 5:4c4a3e2b8582 | 133 | uLCD.printf("Current | "); |
ericspatterson | 5:4c4a3e2b8582 | 134 | uLCD.locate(0,2); //col,row |
ericspatterson | 6:076572f58a5e | 135 | uLCD.printf("Motor | "); |
ericspatterson | 6:076572f58a5e | 136 | four_slots.release(); |
ericspatterson | 6:076572f58a5e | 137 | |
ericspatterson | 6:076572f58a5e | 138 | int place= 100; |
ericspatterson | 6:076572f58a5e | 139 | |
ericspatterson | 6:076572f58a5e | 140 | if(motor==0) { |
ericspatterson | 6:076572f58a5e | 141 | four_slots.wait(); |
ericspatterson | 6:076572f58a5e | 142 | uLCD.color(0xFFFF00); |
ericspatterson | 5:4c4a3e2b8582 | 143 | uLCD.locate(11,2); //col,row |
ericspatterson | 5:4c4a3e2b8582 | 144 | uLCD.printf("OFF"); |
ericspatterson | 4:85eb29963721 | 145 | four_slots.release(); |
ericspatterson | 4:85eb29963721 | 146 | } |
ericspatterson | 6:076572f58a5e | 147 | if(motor==1) { |
ericspatterson | 6:076572f58a5e | 148 | four_slots.wait(); |
ericspatterson | 6:076572f58a5e | 149 | uLCD.color(0xFFFF00); |
ericspatterson | 5:4c4a3e2b8582 | 150 | uLCD.locate(11,2); //col,row |
ericspatterson | 6:076572f58a5e | 151 | uLCD.printf("ON "); |
ericspatterson | 4:85eb29963721 | 152 | four_slots.release(); |
ericspatterson | 0:4fbaafa6574c | 153 | } |
ericspatterson | 6:076572f58a5e | 154 | |
ericspatterson | 7:8a0223a951c1 | 155 | if(up == 1) { |
ericspatterson | 7:8a0223a951c1 | 156 | wait(.5); |
ericspatterson | 7:8a0223a951c1 | 157 | a = 0; |
ericspatterson | 7:8a0223a951c1 | 158 | temptemp=0; |
ericspatterson | 7:8a0223a951c1 | 159 | four_slots.wait(); |
ericspatterson | 7:8a0223a951c1 | 160 | uLCD.color(0xFFFF00); |
ericspatterson | 7:8a0223a951c1 | 161 | uLCD.locate(0,14); //col,row |
ericspatterson | 7:8a0223a951c1 | 162 | uLCD.printf("%3d",temptemp); |
ericspatterson | 7:8a0223a951c1 | 163 | four_slots.release(); |
ericspatterson | 6:076572f58a5e | 164 | |
ericspatterson | 7:8a0223a951c1 | 165 | while(a<3) { |
ericspatterson | 7:8a0223a951c1 | 166 | if(key_code>=0 && key_code<=9) { |
ericspatterson | 7:8a0223a951c1 | 167 | wait(1); |
ericspatterson | 7:8a0223a951c1 | 168 | temptemp+=place*key_code; |
ericspatterson | 7:8a0223a951c1 | 169 | place=place/10; |
ericspatterson | 7:8a0223a951c1 | 170 | four_slots.wait(); |
ericspatterson | 7:8a0223a951c1 | 171 | uLCD.color(0xFFFF00); |
ericspatterson | 7:8a0223a951c1 | 172 | uLCD.locate(0,13); //col,row |
ericspatterson | 7:8a0223a951c1 | 173 | uLCD.printf("intermediate value"); |
ericspatterson | 7:8a0223a951c1 | 174 | uLCD.locate(0,14); //col,row |
ericspatterson | 7:8a0223a951c1 | 175 | uLCD.printf("%3d",temptemp); |
ericspatterson | 7:8a0223a951c1 | 176 | four_slots.release(); |
ericspatterson | 7:8a0223a951c1 | 177 | a++; |
ericspatterson | 7:8a0223a951c1 | 178 | key_code = -1; |
ericspatterson | 7:8a0223a951c1 | 179 | //wait(2); |
ericspatterson | 7:8a0223a951c1 | 180 | } |
ericspatterson | 6:076572f58a5e | 181 | } |
ericspatterson | 6:076572f58a5e | 182 | } |
ericspatterson | 6:076572f58a5e | 183 | settemp=temptemp; |
ericspatterson | 6:076572f58a5e | 184 | four_slots.wait(); |
ericspatterson | 6:076572f58a5e | 185 | uLCD.color(0xFFFF00); |
ericspatterson | 6:076572f58a5e | 186 | uLCD.locate(11,5); //col,row |
ericspatterson | 6:076572f58a5e | 187 | uLCD.printf("%3d",settemp); |
ericspatterson | 6:076572f58a5e | 188 | four_slots.release(); |
ericspatterson | 7:8a0223a951c1 | 189 | //wait(2); |
ericspatterson | 6:076572f58a5e | 190 | |
ericspatterson | 6:076572f58a5e | 191 | |
ericspatterson | 6:076572f58a5e | 192 | |
ericspatterson | 6:076572f58a5e | 193 | if(ftemperature < settemp-1) { //condition for 1 degree under |
ericspatterson | 2:c94244d1cab4 | 194 | toggle=1; //turn on ssr for nichrome wire |
ericspatterson | 7:8a0223a951c1 | 195 | //toggle=0; |
ericspatterson | 2:c94244d1cab4 | 196 | } |
ericspatterson | 7:8a0223a951c1 | 197 | if(ftemperature > settemp-1) { //condition for 1 degree over |
ericspatterson | 7:8a0223a951c1 | 198 | toggle=0; //turn off ssr for nichrome wire |
ericspatterson | 7:8a0223a951c1 | 199 | } |
ericspatterson | 2:c94244d1cab4 | 200 | // wait(.1); //check every 1 second |
ericspatterson | 0:4fbaafa6574c | 201 | } |
ericspatterson | 0:4fbaafa6574c | 202 | } |