Project
Dependencies: 4DGL-uLCD-SE MAX31855 mbed-rtos mbed
Fork of Coffee_Roaster by
main.cpp@5:4c4a3e2b8582, 2014-11-19 (annotated)
- Committer:
- ericspatterson
- Date:
- Wed Nov 19 21:43:51 2014 +0000
- Revision:
- 5:4c4a3e2b8582
- Parent:
- 4:85eb29963721
- Child:
- 6:076572f58a5e
setting controls
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 | 4:85eb29963721 | 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 | 0:4fbaafa6574c | 27 | |
ericspatterson | 2:c94244d1cab4 | 28 | int key_code=0; |
ericspatterson | 2:c94244d1cab4 | 29 | |
ericspatterson | 4:85eb29963721 | 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 | 1:26267401354a | 42 | void elapsedtime(void const *args) { //elapsed timer for when program starts |
ericspatterson | 1:26267401354a | 43 | int s = 00; //seconds |
ericspatterson | 1:26267401354a | 44 | int m = 00; //minutes |
ericspatterson | 1:26267401354a | 45 | int h = 00; //hours |
ericspatterson | 0:4fbaafa6574c | 46 | |
ericspatterson | 0:4fbaafa6574c | 47 | while(true){ |
ericspatterson | 1:26267401354a | 48 | s++; |
ericspatterson | 0:4fbaafa6574c | 49 | if(s>59){ |
ericspatterson | 0:4fbaafa6574c | 50 | s=00; |
ericspatterson | 0:4fbaafa6574c | 51 | m++; |
ericspatterson | 0:4fbaafa6574c | 52 | } |
ericspatterson | 0:4fbaafa6574c | 53 | if(m>59){ |
ericspatterson | 0:4fbaafa6574c | 54 | m=00; |
ericspatterson | 0:4fbaafa6574c | 55 | h++; |
ericspatterson | 0:4fbaafa6574c | 56 | } |
ericspatterson | 1:26267401354a | 57 | four_slots.wait(); // lock screen resource |
ericspatterson | 0:4fbaafa6574c | 58 | //lcd_mutex.lock(); |
ericspatterson | 1:26267401354a | 59 | uLCD.color(0xFFFF00); //set text color |
ericspatterson | 1:26267401354a | 60 | uLCD.locate(0,0); //col,row location of text |
ericspatterson | 1:26267401354a | 61 | wait(0.1); |
ericspatterson | 5:4c4a3e2b8582 | 62 | uLCD.printf("Time | %2d:%2d:%2d", h,m,s); // display time |
ericspatterson | 0:4fbaafa6574c | 63 | //lcd_mutex.unlock(); |
ericspatterson | 1:26267401354a | 64 | four_slots.release(); //unlock screen resource |
ericspatterson | 0:4fbaafa6574c | 65 | Thread::wait(1000); // update once per secon |
ericspatterson | 0:4fbaafa6574c | 66 | } |
ericspatterson | 0:4fbaafa6574c | 67 | } |
ericspatterson | 0:4fbaafa6574c | 68 | |
ericspatterson | 0:4fbaafa6574c | 69 | |
ericspatterson | 1:26267401354a | 70 | // Thread 2: print the temperature from the thermocouple - MAX31855 device |
ericspatterson | 0:4fbaafa6574c | 71 | void thermoread(void const *args) { //line 2 |
ericspatterson | 0:4fbaafa6574c | 72 | while(true){ |
ericspatterson | 0:4fbaafa6574c | 73 | if (max1.ready()==1){ |
ericspatterson | 0:4fbaafa6574c | 74 | ctemperature = max1.read_temp(); //Get the reading |
ericspatterson | 0:4fbaafa6574c | 75 | ftemperature = (ctemperature)*(9.0/5.0)+32.0; |
ericspatterson | 0:4fbaafa6574c | 76 | four_slots.wait(); |
ericspatterson | 0:4fbaafa6574c | 77 | uLCD.color(0xFFFF00); |
ericspatterson | 5:4c4a3e2b8582 | 78 | //uLCD.locate(0,2); //col,row |
ericspatterson | 5:4c4a3e2b8582 | 79 | //wait(0.1); |
ericspatterson | 5:4c4a3e2b8582 | 80 | // uLCD.printf("Current Temp. F"); |
ericspatterson | 5:4c4a3e2b8582 | 81 | uLCD.locate(11,6); //col,row |
ericspatterson | 0:4fbaafa6574c | 82 | uLCD.printf("%4.2f", ftemperature); |
ericspatterson | 0:4fbaafa6574c | 83 | four_slots.release(); |
ericspatterson | 0:4fbaafa6574c | 84 | } |
ericspatterson | 0:4fbaafa6574c | 85 | } |
ericspatterson | 0:4fbaafa6574c | 86 | } |
ericspatterson | 0:4fbaafa6574c | 87 | |
ericspatterson | 2:c94244d1cab4 | 88 | |
ericspatterson | 2:c94244d1cab4 | 89 | void fallInterrupt() { |
ericspatterson | 2:c94244d1cab4 | 90 | int i=0; |
ericspatterson | 2:c94244d1cab4 | 91 | int value=mpr121.read(0x00); |
ericspatterson | 2:c94244d1cab4 | 92 | value +=mpr121.read(0x01)<<8; |
ericspatterson | 2:c94244d1cab4 | 93 | // LED demo mod |
ericspatterson | 2:c94244d1cab4 | 94 | i=0; |
ericspatterson | 2:c94244d1cab4 | 95 | // puts key number out to LEDs for demo |
ericspatterson | 2:c94244d1cab4 | 96 | for (i=0; i<12; i++) { |
ericspatterson | 2:c94244d1cab4 | 97 | if (((value>>i)&0x01)==1) key_code=i+1; |
ericspatterson | 2:c94244d1cab4 | 98 | } |
ericspatterson | 2:c94244d1cab4 | 99 | } |
ericspatterson | 2:c94244d1cab4 | 100 | |
ericspatterson | 2:c94244d1cab4 | 101 | |
ericspatterson | 2:c94244d1cab4 | 102 | int main() { |
ericspatterson | 2:c94244d1cab4 | 103 | max1.initialise(); //initialize thermocouple IC |
ericspatterson | 2:c94244d1cab4 | 104 | uLCD.baudrate(3000000); //set LCD baudrate |
ericspatterson | 2:c94244d1cab4 | 105 | |
ericspatterson | 5:4c4a3e2b8582 | 106 | // four_slots.wait(); |
ericspatterson | 5:4c4a3e2b8582 | 107 | uLCD.color(0xFFFF00); |
ericspatterson | 5:4c4a3e2b8582 | 108 | uLCD.locate(0,5); //col,row |
ericspatterson | 5:4c4a3e2b8582 | 109 | uLCD.printf("Set | "); |
ericspatterson | 5:4c4a3e2b8582 | 110 | uLCD.locate(0,6); //col,row |
ericspatterson | 5:4c4a3e2b8582 | 111 | uLCD.printf("Current | "); |
ericspatterson | 5:4c4a3e2b8582 | 112 | uLCD.locate(11,5); //col,row |
ericspatterson | 5:4c4a3e2b8582 | 113 | uLCD.printf("85"); |
ericspatterson | 5:4c4a3e2b8582 | 114 | uLCD.locate(0,2); //col,row |
ericspatterson | 5:4c4a3e2b8582 | 115 | uLCD.printf("Motor | OFF"); |
ericspatterson | 5:4c4a3e2b8582 | 116 | // four_slots.release(); |
ericspatterson | 5:4c4a3e2b8582 | 117 | |
ericspatterson | 2:c94244d1cab4 | 118 | interrupt.fall(&fallInterrupt); |
ericspatterson | 2:c94244d1cab4 | 119 | interrupt.mode(PullUp); |
ericspatterson | 2:c94244d1cab4 | 120 | |
ericspatterson | 2:c94244d1cab4 | 121 | Thread t1(elapsedtime); //run elapsed time counter |
ericspatterson | 2:c94244d1cab4 | 122 | Thread t2(thermoread); //read and display temperature from thermocouple |
ericspatterson | 2:c94244d1cab4 | 123 | |
ericspatterson | 2:c94244d1cab4 | 124 | while(1){ //hystersis program |
ericspatterson | 2:c94244d1cab4 | 125 | key_code = 0; |
ericspatterson | 5:4c4a3e2b8582 | 126 | |
ericspatterson | 5:4c4a3e2b8582 | 127 | |
ericspatterson | 2:c94244d1cab4 | 128 | |
ericspatterson | 4:85eb29963721 | 129 | if(key_code == 2){ |
ericspatterson | 0:4fbaafa6574c | 130 | settemp += 1; |
ericspatterson | 0:4fbaafa6574c | 131 | four_slots.wait(); |
ericspatterson | 0:4fbaafa6574c | 132 | uLCD.color(0xFFFF00); |
ericspatterson | 5:4c4a3e2b8582 | 133 | uLCD.locate(11,5); //col,row |
ericspatterson | 0:4fbaafa6574c | 134 | uLCD.printf("%d",settemp); |
ericspatterson | 4:85eb29963721 | 135 | four_slots.release(); |
ericspatterson | 4:85eb29963721 | 136 | wait(2); |
ericspatterson | 0:4fbaafa6574c | 137 | } |
ericspatterson | 4:85eb29963721 | 138 | if(key_code== 1){ |
ericspatterson | 0:4fbaafa6574c | 139 | settemp -= 1; |
ericspatterson | 0:4fbaafa6574c | 140 | four_slots.wait(); |
ericspatterson | 0:4fbaafa6574c | 141 | uLCD.color(0xFFFF00); |
ericspatterson | 5:4c4a3e2b8582 | 142 | uLCD.locate(11,5); //col,row |
ericspatterson | 0:4fbaafa6574c | 143 | uLCD.printf("%d",settemp); |
ericspatterson | 0:4fbaafa6574c | 144 | four_slots.release(); |
ericspatterson | 4:85eb29963721 | 145 | } |
ericspatterson | 4:85eb29963721 | 146 | |
ericspatterson | 4:85eb29963721 | 147 | if(key_code== 9){ |
ericspatterson | 4:85eb29963721 | 148 | motor = 0; |
ericspatterson | 4:85eb29963721 | 149 | settemp -= 1; |
ericspatterson | 4:85eb29963721 | 150 | four_slots.wait(); |
ericspatterson | 4:85eb29963721 | 151 | uLCD.color(0xFFFF00); |
ericspatterson | 5:4c4a3e2b8582 | 152 | uLCD.locate(11,2); //col,row |
ericspatterson | 5:4c4a3e2b8582 | 153 | uLCD.printf("OFF"); |
ericspatterson | 4:85eb29963721 | 154 | four_slots.release(); |
ericspatterson | 4:85eb29963721 | 155 | } |
ericspatterson | 4:85eb29963721 | 156 | if(key_code== 10){ |
ericspatterson | 4:85eb29963721 | 157 | motor = 1; |
ericspatterson | 4:85eb29963721 | 158 | settemp -= 1; |
ericspatterson | 4:85eb29963721 | 159 | four_slots.wait(); |
ericspatterson | 4:85eb29963721 | 160 | uLCD.color(0xFFFF00); |
ericspatterson | 5:4c4a3e2b8582 | 161 | uLCD.locate(11,2); //col,row |
ericspatterson | 5:4c4a3e2b8582 | 162 | uLCD.printf(" ON "); |
ericspatterson | 4:85eb29963721 | 163 | four_slots.release(); |
ericspatterson | 0:4fbaafa6574c | 164 | } |
ericspatterson | 2:c94244d1cab4 | 165 | |
ericspatterson | 2:c94244d1cab4 | 166 | if(ftemperature < settemp-1){ //condition for 1 degree under |
ericspatterson | 2:c94244d1cab4 | 167 | toggle=1; //turn on ssr for nichrome wire |
ericspatterson | 5:4c4a3e2b8582 | 168 | toggle=0; |
ericspatterson | 2:c94244d1cab4 | 169 | } |
ericspatterson | 5:4c4a3e2b8582 | 170 | // if(ftemperature > settemp-1){ //condition for 1 degree over |
ericspatterson | 5:4c4a3e2b8582 | 171 | // toggle=0; //turn off ssr for nichrome wire |
ericspatterson | 5:4c4a3e2b8582 | 172 | // } |
ericspatterson | 2:c94244d1cab4 | 173 | // wait(.1); //check every 1 second |
ericspatterson | 0:4fbaafa6574c | 174 | } |
ericspatterson | 0:4fbaafa6574c | 175 | } |