legionella detector

Dependencies:   mbed

Revision:
0:089b4c4f2b6f
Child:
1:87661a0c8ba3
diff -r 000000000000 -r 089b4c4f2b6f main.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Tue Aug 23 08:47:11 2022 +0000
@@ -0,0 +1,442 @@
+
+// importing the libraries
+#include "mbed.h"
+#include "TMP102.h"
+#include "N5110.h"
+#include "Joystick.h"
+#include "Bitmap.h"
+
+//Buttons
+InterruptIn button_back(PTB19);
+InterruptIn button_start(PTC5);
+InterruptIn button_a(PTB9);
+InterruptIn button_b(PTD0);
+InterruptIn button_x(PTC17);
+InterruptIn button_y(PTC12);
+InterruptIn button_left(PTB18);
+InterruptIn button_right(PTB3);
+InterruptIn sw2(SW2);
+InterruptIn sw3(SW3);
+
+//LEDs
+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
+Joystick Joystick(PTB10,PTB11,PTB16);
+N5110 lcd(PTC9,PTC0,PTC7,PTD2,PTD1,PTC11);
+TMP102 tmp(I2C_SDA,I2C_SCL);
+
+//Piezo
+PwmOut piezo(PTC10);
+
+//serial
+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;
+
+// voids
+// functions that will be used in the code
+//void error();
+void init_serial();  // sets up serial
+void init_K64F();    // sets up K64F
+void init_pcb();     // sets up the pcb
+
+// button voids
+void button_a_interrupt();
+void button_b_interrupt();
+void button_x_interrupt();
+void button_y_interrupt();
+void button_left_interrupt();
+void button_right_interrupt();
+void button_back_interrupt();
+void button_start_interrupt();
+
+// page voids
+void startup();
+void cwshigh();
+void codeloop();
+void get_temp();
+void CustHi_SP();
+void CustLo_SP();
+
+
+
+
+int main() {
+    
+    // interrupts setup
+    button_a.rise(&button_a_interrupt);
+    button_b.rise(&button_b_interrupt);
+    button_x.rise(&button_x_interrupt);
+    button_y.rise(&button_y_interrupt);
+    button_left.rise(&button_left_interrupt);
+    button_right.rise(&button_right_interrupt);
+    button_back.rise(&button_back_interrupt);
+    button_start.rise(&button_start_interrupt);
+    
+    
+    
+    init_serial();
+    init_K64F();
+    init_pcb();
+    tmp.init();
+    lcd.init();
+    Joystick.init();
+    
+    lcd.setContrast(0.5);
+    lcd.setBrightness(0.5);
+    
+    startup();
+    
+    codeloop();
+    
+    
+    
+}
+
+// initialise serial connection
+void init_serial()
+{
+    pc.baud(9600);
+}
+
+void init_K64F()
+{
+    boardled_red = 1;
+    boardled_green = 1;
+    boardled_blue = 1;
+    
+    sw2.mode(PullNone);
+    sw3.mode(PullNone);
+}
+
+void init_pcb()
+{
+    
+    led_red1 = 1;
+    led_red2 = 1;
+    led_red3 = 1;
+    led_green1 = 1;
+    led_green2 = 1;
+    led_green3 = 1;
+    // active low LED's so off when = 1
+    
+    // sets up the PCB Buttons
+    button_a.mode(PullDown);
+    button_b.mode(PullDown);
+    button_x.mode(PullDown);
+    button_y.mode(PullDown);
+    button_left.mode(PullDown);
+    button_right.mode(PullDown);
+    button_start.mode(PullDown);
+    button_back.mode(PullDown);
+}
+
+void startup()
+{
+    lcd.printString("Lewis Cameron",0,0);
+    lcd.printString("18689002",0,1);
+    pc.printf("startup");
+    lcd.refresh();
+    wait(5);
+}
+
+void codeloop()
+{
+    int page;
+    
+    while(1){
+        lcd.clear();
+        
+        switch(page){
+        
+        case 1:
+        {
+        
+        init_pcb();
+        
+        float temp = tmp.get_temperature();
+        
+        
+        lcd.clear();
+        lcd.printString("CWS",20,0);
+        lcd.printString("Temp is",0,2);
+        get_temp();
+        lcd.printString("Press Back To Go Home",0,5);
+        
+        if (temp >= CwsHiSP){
+            lcd.printString("Water Temp Too High",0,3);
+            lcd.printString("Legionella Warning",0,4);
+            led_red1 = 0;
+        }
+        
+        else if (temp < CwsHiSP){
+            lcd.printString("Water Temp OK",0,3);
+            lcd.printString("No Danger Of Legionella",0,4);
+            led_red1 = 1;
+        }
+        
+        if (button_back_flag) {
+            button_back_flag = 0;
+            page = 0;
+        }
+    
+    }
+        break;
+    
+        case 2:
+        {
+    
+        init_pcb();
+        float temp = tmp.get_temperature();
+    
+        lcd.clear();
+        lcd.printString("HWS",20,0);
+        lcd.printString("Temp is",0,2);
+        get_temp();
+        lcd.printString("Press Back To Go Home",0,5);
+    
+    if (HwsLoLimSP < temp < HwsKaSP){
+        lcd.printString("Temperature Raised",0,3);
+        lcd.printString("Killing Legionella",0,4);
+        led_green1 = 0;
+        }
+    
+    else if (temp >= HwsKaSP){
+        lcd.printString("Temperature Raised",0,3);
+        lcd.printString("All Legionella Killed",0,4);
+        led_green1 = 0;
+        led_green2 = 0;
+        }
+    
+    else if (temp < HwsLoLimSP){
+        lcd.printString("Temperature Low",0,3);
+        lcd.printString("Legionella Warning",0,4);
+        led_red1 = 0;
+    }
+        
+    if (button_back_flag) {
+        button_back_flag = 0;
+        page = 0;
+    }
+    
+}
+
+    
+    
+    break;
+    
+    case 3:
+    {
+    
+    init_pcb();
+    lcd.clear();
+        
+    lcd.printString("Custom SP",20,0);
+    lcd.printString("Temp is",0,2);
+    get_temp();
+    CustHi_SP();
+    CustLo_SP();
+    lcd.printString("Press Back To Go Home",0,5);
+    
+    if (button_back_flag) {
+        button_back_flag = 0;
+        page = 0;
+    }
+    
+    }
+    
+    break;
+    
+    case 4:
+    {
+        init_pcb();
+        lcd.clear();
+        lcd.printString("Info",20,0);
+        lcd.printString("Legionella Detector",0,1);
+        lcd.printString("Lewis Cameron",0,2);
+        lcd.printString("18689002",0,3);
+        lcd.printString("Press Back To Go Home",0,4);
+        
+        if (button_back_flag) {
+        button_back_flag = 0;
+        page = 0;
+    }
+    
+    }      
+             
+     
+       
+        break;    
+        {
+        default:
+        init_pcb();
+        lcd.clear();
+        
+        lcd.printString("Main Menu",0,0);
+        lcd.printString("A = Cold Water",0,1);
+        lcd.printString("B = Hot Water",0,2);
+        lcd.printString("X = Custom SP",0,3);
+        lcd.printString("Y = Info",0,4);
+        lcd.refresh();
+        
+        if (button_a_flag) {
+            button_a_flag = 0;
+            page = 1;
+        }
+        
+        if (button_b_flag) {
+            button_b_flag = 0;
+            page = 2;
+        }
+        
+        if (button_x_flag) {
+            button_x_flag = 0;
+            page = 3;
+        }
+        
+        if (button_y_flag) {
+            button_y_flag = 0;
+            page = 4;
+        }
+        
+    }
+}   
+    
+    
+}
+        
+void button_a_interrupt();
+{
+    button_a_flag = 1;
+}        
+ 
+void button_b_interrupt();
+{
+    button_b_flag = 1;
+}
+ 
+void button_x_interrupt();
+{
+    button_x_flag = 1;
+}
+ 
+void button_y_interrupt();
+{
+    button_y_flag = 1;
+}
+
+void button_left_interrupt();
+{
+    button_left_flag = 1;
+}
+
+void button_right_interrupt();
+{
+    button_right_flag = 1;
+}
+ 
+void button_back_interrupt();
+{
+    button_back_flag = 1;
+}    
+
+    
+void button_start_interrupt();
+{
+    button_start_flag = 1;
+}  
+
+void cwshigh();
+{
+    lcd.printString("Water Temp Too High",0,3);
+    led_red1 = 0;
+}
+
+void get_temp();
+{
+    char buffer[14];
+    float temp = tmp.get_temperature();
+    int length = sprintf(buffer,"%.2f C",temp); 
+    //pc.printf("temp = %f K\n",temp);
+    if (length <=14)
+    lcd.printString(buffer,20,2);
+    
+}
+
+void CustHi_SP();
+{
+    Direction d = Joystick.get_direction();
+    
+    
+    if (d = W) {
+        CustomHiSPChange = CustomHiSP - 1;
+        wait(0.5);
+        CustomHiSPChange = CustomHiSP;
+    }
+    
+    if (d = E) {
+        CustomHiSPChange = CustomHiSP + 1;
+        wait(0.5);
+        CustomHiSPChange = CustomHiSP;
+        
+        char buffer[14];
+        int length = sprintf(buffer,"HiLimSP=%.2iC",CustomHiSP);
+        
+        if (length <= 14); {
+            lcd.printString(buffer,0,3);
+        }
+    }
+}
+
+void CustLo_SP();
+{
+        if (button_left_flag) {
+            button_left_flag = 0;
+            CustomLoSPChange = CustomLoSP - 1;
+            wait(0.5);
+            CustomLoSPChange = CustomLoSP;
+        }
+        
+        if (button_right_flag) {
+            button_right_flag = 0;
+            CustomLoSPChange = CustomLoSP + 1;
+            wait(0.5);
+            CustomLoSPChange = CustomLoSP;
+        }
+        
+        char buffer[14];
+        int length = sprintf(buffer,"LoLimSP=%.2iC",CustomLoSP);
+        
+        if (length <= 14); {
+            lcd.printString(buffer,0,4);
+        }
+}
+        
+
+    
+}   
+