Benoit Moreau
/
Lab8_requirement8
fffffff
main.cpp@0:192d42444e3b, 2019-11-08 (annotated)
- Committer:
- aahnothebees
- Date:
- Fri Nov 08 18:54:51 2019 +0000
- Revision:
- 0:192d42444e3b
heelo
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
aahnothebees | 0:192d42444e3b | 1 | #include <mbed.h> |
aahnothebees | 0:192d42444e3b | 2 | #include <math.h> |
aahnothebees | 0:192d42444e3b | 3 | |
aahnothebees | 0:192d42444e3b | 4 | |
aahnothebees | 0:192d42444e3b | 5 | float readtempurature(AnalogIn *temp); //function prototype |
aahnothebees | 0:192d42444e3b | 6 | /********************************************************************************************************** |
aahnothebees | 0:192d42444e3b | 7 | Description: The purpose of this function is display the temperature value in a serial terminal |
aahnothebees | 0:192d42444e3b | 8 | |
aahnothebees | 0:192d42444e3b | 9 | 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. |
aahnothebees | 0:192d42444e3b | 10 | Below you will find a rough drawing of what the circuit should look like |
aahnothebees | 0:192d42444e3b | 11 | |
aahnothebees | 0:192d42444e3b | 12 | 3.3V---------Resistor---wire from A1-------Thermistor-------GND |
aahnothebees | 0:192d42444e3b | 13 | |
aahnothebees | 0:192d42444e3b | 14 | Parameters: |
aahnothebees | 0:192d42444e3b | 15 | |
aahnothebees | 0:192d42444e3b | 16 | temp->Defines the pin from which to read data/defines the analog input pin used to measure the voltage of the thermistor |
aahnothebees | 0:192d42444e3b | 17 | ->Pins assignments are for a nucleo F411RE |
aahnothebees | 0:192d42444e3b | 18 | ->A1 is the pin used for this program |
aahnothebees | 0:192d42444e3b | 19 | t->defines the timer object used to calculate the time it takes to run through the the readtermpurature function |
aahnothebees | 0:192d42444e3b | 20 | |
aahnothebees | 0:192d42444e3b | 21 | Returns: |
aahnothebees | 0:192d42444e3b | 22 | Nothing |
aahnothebees | 0:192d42444e3b | 23 | **********************************************************************************************************/ |
aahnothebees | 0:192d42444e3b | 24 | Timer t;//defines a timer object |
aahnothebees | 0:192d42444e3b | 25 | int main(){ |
aahnothebees | 0:192d42444e3b | 26 | AnalogIn temp (A1); //defines pin A1 on the nucleo F411RE as an analog input |
aahnothebees | 0:192d42444e3b | 27 | //for(;;){ //for-loop that will allow the program to run through multiple iterations |
aahnothebees | 0:192d42444e3b | 28 | float tempurature; |
aahnothebees | 0:192d42444e3b | 29 | t.start();//starts the timer |
aahnothebees | 0:192d42444e3b | 30 | tempurature=readtempurature(&temp); |
aahnothebees | 0:192d42444e3b | 31 | printf("The tempurature is: %f\n",tempurature);//prints the tempurature in celcius to the serial terminal |
aahnothebees | 0:192d42444e3b | 32 | t.stop();//stops the timer |
aahnothebees | 0:192d42444e3b | 33 | 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 |
aahnothebees | 0:192d42444e3b | 34 | wait(0.5); //waits for half a second before running the next iteration |
aahnothebees | 0:192d42444e3b | 35 | // } |
aahnothebees | 0:192d42444e3b | 36 | } |
aahnothebees | 0:192d42444e3b | 37 | /********************************************************************************************************** |
aahnothebees | 0:192d42444e3b | 38 | Description: The purpose of this function is to calculate the temperature being read by the thermistor |
aahnothebees | 0:192d42444e3b | 39 | 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. |
aahnothebees | 0:192d42444e3b | 40 | Below you will find a rough drawing of what the circuit should look like |
aahnothebees | 0:192d42444e3b | 41 | |
aahnothebees | 0:192d42444e3b | 42 | 3.3V---------Resistor---wire from A1-------Thermistor-------GND |
aahnothebees | 0:192d42444e3b | 43 | |
aahnothebees | 0:192d42444e3b | 44 | Parameters: |
aahnothebees | 0:192d42444e3b | 45 | |
aahnothebees | 0:192d42444e3b | 46 | temp->Defines the analog input pin used to measure the voltage of the thermistor |
aahnothebees | 0:192d42444e3b | 47 | |
aahnothebees | 0:192d42444e3b | 48 | |
aahnothebees | 0:192d42444e3b | 49 | Returns: |
aahnothebees | 0:192d42444e3b | 50 | celcius: Returns the tempurature measured by the thermistor in degrees celsius as a float value |
aahnothebees | 0:192d42444e3b | 51 | **********************************************************************************************************/ |
aahnothebees | 0:192d42444e3b | 52 | float readtempurature(AnalogIn *temp) |
aahnothebees | 0:192d42444e3b | 53 | { |
aahnothebees | 0:192d42444e3b | 54 | float tempVal; //variable that stores the voltage reading of the thermistor as a value between 1 and zero |
aahnothebees | 0:192d42444e3b | 55 | tempVal=temp->read(); //reads the voltage of the thermistor on analog pin 1 of the nucleo f411RE |
aahnothebees | 0:192d42444e3b | 56 | float vrt; //variable that stores the actual value of the voltage of the thermistor |
aahnothebees | 0:192d42444e3b | 57 | vrt=(tempVal*3.3);//this formula determines the actual voltage of the thermistor |
aahnothebees | 0:192d42444e3b | 58 | float top;//variable that defines the numerator of the formula to determine the resistance of the thermistor |
aahnothebees | 0:192d42444e3b | 59 | top=(vrt*10000);//numerator of the formula used to determine the thermisistor resistance |
aahnothebees | 0:192d42444e3b | 60 | float bottom; //variable that defines the denominator of the formula to determine the resistance of the thermistor |
aahnothebees | 0:192d42444e3b | 61 | bottom=(3.3-vrt); //denominator of the formula to determine the thermisistor resistance |
aahnothebees | 0:192d42444e3b | 62 | float rt=(top/bottom);//this is the formula to determine the thermisitor resistance |
aahnothebees | 0:192d42444e3b | 63 | float A=(3.354016e-3);//Constant A1 in the formula used to calculate the temperature given the resistance of the thermisitor |
aahnothebees | 0:192d42444e3b | 64 | float B=(2.569650e-4);//Constant B1 in the formula used to calculate the temperature given the resistance of the thermisitor |
aahnothebees | 0:192d42444e3b | 65 | float C=(2.620131e-6);//Constant C1 in the formula used to calculate the temperature given the resistance of the thermisitor |
aahnothebees | 0:192d42444e3b | 66 | float D=(6.383091e-8);//Constant D1 in the formula used to calculate the temperature given the resistance of the thermisitor |
aahnothebees | 0:192d42444e3b | 67 | float ln1=(log(rt/10000)); //First ln operation in the formula used to calculate the temperature given the resistance of the thermisitor |
aahnothebees | 0:192d42444e3b | 68 | float ln2=log(pow((rt/10000),2));//Second ln operation in the formula used to calculate the temperature given the resistance of the thermisitor |
aahnothebees | 0:192d42444e3b | 69 | float ln3=log(pow((rt/10000),3));//Second ln operation in the formula used to calculate the temperature given the resistance of the thermisitor |
aahnothebees | 0:192d42444e3b | 70 | 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 |
aahnothebees | 0:192d42444e3b | 71 | float celcius=((1/denominator)-273.15);//completed formula to calculate the tempurature given the resistance of the thermisitor |
aahnothebees | 0:192d42444e3b | 72 | |
aahnothebees | 0:192d42444e3b | 73 | return celcius; //returns the value of the tempurature in degrees celsius |
aahnothebees | 0:192d42444e3b | 74 | } |
aahnothebees | 0:192d42444e3b | 75 |