temperature and distance

Fork of TrashSensors by Corey Ford

Committer:
Luminoscity
Date:
Thu May 28 23:11:33 2015 +0000
Revision:
1:9cd59726cc5e
Parent:
0:3f335fc50cef
Calibration at 5V;

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