fffffff

Dependencies:   mbed

main.cpp

Committer:
aahnothebees
Date:
2019-11-08
Revision:
0:192d42444e3b

File content as of revision 0:192d42444e3b:

#include <mbed.h>
#include <math.h>


float readtempurature(AnalogIn *temp); //function prototype
/**********************************************************************************************************
    Description: The purpose of this function is display the temperature value in a serial terminal
    
        In order for this code to work, you must connect a 10k resistor in series with the thermistor. The wire from A1 on the nucleo f411RE must be connected after the resistor.  The voltage being used in this circuit is 3.3V.
        Below you will find a rough drawing of what the circuit should look like 
        
                 3.3V---------Resistor---wire from A1-------Thermistor-------GND
    
    Parameters:
           
            temp->Defines the pin from which to read data/defines the analog input pin used to measure the voltage of the thermistor
                            ->Pins assignments are for a nucleo F411RE
                                 ->A1 is the pin used for this program
            t->defines the timer object used to calculate the time it takes to run through the the readtermpurature function
            
    Returns:
            Nothing
**********************************************************************************************************/
Timer t;//defines a timer object
int main(){
    AnalogIn temp (A1); //defines pin A1 on the nucleo F411RE as an analog input
    //for(;;){ //for-loop that will allow the program to run through multiple iterations
        float tempurature;
        t.start();//starts the timer
        tempurature=readtempurature(&temp);
        printf("The tempurature is: %f\n",tempurature);//prints the tempurature in celcius to the serial terminal
        t.stop();//stops the timer
        printf("The time taken was %f seconds\n", t.read()); //prints the time it took to run through the readtempurature(&temp) function to the serial terminal
        wait(0.5); //waits for half a second before running the next iteration
   // }
}
/**********************************************************************************************************
    Description: The purpose of this function is to calculate the temperature being read by the thermistor
        In order for this code to work, you must connect a 10k resistor in series with the thermistor. The wire from A1 on the nucleo f411RE must be connected after the resistor.  The voltage being used in this circuit is 3.3V.
        Below you will find a rough drawing of what the circuit should look like 
        
                 3.3V---------Resistor---wire from A1-------Thermistor-------GND
    
    Parameters:
           
            temp->Defines the analog input pin used to measure the voltage of the thermistor
                          
            
    Returns:
            celcius: Returns the tempurature measured by the thermistor in degrees celsius as a float value
**********************************************************************************************************/
float readtempurature(AnalogIn *temp)
{
    float tempVal; //variable that stores the voltage reading of the thermistor as a value between 1 and zero 
    tempVal=temp->read(); //reads the voltage of the thermistor on analog pin 1 of the nucleo f411RE
    float vrt; //variable that stores the actual value of the voltage of the thermistor
    vrt=(tempVal*3.3);//this formula determines the actual voltage of the thermistor
    float top;//variable that defines the numerator of the formula to determine the resistance of the thermistor
    top=(vrt*10000);//numerator of the formula used to determine the thermisistor resistance
    float bottom; //variable that defines the denominator of the formula to determine the resistance of the thermistor
    bottom=(3.3-vrt); //denominator of the formula to determine the thermisistor resistance
    float rt=(top/bottom);//this is the formula to determine the thermisitor resistance
    float A=(3.354016e-3);//Constant A1 in the formula used to calculate the temperature given the resistance of the thermisitor
    float B=(2.569650e-4);//Constant B1 in the formula used to calculate the temperature given the resistance of the thermisitor
    float C=(2.620131e-6);//Constant C1 in the formula used to calculate the temperature given the resistance of the thermisitor
    float D=(6.383091e-8);//Constant D1 in the formula used to calculate the temperature given the resistance of the thermisitor
    float ln1=(log(rt/10000)); //First ln operation in the formula used to calculate the temperature given the resistance of the thermisitor
    float ln2=log(pow((rt/10000),2));//Second ln operation in the formula used to calculate the temperature given the resistance of the thermisitor
    float ln3=log(pow((rt/10000),3));//Second ln operation in the formula used to calculate the temperature given the resistance of the thermisitor
    float denominator=(A+(B*ln1)+(C*ln2)+(D*ln3)); //complete denominator of the formula used to calculate the temperature given the resistance of the thermisitor
    float  celcius=((1/denominator)-273.15);//completed formula to calculate the tempurature given the resistance of the thermisitor
    
    return celcius; //returns the value of the tempurature in degrees celsius
}