![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
Proof of concept for distance and temperature monitoring
Dependencies: mbed mbedConnectorInterface mbedEndpointNetwork
GroveTemp.cpp@10:338191178cbf, 2015-05-18 (annotated)
- Committer:
- coyotebush
- Date:
- Mon May 18 00:44:59 2015 +0000
- Revision:
- 10:338191178cbf
- Parent:
- 2:fa0acc891712
max distance
Who changed what in which revision?
User | Revision | Line number | New 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 | } |