Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: mbed
main.cpp
00001 /** 00002 IOT Coursework Project 00003 Lewis Cameron 00004 00005 Water Temperature Legionella Detector With Custom Setpoint 00006 00007 Acknowledgements to Dr. Edmond Nurellari, University of Lincoln, for example codes and libraries 00008 Acknowledgements to Craig A. Evans, University of Leeds, for libraries 00009 00010 Date 23/08/2022 00011 00012 */ 00013 // importing the libraries to be used within the code 00014 #include "mbed.h" 00015 #include "TMP102.h" 00016 #include "N5110.h" 00017 #include "Joystick.h" 00018 #include "Bitmap.h" 00019 00020 //Defining PCB Buttons as Interrupts 00021 InterruptIn button_back(PTB19); 00022 InterruptIn button_start(PTC5); 00023 InterruptIn button_a(PTB9); 00024 InterruptIn button_b(PTD0); 00025 InterruptIn button_x(PTC17); 00026 InterruptIn button_y(PTC12); 00027 InterruptIn button_left(PTB18); 00028 InterruptIn button_right(PTB3); 00029 InterruptIn sw2(SW2); 00030 InterruptIn sw3(SW3); 00031 00032 //Defining the PCB Led's as PWM Outputs 00033 PwmOut led_red1(PTA1); 00034 PwmOut led_red2(PTA2); 00035 PwmOut led_red3(PTC2); 00036 PwmOut led_green1(PTC3); 00037 PwmOut led_green2(PTC4); 00038 PwmOut led_green3(PTD3); 00039 DigitalOut boardled_red(LED_RED); 00040 DigitalOut boardled_green(LED_GREEN); 00041 DigitalOut boardled_blue(LED_BLUE); 00042 00043 // Defining the Joystick, LCD & TMP102 on their respective pins 00044 Joystick Joystick(PTB10,PTB11,PTB16); 00045 N5110 lcd(PTC9,PTC0,PTC7,PTD2,PTD1,PTC11); 00046 TMP102 tmp(I2C_SDA,I2C_SCL); 00047 00048 //Defined the piezo as a PWM Output although it has not been used in the code 00049 PwmOut piezo(PTC10); 00050 00051 //Creating the serial connection to the PC to enable the use of coolterm 00052 Serial pc(USBTX,USBRX); 00053 00054 // Setting up the variables that will be used throughout the code in functions 00055 volatile int button_a_flag = 0; //PCB A Button 00056 volatile int button_b_flag = 0; //PCB B Button 00057 volatile int button_x_flag = 0; //PCB X Button 00058 volatile int button_y_flag = 0; //PCB Y Button 00059 volatile int button_left_flag = 0; //PCB Left Button 00060 volatile int button_right_flag = 0; //PCB Right Button 00061 volatile int button_back_flag = 0; //PCB Back Button 00062 volatile int button_start_flag = 0; //PCB Start Button 00063 volatile int CwsHiSP = 20; //CWS Water High Limit Temperature Setpoint 00064 volatile int HwsKaSP = 70; //HWS Water Temp which kills all legionella 00065 volatile int HwsLoLimSP = 50; //HWS water low limit temperature setpoint 00066 volatile int CustomLoSP = 10; //Custom low limit temperature setpoint which can be changed using the joystick 00067 volatile int CustomHiSP = 20; //Custom high limit temperature setpoint which can be changed using left & right 00068 00069 // voids 00070 // functions that will be used in the code 00071 void init_serial(); // sets up serial baud rate 00072 void init_K64F(); // sets up K64F 00073 void init_pcb(); // sets up the pcb 00074 00075 // button voids 00076 void button_a_isr(); 00077 void button_b_isr(); 00078 void button_x_isr(); 00079 void button_y_isr(); 00080 void button_left_isr(); 00081 void button_right_isr(); 00082 void button_back_isr(); 00083 void button_start_isr(); 00084 00085 // void functions that will be called upon throughout the code 00086 void startup(); //startup function 00087 void codeloop(); //main loop function 00088 void get_temp(); //temperature display function 00089 void CustHi_SP(); //function for changine the custom high limit SP 00090 void CustLo_SP(); //function for changine the custom low limit SP 00091 void ShowSP(); //function to show the custom SP's on screen when they after they are changed 00092 00093 //State structure 00094 struct State { 00095 //Output Setpoint 00096 int output; 00097 //Time delay while in state 00098 float time; 00099 //Next states array and length 00100 int nextState[9]; 00101 }; 00102 00103 00104 //Main Function 00105 00106 int main() { 00107 00108 // interrupts setup 00109 button_a.rise(&button_a_isr); 00110 button_b.rise(&button_b_isr); 00111 button_x.rise(&button_x_isr); 00112 button_y.rise(&button_y_isr); 00113 button_left.rise(&button_left_isr); 00114 button_right.rise(&button_right_isr); 00115 button_back.rise(&button_back_isr); 00116 button_start.rise(&button_start_isr); 00117 00118 00119 00120 init_serial(); 00121 init_K64F(); 00122 init_pcb(); 00123 tmp.init(); 00124 lcd.init(); 00125 Joystick.init(); 00126 //pc.printf("hardware initialised"); 00127 00128 lcd.setContrast(1); 00129 lcd.setBrightness(0.5); 00130 00131 startup(); 00132 00133 codeloop(); 00134 } 00135 00136 // initialise serial connection 00137 void init_serial() 00138 { 00139 pc.baud(9600); 00140 } 00141 00142 void init_K64F() 00143 { 00144 boardled_red = 1; 00145 boardled_green = 1; 00146 boardled_blue = 1; 00147 00148 sw2.mode(PullNone); 00149 sw3.mode(PullNone); 00150 } 00151 00152 void init_pcb() 00153 { 00154 00155 led_red1 = 1; 00156 led_red2 = 1; 00157 led_red3 = 1; 00158 led_green1 = 1; 00159 led_green2 = 1; 00160 led_green3 = 1; 00161 // disables PCB LED's active low LED's so off when = 1 00162 00163 // sets up the PCB Buttons 00164 button_a.mode(PullDown); 00165 button_b.mode(PullDown); 00166 button_x.mode(PullDown); 00167 button_y.mode(PullDown); 00168 button_left.mode(PullDown); 00169 button_right.mode(PullDown); 00170 button_start.mode(PullDown); 00171 button_back.mode(PullDown); 00172 } 00173 00174 void startup() // startup page 00175 { 00176 lcd.printString("Lewis Cameron",0,0); 00177 lcd.printString("18689002",0,1); 00178 lcd.printString("Legionella",0,2); 00179 lcd.printString("Sensor",0,3); 00180 lcd.printString("w/ Custom SP",0,4); 00181 //pc.printf("startup"); 00182 lcd.refresh(); 00183 wait(5); 00184 } 00185 00186 void codeloop() // main loop 00187 { 00188 int page; 00189 00190 while(1){ 00191 lcd.clear(); 00192 00193 switch(page){ 00194 00195 case 1: // CWS Page 00196 { 00197 00198 init_pcb(); 00199 00200 float temp = tmp.get_temperature(); 00201 00202 00203 lcd.clear(); 00204 lcd.printString("CWS",30,0); 00205 lcd.printString("Temp =",0,1); 00206 get_temp(); // function to show the temperature on screen 00207 lcd.printString("Back = Home",0,5); 00208 //pc.printf("CWS"); 00209 00210 if (temp >= CwsHiSP){ // High temperature Alarm Warning appears on screen 00211 lcd.printString("CWS Temp High",0,2); 00212 lcd.printString("Legionella",0,3); 00213 lcd.printString("Warning",0,4); 00214 led_red1 = 0; 00215 //pc.printf("CWS High Temp"); 00216 } 00217 00218 else if (temp < CwsHiSP){ // Temperature at safe levels so message appears on screen 00219 lcd.printString("Temp OK",0,2); 00220 lcd.printString("No Danger",0,3); 00221 lcd.printString("Of Legionella",0,4); 00222 led_red1 = 1; 00223 //pc.printf("CWS Temp OK"); 00224 } 00225 00226 if (button_back_flag) { 00227 button_back_flag = 0; 00228 page = 0; 00229 } 00230 lcd.refresh(); 00231 } 00232 break; 00233 00234 case 2: 00235 { 00236 00237 init_pcb(); 00238 float temp = tmp.get_temperature(); 00239 00240 lcd.clear(); 00241 lcd.printString("HWS",30,0); 00242 lcd.printString("Temp =",0,1); 00243 get_temp(); 00244 lcd.printString("Back = Home",0,5); 00245 00246 if (HwsLoLimSP < temp & temp < HwsKaSP){ // Temperature between 50-70 degrees which will start killing legionella 00247 lcd.printString("Temp Raised",0,2); 00248 lcd.printString("Killing",0,3); 00249 lcd.printString("Legionella",0,4); 00250 led_green1 = 0; 00251 //pc.printf("Temp killing legionella"); 00252 } 00253 00254 else if (temp >= HwsKaSP){ // Temperature above 70 degrees killing all legionella 00255 lcd.printString("Temp Raised",0,2); 00256 lcd.printString("No Danger",0,3); 00257 lcd.printString("Of Legionella",0,4); 00258 led_green1 = 0; 00259 led_green2 = 0; 00260 //pc.printf("Temp killed all legionella"); 00261 } 00262 00263 else if (temp < HwsLoLimSP){ // Temperature below 50 degrees allowing for legionella to live 00264 lcd.printString("Temp Low",0,2); 00265 lcd.printString("Legionella",0,3); 00266 lcd.printString("Warning",0,4); 00267 led_red1 = 0; 00268 //pc.printf("Temp too low legionella warning"); 00269 } 00270 00271 if (button_back_flag) { 00272 button_back_flag = 0; 00273 page = 0; 00274 } 00275 00276 lcd.refresh(); 00277 00278 } 00279 00280 00281 00282 break; 00283 00284 case 3: 00285 { 00286 00287 init_pcb(); 00288 float temp = tmp.get_temperature(); 00289 00290 lcd.clear(); 00291 00292 lcd.printString("Custom SP",15,0); 00293 lcd.printString("Temp =",0,1); 00294 get_temp(); 00295 CustHi_SP(); // function to change high limit setpoint 00296 CustLo_SP(); // function to change low limit setpoint 00297 ShowSP(); // function to show both setpoints and change them on screen 00298 lcd.printString("Back = Home",0,5); 00299 00300 if (CustomLoSP < temp & temp < CustomHiSP){ 00301 lcd.printString("Temp In Range",0,4); 00302 led_green1 = 0; 00303 //pc.printf("Temp in custom range"); 00304 } 00305 00306 else if (temp > CustomHiSP){ 00307 lcd.printString("High Temp",0,4); 00308 led_red1 = 0; 00309 //pc.printf("Custom temp high limit"); 00310 } 00311 00312 else if (temp < CustomLoSP){ 00313 lcd.printString("Low Temp",0,4); 00314 led_red2 = 0; 00315 //pc.printf("Custom temp low limit"); 00316 } 00317 00318 if (button_back_flag) { 00319 button_back_flag = 0; 00320 page = 0; 00321 } 00322 lcd.refresh(); 00323 00324 } 00325 00326 break; 00327 00328 case 4: 00329 { 00330 init_pcb(); 00331 lcd.clear(); 00332 lcd.printString("Info",26,0); 00333 lcd.printString("Legionella",0,1); 00334 lcd.printString("Detector",0,2); 00335 lcd.printString("Lewis Cameron",0,3); 00336 lcd.printString("18689002",0,4); 00337 lcd.printString("Back = Home",0,5); 00338 lcd.refresh(); 00339 //pc.printf("Info page"); 00340 00341 if (button_back_flag) { 00342 button_back_flag = 0; 00343 page = 0; 00344 } 00345 00346 } 00347 00348 00349 00350 break; 00351 { 00352 default: 00353 init_pcb(); 00354 lcd.clear(); 00355 00356 lcd.printString("Main Menu",15,0); 00357 lcd.printString("A = Cold Water",0,1); 00358 lcd.printString("B = Hot Water",0,2); 00359 lcd.printString("X = Custom SP",0,3); 00360 lcd.printString("Y = Info",0,4); 00361 lcd.refresh(); 00362 00363 if (button_a_flag) { 00364 button_a_flag = 0; 00365 page = 1; 00366 //pc.printf("A Pressed"); 00367 } 00368 00369 if (button_b_flag) { 00370 button_b_flag = 0; 00371 page = 2; 00372 //pc.printf("B Pressed"); 00373 } 00374 00375 if (button_x_flag) { 00376 button_x_flag = 0; 00377 page = 3; 00378 //pc.printf("X Pressed"); 00379 } 00380 00381 if (button_y_flag) { 00382 button_y_flag = 0; 00383 page = 4; 00384 //pc.printf("Y Pressed"); 00385 } 00386 00387 } 00388 } 00389 00390 } 00391 } 00392 00393 00394 00395 void button_a_isr() 00396 { 00397 button_a_flag = 1; 00398 } 00399 00400 void button_b_isr() 00401 { 00402 button_b_flag = 1; 00403 } 00404 00405 void button_x_isr() 00406 { 00407 button_x_flag = 1; 00408 } 00409 00410 void button_y_isr() 00411 { 00412 button_y_flag = 1; 00413 } 00414 00415 void button_left_isr() 00416 { 00417 button_left_flag = 1; 00418 } 00419 00420 void button_right_isr() 00421 { 00422 button_right_flag = 1; 00423 } 00424 00425 void button_back_isr() 00426 { 00427 button_back_flag = 1; 00428 } 00429 00430 00431 void button_start_isr() 00432 { 00433 button_start_flag = 1; 00434 } 00435 00436 void get_temp() 00437 { 00438 char buffer[14]; 00439 float temp = tmp.get_temperature(); 00440 int length = sprintf(buffer,"%.2f C",temp); 00441 //pc.printf("temp = %f K\n",temp); 00442 if (length <=14) 00443 lcd.printString(buffer,38,1); 00444 00445 } 00446 00447 void CustHi_SP() 00448 { 00449 Direction d = Joystick.get_direction(); //gets the direction the joystick is pushed 00450 00451 00452 if (d == W) { // decreases the setpoint when it is pushed left 00453 //pc.printf("Joystick Left"); 00454 CustomHiSP = CustomHiSP - 1; 00455 wait(0.5); 00456 } 00457 00458 if (d == E) { //increases the setpoint when it is pushed right 00459 //pc.printf("Joystick Right"); 00460 CustomHiSP = CustomHiSP + 1; 00461 wait(0.5); 00462 00463 } 00464 } 00465 00466 void CustLo_SP() //function to change low limit SP 00467 { 00468 if (button_left_flag) { //decreases the setpoint when the left bumper is pressed 00469 button_left_flag = 0; 00470 //pc.printf("Left pressed"); 00471 CustomLoSP = CustomLoSP - 1; 00472 wait(0.5); 00473 } 00474 00475 if (button_right_flag) { // increases the setpoint when the right bumper is pressed 00476 button_right_flag = 0; 00477 //pc.printf("Right pressed"); 00478 CustomLoSP = CustomLoSP + 1; 00479 wait(0.5); 00480 } 00481 00482 } 00483 00484 void ShowSP() 00485 { 00486 char buffer[14]; 00487 int length = sprintf(buffer,"LoLimSP=%.2iC",CustomLoSP); 00488 00489 if (length <= 14); { 00490 lcd.printString(buffer,0,3); // prints the low SP value on screen 00491 } 00492 00493 length = sprintf(buffer,"HiLimSP=%.2iC",CustomHiSP); 00494 00495 if (length <= 14); { 00496 lcd.printString(buffer,0,2); // prints the high SP value on screen 00497 } 00498 } 00499 00500 00501 void TMP102::error() // function to show that there is an error with the temp sensor on screen and switch on a red LED 00502 { 00503 00504 startup(); 00505 lcd.clear(); 00506 lcd.printString("TMP102",0,0); 00507 lcd.printString("Sensor Error",0,1); 00508 00509 while(1) { 00510 //printf("Temp sensor error \n"); 00511 led_red2 = 0; 00512 } 00513 } 00514 00515 00516 00517 00518 00519
Generated on Wed Aug 24 2022 19:44:05 by
1.7.2