Servo that moves based on temperature readings from a thermistor

Dependencies:   mbed

Committer:
aahnothebees
Date:
Wed Nov 13 17:11:08 2019 +0000
Revision:
3:1dd8980f5f07
Parent:
2:319ee9815aba
take 2;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
aahnothebees 0:e043caac158b 1 #include <mbed.h>
aahnothebees 0:e043caac158b 2 #include <math.h>
aahnothebees 0:e043caac158b 3
aahnothebees 0:e043caac158b 4 float readtempurature(AnalogIn *temp); //function prototype
aahnothebees 0:e043caac158b 5
aahnothebees 0:e043caac158b 6
aahnothebees 0:e043caac158b 7 int main()
aahnothebees 0:e043caac158b 8 {
aahnothebees 0:e043caac158b 9 PwmOut servo(A0);
aahnothebees 0:e043caac158b 10 AnalogIn temp (A1); //defines pin A1 on the nucleo F411RE as an analog input
aahnothebees 0:e043caac158b 11 for(;;){ //for-loop that will allow the program to run through multiple iterations
aahnothebees 0:e043caac158b 12 float tempurature;
aahnothebees 0:e043caac158b 13 float y;//float value that stores the duty-cyle calculated on line 15
aahnothebees 2:319ee9815aba 14 int bite;
aahnothebees 1:563cf31920da 15 tempurature=readtempurature(&temp); //calls the temperature function and stores its return value
aahnothebees 0:e043caac158b 16 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
aahnothebees 0:e043caac158b 17 servo.period(0.02); // sets the period of the servo PWM to 20 ms as specifed in lesson 7
aahnothebees 0:e043caac158b 18 servo.write(y); //writes the calculated duty-cycle to the servo
aahnothebees 3:1dd8980f5f07 19 bite=((28.333333333333*(y*100))-85);
aahnothebees 0:e043caac158b 20 printf("The tempurature is: %f\n",tempurature);//prints the temperature in celcius to the serial terminal
aahnothebees 0:e043caac158b 21 wait(0.5); //waits for half a second before running the next iteration
aahnothebees 0:e043caac158b 22 }
aahnothebees 0:e043caac158b 23 }
aahnothebees 0:e043caac158b 24 float readtempurature(AnalogIn *temp)
aahnothebees 0:e043caac158b 25 {
aahnothebees 0:e043caac158b 26 float tempVal; //variable that stores the voltage reading of the thermistor as a value between 1 and zero
aahnothebees 0:e043caac158b 27 tempVal=temp->read(); //reads the voltage of the thermistor on analog pin 1 of the nucleo f411RE
aahnothebees 0:e043caac158b 28 float vrt; //variable that stores the actual value of the voltage of the thermistor
aahnothebees 0:e043caac158b 29 vrt=(tempVal*3.3);//this formula determines the actual voltage of the thermistor
aahnothebees 0:e043caac158b 30 float top;//variable that defines the numerator of the formula to determine the resistance of the thermistor
aahnothebees 0:e043caac158b 31 top=(vrt*10000);//numerator of the formula used to determine the thermisistor resistance
aahnothebees 0:e043caac158b 32 float bottom; //variable that defines the denominator of the formula to determine the resistance of the thermistor
aahnothebees 0:e043caac158b 33 bottom=(3.3-vrt); //denominator of the formula to determine the thermisistor resistance
aahnothebees 0:e043caac158b 34 float rt=(top/bottom);//this is the formula to determine the thermisitor resistance
aahnothebees 0:e043caac158b 35 float A=(3.354016e-3);//Constant A1 in the formula used to calculate the temperature given the resistance of the thermisitor
aahnothebees 0:e043caac158b 36 float B=(2.569650e-4);//Constant B1 in the formula used to calculate the temperature given the resistance of the thermisitor
aahnothebees 0:e043caac158b 37 float C=(2.620131e-6);//Constant C1 in the formula used to calculate the temperature given the resistance of the thermisitor
aahnothebees 0:e043caac158b 38 float D=(6.383091e-8);//Constant D1 in the formula used to calculate the temperature given the resistance of the thermisitor
aahnothebees 0:e043caac158b 39 float ln1=(log(rt/10000)); //First ln operation in the formula used to calculate the temperature given the resistance of the thermisitor
aahnothebees 0:e043caac158b 40 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:e043caac158b 41 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:e043caac158b 42 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:e043caac158b 43 float celcius=((1/denominator)-273.15);//completed formula to calculate the tempurature given the resistance of the thermisitor
aahnothebees 0:e043caac158b 44
aahnothebees 0:e043caac158b 45 return celcius; //returns the value of the tempurature in degrees celsius
aahnothebees 0:e043caac158b 46 }
aahnothebees 0:e043caac158b 47