Temp/Humidity alarm

Dependencies:   mbed C12832

main.cpp

Committer:
ciaranom
Date:
2020-07-31
Revision:
0:2e716206ddde
Child:
1:42f6beb90ce3

File content as of revision 0:2e716206ddde:

#include "mbed.h"
#include "C12832.h"

Serial pc(USBTX, USBRX);//pc is an object of class Serial
DigitalOut led1(LED1), led2(LED2), led3(LED3), led4(LED4);//LED object
C12832 lcd(p5, p7, p6, p8, p11);// LCD object
AnalogIn pot1(p19), pot2(p20);// Pot object

// Relitive humidity from 0% to 100% 
// Temperature from -20°C to 140°C - See word doc for calculations

//LED4 = humidty high
//LED3 = temperature high
//LED2 = humidty low
//LED1 = temperature low



//bool check = true;

float RH;

float m=160, c=-20;
float temp;
float tempK;

float mintemp =5.0, minRH =20.0, maxtemp = 35.0, maxRH =80.0;
int i;

int main() {
    while(1) {
        
        //while(check)
       // {
            //check =false;
            //}
        
        
        temp = pot2*m + c;
        tempK= temp +273.15;
        RH = pot1*100;
        

        pc.printf(" Relative humidity: %.2f %%\r\n\n" , RH); //check %%
        
        pc.printf(" Temperarure: %.2f C\r\n Temperature: %.2f K\r\n\n\n", temp, tempK); //Show temperature in degree Celcius and in Kelvin
        //pc.cls();
        
        wait(3);
        lcd.cls();
        lcd.locate(0,20);
        lcd.printf("Relative humidity: %.2f %%", RH); //check %%
        
        lcd.locate(0,0);
        lcd.printf("Temperarure: %.2f C \nTemperature: %.2f K", temp, tempK);
        //lcd.locate(10,0);
        //lcd.printf("Temperarure: %.2f °C, Temperature: %.2f K", temp, tempK);
    
        
        
 //       if (temp < mintemp){
 //           for( i=0;i<3;i++){
 //               led1 = 1;
 //               wait(0.5);
 //               led1=0;
 //               wait(0.5);
 //               }              
 //           
 //           }
 //      if (RH < minRH){
 //           for( i=0;i<3;i++){
 //               led2 = 1;
 //               wait(0.5);
 //               led2=0;
//                wait(0.5);
//                }              
//            
//             }
//      if (temp > maxtemp){
//            for( i=0;i<3;i++){
//                led3 = 1;
//                wait(0.5);
//                led3=0;
//                wait(0.5);
//                }              
//            
//             }
//             
//       if (RH > maxRH){
//            for( i=0;i<3;i++){
//                led4 = 1;
//                wait(0.5);
//                led4=0;
//                wait(0.5);
//                }              
//            
//             }



// temp < mintemp and RH < minRH   1&2
// temp < mintemp and RH > minRH and RH < maxRH   1
// temp < mintemp and RH > maxRH   1&4
// temp > mintemp and temp < maxtemp and RH < minRH 2
// temp > mintemp and temp < maxtemp and RH > minRH and RH < maxRH noLED
// temp > mintemp and temp < maxtemp and RH > maxRH 4
// temp > maxtemp and RH < minRH 3&2
// temp > maxtemp and RH > minRH and RH < maxRH 3
// temp > maxtemp and RH > maxRH 3&4

      if (temp < mintemp and RH < minRH){
            pc.printf("Temperature too low. Please increase. \r\n Humidty too low. Please increase \r\n\n"); // adding\r for TeraTeam, to print starts at 0 on the x axis.
            while(temp < mintemp and RH < minRH){
            //for( i=0;i<3;i++){
                led1 = 1,led2 = 1;
                wait(0.5);
                led1 = 0,led2 = 0;
                wait(0.5);
                temp = pot2*m + c; //adding another check within the loop to see if temp and RH have moved back within limits
                RH = pot1*100;
                }  
                }            
            else if(temp < mintemp and RH > minRH and RH < maxRH){
            pc.printf("Temperature too low. Please increase \r\n\n");
            while (temp < mintemp and RH > minRH and RH < maxRH){
            //for( i=0;i<3;i++){
                //pc.printf("Temperature too low. Please increase \n");
                led1 = 1;
                wait(0.5);
                led1 = 0;
                wait(0.5);
                temp = pot2*m + c;
                RH = pot1*100;
                }
                }
            else if (temp < mintemp and RH > maxRH){
            pc.printf("Temperature too low. Please increase. \r\n Humidty too high. Please reduce \r\n\n");
            while (temp < mintemp and RH > maxRH){
            //for( i=0;i<3;i++){
                //pc.printf("Temperature too low. Please increase. \n Humidty too high. Please reduce \n");
                led1 = 1,led3 = 1;
                wait(0.5);
                led1 = 0,led3 = 0;
                wait(0.5);
                temp = pot2*m + c;
                RH = pot1*100;
                }
                }              
            else if (temp > mintemp and temp < maxtemp and RH < minRH){
           pc.printf("Humidty too low. Please increase \r\n\n");
           while (temp > mintemp and temp < maxtemp and RH < minRH){
           //for( i=0;i<3;i++){
                //pc.printf("Humidty too low. Please increase \n");
                led2 = 1;
                wait(0.5);
                led2 = 0;
                wait(0.5);
                temp = pot2*m + c;
                RH = pot1*100;
                }
                } 
                             
            else if (temp > mintemp and temp < maxtemp and RH > maxRH ){
            pc.printf("Humidty too high. Please reduce \r\n\n");
            while (temp > mintemp and temp < maxtemp and RH > maxRH ){
            //for( i=0;i<3;i++){
                //pc.printf("Humidty too high. Please reduce \n");
                led3 = 1;
                wait(0.5);
                led3 = 0;
                wait(0.5);
                temp = pot2*m + c;
                RH = pot1*100;
                }      
                }
                       
            else if (temp > maxtemp and RH < minRH){
             pc.printf("Temperature too high. Please reduce. \r\n Humidty too low. Please increase \r\n\n");
            while (temp > maxtemp and RH < minRH){
            //for( i=0;i<3;i++){
                //pc.printf("Temperature too high. Please reduce. \n Humidty too low. Please increase \n");
                led4 = 1,led2 = 1;
                wait(0.5);
                led4 = 0,led2 = 0;
                wait(0.5);
                temp = pot2*m + c;
                RH = pot1*100;                
                }
                }    
                          
        else if (temp > maxtemp and RH > minRH and RH < maxRH){
            pc.printf("Temperature too high. Please reduce \r\n\n");
            while (temp > maxtemp and RH > minRH and RH < maxRH){
            //for( i=0;i<3;i++){
                //pc.printf("Temperature too high. Please reduce \n");
                led4 = 1;
                wait(0.5);
                led4 = 0;
                wait(0.5);
                temp = pot2*m + c;
                RH = pot1*100;                
                }
                }     
                         
        else if (temp > maxtemp and RH > maxRH){
            pc.printf("Temperature too high. Please reduce. \r\n Humidty too high. Please reduce \r\n\n");
            while (temp > maxtemp and RH > maxRH){ 
            //for( i=0;i<3;i++){
                //pc.printf("Temperature too high. Please reduce. \n Humidty too high. Please reduce \n");
                led4 = 1,led3 = 1;
                wait(0.5);
                led4 = 0, led3 = 0;
                wait(0.5);
                temp = pot2*m + c;
                RH = pot1*100;
                }
                }              
            
             }

// instead of running through cycle 3 times with a for loop, while loop will continue to run while RH and temp are outside of parameters.



      
        

    }