Proof of concept for distance and temperature monitoring

Dependencies:   mbed mbedConnectorInterface mbedEndpointNetwork

Committer:
mnorris
Date:
Fri May 01 23:30:11 2015 +0000
Revision:
2:fa0acc891712
Parent:
0:cb422b231ea5
Michael testing testing 1 2 3

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Luminoscity 0:cb422b231ea5 1 #include "mbed.h"
Luminoscity 0:cb422b231ea5 2 #include <math.h>
Luminoscity 0:cb422b231ea5 3 #include "GroveTemp.h"
mnorris 2:fa0acc891712 4 //
Luminoscity 0:cb422b231ea5 5 AnalogIn thermistor(A0); /* Temperature sensor connected to Analog Grove connector */
Luminoscity 0:cb422b231ea5 6
Luminoscity 0:cb422b231ea5 7 GroveTempSensor::GroveTempSensor() {
Luminoscity 0:cb422b231ea5 8 resetMinMax();
Luminoscity 0:cb422b231ea5 9 }
Luminoscity 0:cb422b231ea5 10
Luminoscity 0:cb422b231ea5 11 float GroveTempSensor::getTemp() {
Luminoscity 0:cb422b231ea5 12 unsigned int analogRead;
Luminoscity 0:cb422b231ea5 13 // units,
Luminoscity 0:cb422b231ea5 14 // tens;
Luminoscity 0:cb422b231ea5 15
Luminoscity 0:cb422b231ea5 16 analogRead = thermistor.read_u16(); /* Read analog value */
Luminoscity 0:cb422b231ea5 17
Luminoscity 0:cb422b231ea5 18 /* Calculate the resistance of the thermistor from analog votage read. */
Luminoscity 0:cb422b231ea5 19 resistance = (float) 10000.0 * ((65536.0 / analogRead) - 1.0);
Luminoscity 0:cb422b231ea5 20
Luminoscity 0:cb422b231ea5 21 /* Convert the resistance to temperature using Steinhart's Hart equation */
Luminoscity 0:cb422b231ea5 22 temperature = (1/((log(resistance/10000.0)/TEMP_BETA) + (1.0/298.15)))-273.15;
Luminoscity 0:cb422b231ea5 23
Luminoscity 0:cb422b231ea5 24 // units = (int) temperature % 10;
Luminoscity 0:cb422b231ea5 25 // tens = (int) temperature / 10;
Luminoscity 0:cb422b231ea5 26 if (temperature > maxTemp)
Luminoscity 0:cb422b231ea5 27 maxTemp = temperature;
Luminoscity 0:cb422b231ea5 28 if (temperature < minTemp)
Luminoscity 0:cb422b231ea5 29 minTemp = temperature;
Luminoscity 0:cb422b231ea5 30
Luminoscity 0:cb422b231ea5 31 return temperature;
Luminoscity 0:cb422b231ea5 32 }
Luminoscity 0:cb422b231ea5 33
Luminoscity 0:cb422b231ea5 34 void GroveTempSensor::resetMinMax() {
Luminoscity 0:cb422b231ea5 35 maxTemp = -999.9;
Luminoscity 0:cb422b231ea5 36 minTemp = 999.9;
Luminoscity 0:cb422b231ea5 37 minMaxDefined = false;
Luminoscity 0:cb422b231ea5 38 }
Luminoscity 0:cb422b231ea5 39
Luminoscity 0:cb422b231ea5 40 float GroveTempSensor::getMin() {
Luminoscity 0:cb422b231ea5 41 if (!minMaxDefined)
Luminoscity 0:cb422b231ea5 42 getTemp();
Luminoscity 0:cb422b231ea5 43 return minTemp;
Luminoscity 0:cb422b231ea5 44 }
Luminoscity 0:cb422b231ea5 45
Luminoscity 0:cb422b231ea5 46 float GroveTempSensor::getMax() {
Luminoscity 0:cb422b231ea5 47 if (!minMaxDefined)
Luminoscity 0:cb422b231ea5 48 getTemp();
Luminoscity 0:cb422b231ea5 49 return maxTemp;
Luminoscity 0:cb422b231ea5 50 }