Temperature Sensor Project, ammended from cookbook
Dependencies: mbed C12832 LM75B
main.cpp@6:3a4fb916a217, 2020-08-26 (annotated)
- Committer:
- saltire78
- Date:
- Wed Aug 26 22:39:41 2020 +0000
- Revision:
- 6:3a4fb916a217
- Parent:
- 5:608f2bf4d3f7
Temperature Sensor Project, ammended from cookbook recipe
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
okano | 0:ce7a8546502b | 1 | #include "mbed.h" |
chris | 2:9e757151de9b | 2 | #include "LM75B.h" |
chris | 5:608f2bf4d3f7 | 3 | #include "C12832.h" |
okano | 0:ce7a8546502b | 4 | |
chris | 5:608f2bf4d3f7 | 5 | C12832 lcd(p5, p7, p6, p8, p11); |
chris | 5:608f2bf4d3f7 | 6 | |
chris | 4:6df97cb10041 | 7 | LM75B sensor(p28,p27); |
chris | 4:6df97cb10041 | 8 | Serial pc(USBTX,USBRX); |
saltire78 | 6:3a4fb916a217 | 9 | DigitalIn JoyLeft(p13); |
saltire78 | 6:3a4fb916a217 | 10 | DigitalIn JoyRight(p16); |
saltire78 | 6:3a4fb916a217 | 11 | DigitalIn JoyButton(p14); |
saltire78 | 6:3a4fb916a217 | 12 | DigitalOut AlarmLED(LED1); |
okano | 0:ce7a8546502b | 13 | |
saltire78 | 6:3a4fb916a217 | 14 | int main (){ |
saltire78 | 6:3a4fb916a217 | 15 | lcd.cls(); //clear LCD screen |
saltire78 | 6:3a4fb916a217 | 16 | lcd.locate(0,0); //home the lcd screen |
saltire78 | 6:3a4fb916a217 | 17 | lcd.printf("Killian's LM75 Temperature Sensor Detection"); //Text |
saltire78 | 6:3a4fb916a217 | 18 | wait(2); //pause for visibility |
saltire78 | 6:3a4fb916a217 | 19 | int tempselect=0; //set temp selection as undetermined |
saltire78 | 6:3a4fb916a217 | 20 | |
saltire78 | 6:3a4fb916a217 | 21 | while (tempselect==0){ //while temp selection undetermined |
saltire78 | 6:3a4fb916a217 | 22 | lcd.cls(); //clear LCD screen |
saltire78 | 6:3a4fb916a217 | 23 | lcd.locate(0,0); //home the lcd screen |
saltire78 | 6:3a4fb916a217 | 24 | lcd.printf("Deg. C = Joystick <\nDeg. F = Joystick >\nBoth = Joystick Button"); |
saltire78 | 6:3a4fb916a217 | 25 | if(JoyLeft==1 and JoyRight==0 and JoyButton==0){ |
saltire78 | 6:3a4fb916a217 | 26 | lcd.cls(); //clear LCD screen |
saltire78 | 6:3a4fb916a217 | 27 | lcd.locate(0,0); //home the lcd screen |
saltire78 | 6:3a4fb916a217 | 28 | lcd.printf("Deg. C Selected\nPlease Wait..."); //selection text |
saltire78 | 6:3a4fb916a217 | 29 | tempselect=1; //ammend selection constant - breaks while loop |
saltire78 | 6:3a4fb916a217 | 30 | wait(1); //pause for visibility and debounce |
saltire78 | 6:3a4fb916a217 | 31 | } |
saltire78 | 6:3a4fb916a217 | 32 | else if(JoyLeft==0 and JoyRight==1 and JoyButton==0){ |
saltire78 | 6:3a4fb916a217 | 33 | lcd.cls(); //clear LCD screen |
saltire78 | 6:3a4fb916a217 | 34 | lcd.locate(0,0); //home the lcd screen |
saltire78 | 6:3a4fb916a217 | 35 | lcd.printf("Deg. F Selected\nPlease Wait..."); //selection text |
saltire78 | 6:3a4fb916a217 | 36 | tempselect=2; //ammend selection constant - breaks while loop |
saltire78 | 6:3a4fb916a217 | 37 | wait(1); //pause for visibility and debounce |
saltire78 | 6:3a4fb916a217 | 38 | } |
saltire78 | 6:3a4fb916a217 | 39 | else if(JoyLeft==0 and JoyRight==0 and JoyButton==1){ |
saltire78 | 6:3a4fb916a217 | 40 | lcd.cls(); //clear LCD screen |
saltire78 | 6:3a4fb916a217 | 41 | lcd.locate(0,0); //home the lcd screen |
saltire78 | 6:3a4fb916a217 | 42 | lcd.printf("Deg. C and Deg. F Selected\nPlease Wait..."); //selection text |
saltire78 | 6:3a4fb916a217 | 43 | tempselect=3; //ammend selection constant - breaks while loop |
saltire78 | 6:3a4fb916a217 | 44 | wait(1); //pause for visibility and debounce |
saltire78 | 6:3a4fb916a217 | 45 | } |
saltire78 | 6:3a4fb916a217 | 46 | } |
saltire78 | 6:3a4fb916a217 | 47 | |
chris | 4:6df97cb10041 | 48 | //Try to open the LM75B |
chris | 4:6df97cb10041 | 49 | if (sensor.open()) { |
chris | 4:6df97cb10041 | 50 | printf("Device detected!\n"); |
chris | 4:6df97cb10041 | 51 | |
chris | 4:6df97cb10041 | 52 | while (1) { |
saltire78 | 6:3a4fb916a217 | 53 | /* setting temperature range as between 0C and 30C, or 32F to 86F, using y=mx+c. |
saltire78 | 6:3a4fb916a217 | 54 | for Celsius tempC=30sensor+0 |
saltire78 | 6:3a4fb916a217 | 55 | for Fahrenheit tempF=54sensor+32 */ |
saltire78 | 6:3a4fb916a217 | 56 | float tempC=30*(float)sensor; //creating Celsius selection value |
saltire78 | 6:3a4fb916a217 | 57 | float tempF=54*(float)sensor+32; //creating Fahrenheit selection value |
saltire78 | 6:3a4fb916a217 | 58 | |
saltire78 | 6:3a4fb916a217 | 59 | if(tempselect==1){ //if deg. C selected |
saltire78 | 6:3a4fb916a217 | 60 | lcd.cls(); //clear LCD screen |
saltire78 | 6:3a4fb916a217 | 61 | lcd.locate(0,0); //home LCD screen |
saltire78 | 6:3a4fb916a217 | 62 | lcd.printf("Temp = %.1f Deg. C\n", tempC); //print temp info |
saltire78 | 6:3a4fb916a217 | 63 | wait(1.0); //pause for visibility |
saltire78 | 6:3a4fb916a217 | 64 | } |
saltire78 | 6:3a4fb916a217 | 65 | else if(tempselect==2){ //if deg. F selected |
saltire78 | 6:3a4fb916a217 | 66 | lcd.cls(); //clear LCD screen |
saltire78 | 6:3a4fb916a217 | 67 | lcd.locate(0,0); //home LCD screen |
saltire78 | 6:3a4fb916a217 | 68 | lcd.printf("Temp = %.1f Deg. F\n", tempF); //print temp info |
saltire78 | 6:3a4fb916a217 | 69 | wait(1.0); //pause for visibility |
saltire78 | 6:3a4fb916a217 | 70 | } |
saltire78 | 6:3a4fb916a217 | 71 | else if(tempselect==3){ //if both temps selected |
saltire78 | 6:3a4fb916a217 | 72 | lcd.cls(); //clear LCD screen |
saltire78 | 6:3a4fb916a217 | 73 | lcd.locate(0,0); //home LCD screen |
saltire78 | 6:3a4fb916a217 | 74 | lcd.printf("Temp = %.1f Deg. C\nTemp = %.1f Deg. F", tempC, tempF); //print both temp infos |
saltire78 | 6:3a4fb916a217 | 75 | wait(1.0); //pause for visibility |
saltire78 | 6:3a4fb916a217 | 76 | } |
saltire78 | 6:3a4fb916a217 | 77 | |
saltire78 | 6:3a4fb916a217 | 78 | if(tempC>=27.0){ //High temp alarm set for 27 degrees C or over |
saltire78 | 6:3a4fb916a217 | 79 | lcd.cls(); //clear LCD screen |
saltire78 | 6:3a4fb916a217 | 80 | lcd.locate(0,0); //home LCD screen |
saltire78 | 6:3a4fb916a217 | 81 | lcd.printf("Warning - Temperature High Alarm!"); //display alarm text |
saltire78 | 6:3a4fb916a217 | 82 | AlarmLED=1; //toggle on alarm LED |
saltire78 | 6:3a4fb916a217 | 83 | wait(1); //pause while LED lit |
saltire78 | 6:3a4fb916a217 | 84 | AlarmLED=0; //clear the alarm LED |
saltire78 | 6:3a4fb916a217 | 85 | } |
saltire78 | 6:3a4fb916a217 | 86 | |
saltire78 | 6:3a4fb916a217 | 87 | } // end of while loop |
saltire78 | 6:3a4fb916a217 | 88 | |
saltire78 | 6:3a4fb916a217 | 89 | } // the end of if statment |
saltire78 | 6:3a4fb916a217 | 90 | |
saltire78 | 6:3a4fb916a217 | 91 | else { |
saltire78 | 6:3a4fb916a217 | 92 | error("Device not detected!\n"); |
saltire78 | 6:3a4fb916a217 | 93 | } |
chris | 4:6df97cb10041 | 94 | |
saltire78 | 6:3a4fb916a217 | 95 | } // end of main |