Water control / Mbed 2 deprecated Water_pump

Dependencies:   DRV88255 mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers temp.cpp Source File

temp.cpp

00001 #include "temp.h"
00002 #include "TextLCD.h"
00003 #include <cmath>
00004 
00005 AnalogIn temp(p19);
00006 DigitalOut htr(p27);
00007 extern Serial pc;
00008 extern TextLCD lcd;
00009 extern bool htr_suspension, rfl_flag;
00010 extern float air_temp;
00011 
00012 float calc_temp() {
00013     float rt, t, voltage = 3.3 * temp.read();;
00014     rt = R1*voltage/(3.3-voltage);
00015     t = 1 / (K0 + K1 * log(rt) + K2 * pow(log(rt),3.0f));
00016     return t - 273.15;
00017 }
00018 
00019 float take_air_temp() {
00020     pc.printf("Going to take air temperature. Please make sure that the sensor is in the air and connected to the controller\n");
00021     pc.printf("If you are ready, please press switch 3\n");
00022     //pc.printf("rfl_flag = %s\n", rfl_flag ? "true":"false");
00023     while (!rfl_flag) {wait(0.1);}        //Wait until switch 4 has been pressed
00024     rfl_flag = false;
00025     return calc_temp();
00026 }
00027 
00028 void print_temp(float temp) {
00029     pc.printf("Temperature = %.3f\n", temp);
00030     lcd.cls();
00031     lcd.printf("Temp: %.3f\n", temp);
00032 }
00033 
00034 bool sensor_out_of_water_check(float cT) {
00035     if (air_temp-ATB<cT && cT<air_temp+ATB) {                                   //sensor out of water
00036         pc.printf("Temperature similar to air temperature. Highly likely that the sensor is out of the water\n");
00037         pc.printf("Please check the temperature sensor position\n");
00038         htr_suspension = true;                                                  //Suspend heater
00039         pc.printf("For safety reasons, heater operation has been suspended. If everything is OK, resume operation by pressing switch 4\n");
00040         return false;
00041     }
00042     return true;
00043 }
00044 
00045 bool connection_check(float cT) {
00046     if (cT<LC) {                                                                //Connection lost
00047         pc.printf("Temperature extremely freezingly low! Highly likely that the temp sensor connection is lost\n");
00048         pc.printf("Please check the temperature sensor connection\n");
00049         htr_suspension = true;                                                  //Suspend heater
00050         pc.printf("For safety reasons, heater operation has been suspended. If everything is OK, resume operation by pressing switch 4\n");
00051         return false;
00052     }
00053     return true;
00054 }
00055 
00056 bool malfunction_check(float cT) {
00057     if (cT<TMLL || cT>TMUL) {                                                   //Bizzare temps
00058         pc.printf("Temperature sensor is giving bizzare temps\n");
00059         pc.printf("Temperature sensor malfunction is higly likely! Please check the sensor\n");
00060         htr_suspension = true;                                                  //Suspend heater
00061         pc.printf("For safety reasons, heater operation has been suspended. If everything is OK, resume operation by pressing switch 4\n");
00062         return false;
00063     }
00064     return true;
00065 }
00066 
00067 void heater_control(float cT) {
00068     if (cT<=TSLL) {
00069         if(!htr_suspension) {
00070             htr=1;  pc.printf("Heater activated\n");
00071             pc.printf("Expecting temperature to rise\n");
00072         }
00073         else { pc.printf("Heater operation is suspended! If you want to resume operation press switch 4\n");}
00074     }
00075     else if(cT>=TSUL) {
00076         htr=0;
00077         pc.printf("Heater deactivated\n");
00078         pc.printf("Expecting temperature to fall\n");
00079     }
00080 }
00081 
00082 bool temp_control(){
00083     bool temp_state = true;                                                     //Assume normal temperature
00084     float cT = calc_temp();                                                     //Calculate temperature
00085     print_temp(cT);                                                             //Print temp to lcd and pc
00086     temp_state = connection_check(cT);                                          //connection check
00087     temp_state = malfunction_check(cT);                                         //Malfunction check
00088     temp_state = sensor_out_of_water_check(cT);                                 //Sensor out of water check
00089     heater_control(cT);                                                         //Heater control
00090     wait(3);                                                                    //Introduce a 3" delay in order for the changes to have an effect
00091     cT = calc_temp();                                                           //Calculate temperature again    
00092     if (cT<TALL) {
00093         temp_state = false;
00094         pc.printf("Temperature really low! Heater must be activated!\n");
00095         if(htr==1) pc.printf("Heater already activated so beaker is exposed to freezing conditions");
00096     }
00097     if (cT>TAUL) {
00098         temp_state = false;
00099         pc.printf("Temperature really high!\n");
00100         if(htr==0) pc.printf("Heater already deactivated so beaker is exposed to an external heating source or heater is powered directly from the mains!\nPlease check heater connection!");
00101     }
00102     pc.printf("\n");
00103     return temp_state;
00104 }