temperature and distance

Fork of TrashSensors by Corey Ford

Committer:
coyotebush
Date:
Sun May 24 20:45:32 2015 +0000
Revision:
0:3f335fc50cef
Child:
1:9cd59726cc5e
extract sensor code

Who changed what in which revision?

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