Advertisement
marcushund

thermometers.cpp

Jun 25th, 2014
163
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.32 KB | None | 0 0
  1. /*
  2.  * Original code ported from Arduino website
  3.  * Authors: Milan Malesevic and Zoran Stupic 2011.
  4.  * Adaptations & port: Marcus Hund 2014
  5.  * Inputs ADC Value from Thermistor and outputs Temperature in Celsius
  6.  *  requires: include <math.h>
  7.  * Utilizes the Steinhart-Hart Thermistor Equation:
  8.  *    Temperature in Kelvin = 1 / {A + B[ln(R)] + C[ln(R)]3}
  9.  *    where A = 0.001129148, B = 0.000234125 and C = 8.76741E-08
  10.  *
  11.  * These coefficients seem to work fairly universally, which is a bit of a
  12.  * surprise.
  13.  *
  14.  * Schematic:
  15.  *   [Ground] -- [10k-pad-resistor] -- | -- [thermistor] --[Vcc (5 or 3.3v)]
  16.  *                                     |
  17.  *                                Analog Pin 0
  18.  *
  19.  * In case it isn't obvious (as it wasn't to me until I thought about it), the analog ports
  20.  * measure the voltage between 0v -> Vcc which for an Arduino is a nominal 5v, but for (say)
  21.  * a JeeNode, is a nominal 3.3v.
  22.  *
  23.  * The resistance calculation uses the ratio of the two resistors, so the voltage
  24.  * specified above is really only required for the debugging that is commented out below
  25.  *
  26.  * Resistance = (1024 * PadResistance/ADC) - PadResistor
  27.  *
  28.  * I have used this successfully with some CH Pipe Sensors (http://www.atcsemitec.co.uk/pdfdocs/ch.pdf)
  29.  * which be obtained from http://www.rapidonline.co.uk.
  30.  *
  31.  */
  32.  
  33. #include "thermometers.h"
  34.  
  35. thermometers::thermometers(){
  36.    pad = 9300; //compensatie waarde om de verhouding tussen de NTC en de weerstand te calibreren 10000 is de startwaarde.
  37.  
  38. };
  39.  
  40.  
  41.  
  42. void thermometers::get(int index,String *out){
  43.     char buf[32];
  44.     *out = ftoa(buf,temperatures[index],1);
  45.    
  46. }
  47.  
  48.  
  49. float thermometers::ntc(int RawADC) {
  50.     long Resistance;
  51.     float Temp; // Dual-Purpose variable to save space.
  52.  
  53.     Resistance = ((4096 * pad / RawADC) - pad);
  54.     Temp = log(Resistance); // Saving the Log(resistance) so not to calculate  it 4 times later
  55.     Temp = 1 / (0.001129148 + (0.000234125 * Temp) + (0.0000000876741 * Temp * Temp * Temp));
  56.     Temp = Temp - 273.15; // Convert Kelvin to Celsius                      
  57.     // Uncomment this line for the function to return Fahrenheit instead.
  58.     //temp = (Temp * 9.0)/ 5.0 + 32.0;                  // Convert to Fahrenheit
  59.     return Temp; // Return the Temperature
  60. }
  61.  
  62. void thermometers::print_temps() {
  63.     for (int ii = 0; ii < NUM_THERMOMETERS; ii++) {
  64.         Serial1.print(temperatures[ii], DEC);
  65.         Serial1.print("\t");
  66.     }
  67.     Serial1.println();
  68. }
  69.  
  70. void thermometers::read_temps() {
  71.     float temp;
  72.     int adc;
  73.    
  74.     for (int ii = 0; ii < NUM_THERMOMETERS; ii++) {
  75.      
  76.      
  77.             delay(20); //<-- might not be needed in a spark....
  78.         adc = analogRead(ii);
  79.        
  80.        
  81. #ifdef VERBOSE        
  82.           Serial1.print(adc,DEC);
  83.           Serial1.print("\t");
  84. #endif
  85.         // delay(100);
  86.         temp = ntc(adc); // read ADC and  convert it to Celsius
  87.  
  88.         temperatures[ii] = temp;
  89.     }
  90. #ifdef VERBOSE
  91.     Serial1.println();
  92. #endif
  93. }
  94.  
  95.  
  96. char *thermometers::ftoa(char *a, double f, int precision)
  97. {
  98.   long p[] = {0,10,100,1000,10000,100000,1000000,10000000,100000000};
  99.  
  100.   char *ret = a;
  101.   long num = (long)f;
  102.   itoa(num, a, 10);
  103.   while (*a != '\0') a++;
  104.   *a++ = '.';
  105.   long deci = abs((long)((f - num) * p[precision]));
  106.   itoa(deci, a, 10);
  107.   return ret;
  108. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement