Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * Original code ported from Arduino website
- * Authors: Milan Malesevic and Zoran Stupic 2011.
- * Adaptations & port: Marcus Hund 2014
- * Inputs ADC Value from Thermistor and outputs Temperature in Celsius
- * requires: include <math.h>
- * Utilizes the Steinhart-Hart Thermistor Equation:
- * Temperature in Kelvin = 1 / {A + B[ln(R)] + C[ln(R)]3}
- * where A = 0.001129148, B = 0.000234125 and C = 8.76741E-08
- *
- * These coefficients seem to work fairly universally, which is a bit of a
- * surprise.
- *
- * Schematic:
- * [Ground] -- [10k-pad-resistor] -- | -- [thermistor] --[Vcc (5 or 3.3v)]
- * |
- * Analog Pin 0
- *
- * In case it isn't obvious (as it wasn't to me until I thought about it), the analog ports
- * measure the voltage between 0v -> Vcc which for an Arduino is a nominal 5v, but for (say)
- * a JeeNode, is a nominal 3.3v.
- *
- * The resistance calculation uses the ratio of the two resistors, so the voltage
- * specified above is really only required for the debugging that is commented out below
- *
- * Resistance = (1024 * PadResistance/ADC) - PadResistor
- *
- * I have used this successfully with some CH Pipe Sensors (http://www.atcsemitec.co.uk/pdfdocs/ch.pdf)
- * which be obtained from http://www.rapidonline.co.uk.
- *
- */
- #include "thermometers.h"
- thermometers::thermometers(){
- pad = 9300; //compensatie waarde om de verhouding tussen de NTC en de weerstand te calibreren 10000 is de startwaarde.
- };
- void thermometers::get(int index,String *out){
- char buf[32];
- *out = ftoa(buf,temperatures[index],1);
- }
- float thermometers::ntc(int RawADC) {
- long Resistance;
- float Temp; // Dual-Purpose variable to save space.
- Resistance = ((1024 * pad / RawADC) - pad);
- Temp = log(Resistance); // Saving the Log(resistance) so not to calculate it 4 times later
- Temp = 1 / (0.001129148 + (0.000234125 * Temp) + (0.0000000876741 * Temp * Temp * Temp));
- Temp = Temp - 273.15; // Convert Kelvin to Celsius
- // Uncomment this line for the function to return Fahrenheit instead.
- //temp = (Temp * 9.0)/ 5.0 + 32.0; // Convert to Fahrenheit
- return Temp; // Return the Temperature
- }
- void thermometers::print_temps() {
- for (int ii = 0; ii < NUM_THERMOMETERS; ii++) {
- Serial1.print(temperatures[ii], DEC);
- Serial1.print("\t");
- }
- Serial1.println();
- }
- void thermometers::read_temps() {
- float temp;
- int adc;
- for (int ii = 0; ii < NUM_THERMOMETERS; ii++) {
- delay(20); //<-- might not be needed in a spark....
- adc = analogRead(ii);
- #ifdef VERBOSE
- Serial1.print(adc,DEC);
- Serial1.print("\t");
- #endif
- // delay(100);
- temp = ntc(adc); // read ADC and convert it to Celsius
- temperatures[ii] = temp;
- }
- #ifdef VERBOSE
- Serial1.println();
- #endif
- }
- char *thermometers::ftoa(char *a, double f, int precision)
- {
- long p[] = {0,10,100,1000,10000,100000,1000000,10000000,100000000};
- char *ret = a;
- long num = (long)f;
- itoa(num, a, 10);
- while (*a != '\0') a++;
- *a++ = '.';
- long deci = abs((long)((f - num) * p[precision]));
- itoa(deci, a, 10);
- return ret;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement