Servo that moves based on temperature readings from a thermistor

Dependencies:   mbed

Revision:
0:e043caac158b
Child:
1:563cf31920da
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Wed Nov 13 16:17:15 2019 +0000
@@ -0,0 +1,45 @@
+#include <mbed.h>
+#include <math.h>
+
+float readtempurature(AnalogIn *temp); //function prototype
+
+
+int main() 
+{
+    PwmOut servo(A0);  
+    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;
+        float y;//float value that stores the duty-cyle calculated on line 15
+        tempurature=readtempurature(&temp);
+        y = (((0.3*tempurature) + 3)/100); //formula that converts the temperature measured by the thermistor into an equivalent duty-cycle, in order for the servo to display an accurate 1:1 scale, change the constant from 0.3 to 0.05
+        servo.period(0.02);  // sets the period of the servo PWM to 20 ms as specifed in lesson 7
+        servo.write(y); //writes the calculated duty-cycle to the servo 
+        printf("The tempurature is: %f\n",tempurature);//prints the temperature in celcius to the serial terminal
+        wait(0.5); //waits for half a second before running the next iteration
+    }
+}
+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
+}
+    
\ No newline at end of file