legionella detector
Dependencies: mbed
Diff: main.cpp
- Revision:
- 4:b82f09e597ec
- Parent:
- 3:eaa6fbb8fd87
- Child:
- 5:2d1d347847a6
diff -r eaa6fbb8fd87 -r b82f09e597ec main.cpp --- a/main.cpp Tue Aug 23 12:49:09 2022 +0000 +++ b/main.cpp Tue Aug 23 16:54:43 2022 +0000 @@ -1,12 +1,23 @@ +/** +IOT Coursework Project +Lewis Cameron -// importing the libraries +Water Temperature Legionella Detector With Custom Setpoint + +Acknowledgements to Dr. Edmond Nurellari, University of Lincoln, for example codes and libraries +Acknowledgements to Craig A. Evans, University of Leeds, for libraries + +Date 23/08/2022 + +*/ +// importing the libraries to be used within the code #include "mbed.h" #include "TMP102.h" #include "N5110.h" #include "Joystick.h" #include "Bitmap.h" -//Buttons +//Defining PCB Buttons as Interrupts InterruptIn button_back(PTB19); InterruptIn button_start(PTC5); InterruptIn button_a(PTB9); @@ -15,52 +26,44 @@ InterruptIn button_y(PTC12); InterruptIn button_left(PTB18); InterruptIn button_right(PTB3); -InterruptIn sw2(SW2); -InterruptIn sw3(SW3); -//LEDs +//Defining the PCB Led's as PWM Outputs PwmOut led_red1(PTA1); PwmOut led_red2(PTA2); PwmOut led_red3(PTC2); PwmOut led_green1(PTC3); PwmOut led_green2(PTC4); PwmOut led_green3(PTD3); -DigitalOut boardled_red(LED_RED); -DigitalOut boardled_green(LED_GREEN); -DigitalOut boardled_blue(LED_BLUE); -// Joystick, LCD & TMP102 +// Defining the Joystick, LCD & TMP102 on their respective pins Joystick Joystick(PTB10,PTB11,PTB16); N5110 lcd(PTC9,PTC0,PTC7,PTD2,PTD1,PTC11); TMP102 tmp(I2C_SDA,I2C_SCL); -//Piezo +//Defined the piezo as a PWM Output although it has not been used in the code PwmOut piezo(PTC10); -//serial +//Creating the serial connection to the PC to enable the use of coolterm Serial pc(USBTX,USBRX); -// variables -volatile int button_a_flag = 0; -volatile int button_b_flag = 0; -volatile int button_x_flag = 0; -volatile int button_y_flag = 0; -volatile int button_left_flag = 0; -volatile int button_right_flag = 0; -volatile int button_back_flag = 0; -volatile int button_start_flag = 0; -volatile int CwsHiSP = 20; -volatile int HwsKaSP = 70; -volatile int HwsLoLimSP = 50; -volatile int CustomLoSP = 10; -volatile int CustomLoSPChange = 0; -volatile int CustomHiSP = 20; -volatile int CustomHiSPChange = 0; +// Setting up the variables that will be used throughout the code in functions +volatile int button_a_flag = 0; //PCB A Button +volatile int button_b_flag = 0; //PCB B Button +volatile int button_x_flag = 0; //PCB X Button +volatile int button_y_flag = 0; //PCB Y Button +volatile int button_left_flag = 0; //PCB Left Button +volatile int button_right_flag = 0; //PCB Right Button +volatile int button_back_flag = 0; //PCB Back Button +volatile int button_start_flag = 0; //PCB Start Button +volatile int CwsHiSP = 20; //CWS Water High Limit Temperature Setpoint +volatile int HwsKaSP = 70; //HWS Water Temp which kills all legionella +volatile int HwsLoLimSP = 50; //HWS water low limit temperature setpoint +volatile int CustomLoSP = 10; //Custom low limit temperature setpoint which can be changed using the joystick +volatile int CustomHiSP = 20; //Custom high limit temperature setpoint which can be changed using left & right // voids // functions that will be used in the code -//void error(); -void init_serial(); // sets up serial +void init_serial(); // sets up serial baud rate void init_K64F(); // sets up K64F void init_pcb(); // sets up the pcb @@ -74,14 +77,14 @@ void button_back_isr(); void button_start_isr(); -// page voids -void startup(); -void cwshigh(); -void codeloop(); -void get_temp(); -void CustHi_SP(); -void CustLo_SP(); -void ShowSP(); +// void functions that will be called upon throughout the code +void startup(); //startup function +void cwshigh(); //CWS high temperature function +void codeloop(); //main loop function +void get_temp(); //temperature display function +void CustHi_SP(); //function for changine the custom high limit SP +void CustLo_SP(); //function for changine the custom low limit SP +void ShowSP(); //function to show the custom SP's on screen when they after they are changed //State structure struct State { @@ -94,7 +97,7 @@ }; - +//Main Function int main() { @@ -116,6 +119,7 @@ tmp.init(); lcd.init(); Joystick.init(); + //pc.printf("hardware initialised"); lcd.setContrast(1); lcd.setBrightness(0.5); @@ -153,7 +157,7 @@ led_green1 = 1; led_green2 = 1; led_green3 = 1; - // active low LED's so off when = 1 + // disables PCB LED's active low LED's so off when = 1 // sets up the PCB Buttons button_a.mode(PullDown); @@ -166,19 +170,19 @@ button_back.mode(PullDown); } -void startup() +void startup() // startup page { lcd.printString("Lewis Cameron",0,0); lcd.printString("18689002",0,1); lcd.printString("Legionella",0,2); lcd.printString("Sensor",0,3); lcd.printString("w/ Custom SP",0,4); - pc.printf("startup"); + //pc.printf("startup"); lcd.refresh(); wait(5); } -void codeloop() +void codeloop() // main loop { int page; @@ -187,7 +191,7 @@ switch(page){ - case 1: + case 1: // CWS Page { init_pcb(); @@ -198,24 +202,24 @@ lcd.clear(); lcd.printString("CWS",30,0); lcd.printString("Temp =",0,1); - get_temp(); + get_temp(); // function to show the temperature on screen lcd.printString("Back = Home",0,5); - pc.printf("CWS"); + //pc.printf("CWS"); - if (temp >= CwsHiSP){ + if (temp >= CwsHiSP){ // High temperature Alarm Warning appears on screen lcd.printString("CWS Temp High",0,2); lcd.printString("Legionella",0,3); lcd.printString("Warning",0,4); led_red1 = 0; - pc.printf("CWS High Temp"); + //pc.printf("CWS High Temp"); } - else if (temp < CwsHiSP){ + else if (temp < CwsHiSP){ // Temperature at safe levels so message appears on screen lcd.printString("Temp OK",0,2); lcd.printString("No Danger",0,3); lcd.printString("Of Legionella",0,4); led_red1 = 1; - pc.printf("CWS Temp OK"); + //pc.printf("CWS Temp OK"); } if (button_back_flag) { @@ -238,26 +242,29 @@ get_temp(); lcd.printString("Back = Home",0,5); - if (HwsLoLimSP < temp & temp < HwsKaSP){ + if (HwsLoLimSP < temp & temp < HwsKaSP){ // Temperature between 50-70 degrees which will start killing legionella lcd.printString("Temp Raised",0,2); lcd.printString("Killing",0,3); lcd.printString("Legionella",0,4); led_green1 = 0; + //pc.printf("Temp killing legionella"); } - else if (temp >= HwsKaSP){ + else if (temp >= HwsKaSP){ // Temperature above 70 degrees killing all legionella lcd.printString("Temp Raised",0,2); lcd.printString("No Danger",0,3); lcd.printString("Of Legionella",0,4); led_green1 = 0; led_green2 = 0; + //pc.printf("Temp killed all legionella"); } - else if (temp < HwsLoLimSP){ + else if (temp < HwsLoLimSP){ // Temperature below 50 degrees allowing for legionella to live lcd.printString("Temp Low",0,2); lcd.printString("Legionella",0,3); lcd.printString("Warning",0,4); led_red1 = 0; + //pc.printf("Temp too low legionella warning"); } if (button_back_flag) { @@ -284,24 +291,27 @@ lcd.printString("Custom SP",15,0); lcd.printString("Temp =",0,1); get_temp(); - CustHi_SP(); - CustLo_SP(); - ShowSP(); + CustHi_SP(); // function to change high limit setpoint + CustLo_SP(); // function to change low limit setpoint + ShowSP(); // function to show both setpoints and change them on screen lcd.printString("Back = Home",0,5); if (CustomLoSP < temp & temp < CustomHiSP){ lcd.printString("Temp In Range",0,4); led_green1 = 0; + //pc.printf("Temp in custom range"); } else if (temp > CustomHiSP){ lcd.printString("High Temp",0,4); led_red1 = 0; + //pc.printf("Custom temp high limit"); } else if (temp < CustomLoSP){ lcd.printString("Low Temp",0,4); led_red2 = 0; + //pc.printf("Custom temp low limit"); } if (button_back_flag) { @@ -325,6 +335,7 @@ lcd.printString("18689002",0,4); lcd.printString("Back = Home",0,5); lcd.refresh(); + //pc.printf("Info page"); if (button_back_flag) { button_back_flag = 0; @@ -351,25 +362,25 @@ if (button_a_flag) { button_a_flag = 0; page = 1; - pc.printf("A Pressed"); + //pc.printf("A Pressed"); } if (button_b_flag) { button_b_flag = 0; page = 2; - pc.printf("B Pressed"); + //pc.printf("B Pressed"); } if (button_x_flag) { button_x_flag = 0; page = 3; - pc.printf("X Pressed"); + //pc.printf("X Pressed"); } if (button_y_flag) { button_y_flag = 0; page = 4; - pc.printf("Y Pressed"); + //pc.printf("Y Pressed"); } } @@ -440,35 +451,35 @@ void CustHi_SP() { - Direction d = Joystick.get_direction(); + Direction d = Joystick.get_direction(); //gets the direction the joystick is pushed - if (d == W) { - pc.printf("Joystick Left"); + if (d == W) { // decreases the setpoint when it is pushed left + //pc.printf("Joystick Left"); CustomHiSP = CustomHiSP - 1; wait(0.5); } - if (d == E) { - pc.printf("Joystick Right"); + if (d == E) { //increases the setpoint when it is pushed right + //pc.printf("Joystick Right"); CustomHiSP = CustomHiSP + 1; wait(0.5); } } -void CustLo_SP() +void CustLo_SP() //function to change low limit SP { - if (button_left_flag) { + if (button_left_flag) { //decreases the setpoint when the left bumper is pressed button_left_flag = 0; - pc.printf("Left pressed"); + //pc.printf("Left pressed"); CustomLoSP = CustomLoSP - 1; wait(0.5); } - if (button_right_flag) { + if (button_right_flag) { // increases the setpoint when the right bumper is pressed button_right_flag = 0; - pc.printf("Right pressed"); + //pc.printf("Right pressed"); CustomLoSP = CustomLoSP + 1; wait(0.5); } @@ -481,29 +492,27 @@ int length = sprintf(buffer,"LoLimSP=%.2iC",CustomLoSP); if (length <= 14); { - lcd.printString(buffer,0,3); + lcd.printString(buffer,0,3); // prints the low SP value on screen } length = sprintf(buffer,"HiLimSP=%.2iC",CustomHiSP); if (length <= 14); { - lcd.printString(buffer,0,2); + lcd.printString(buffer,0,2); // prints the high SP value on screen } } -void TMP102::error() +void TMP102::error() // function to show that there is an error with the temp sensor on screen and switch on a red LED { - //Run start screens + startup(); lcd.clear(); lcd.printString("TMP102",0,0); lcd.printString("Sensor Error",0,1); - - // If there is an error than flash led while(1) { - printf("Temp sensor error \n"); + //printf("Temp sensor error \n"); led_red2 = 0; } }