Hi All,
I'm in the process of making a custom library. This is my first crack at making libraries so I thought I would start with a code set I'm very familiar with:
My thermistors are reacting but the reading are not accruate at all. My libraries work when I put the RNOM, TNOM, BCOE, SRES parameters in the globals.h instead of feeding them through via the AC_Test.ino file ( I would rather do this for when I use this library for various other thermistors). I plan on continueing the development with this library (variables for NTC/PTC and other things) and making it public for others to use.
globals.h:
#pragma once
#ifndef ARDUINO_H
#include <Arduino.h>
#endif
#define tms_period 10 //Ten millisecond period
#define NSAMP 5 // Number of smaples taken for average
thermistor.h:
#ifndef thermistor_h
#define thermistor_h
#include "globals.h"
class ThermADC
{
public:
ThermADC(uint8_t pin, uint16_t RNOM, uint8_t TNOM, uint16_t BCOE, uint16_t SRES);
uint16_t read();
private:
uint8_t pin; // analog input
uint16_t RNOM;//nominal value of resistance of the thermistor at 25 *C
uint8_t TNOM;//nominal temperature for 10kΩ resistance
uint16_t BCOE;// Beta coeffiecient fo the thermistor
uint16_t SRES;// Value of R1 in the voltage divider
void setThermistorParameters(uint8_t pin, uint16_t RNOM, uint8_t TNOM, uint16_t BCOE, uint16_t SRES);
uint8_t getPin();
uint16_t getRNOM();
uint8_t getTNOM();
uint16_t getBCOE();
uint16_t getSRES();
};
#endif
thermistor.cpp file:
#include "thermistor.h"
ThermADC::ThermADC(uint8_t pin, uint16_t RNOM, uint8_t TNOM, uint16_t BCOE, uint16_t SRES)
{
setThermistorParameters(pin,RNOM,TNOM,BCOE,SRES);
}
uint16_t ThermADC::read()
{
uint8_t i;
uint8_t samp[i];
for (i = 0; i < NSAMP; i++)
{
samp[i] = analogRead(getPin());
unsigned long current_millis_tms = millis();
while(millis() < current_millis_tms + tms_period)
{
// 10 millisecond delay
}
}
float average = 0;
for (i = 0; i < NSAMP; i++)
{
average += samp[i];
}
average /= NSAMP;
// convert the value to resistance
average = 1023 / average - 1;
average = getSRES() / average;
// Convert resistance to the temperature value
float steinhart;
steinhart = average / getRNOM() ; // (R/Ro)
steinhart = log(steinhart); // ln(R/Ro)
steinhart /= getBCOE(); // 1/B * ln(R/Ro)
steinhart += 1.0 / (getTNOM()+ 273.15); // + (1/To)
steinhart = 1.0 / steinhart; // Invert
steinhart -= 273.15; // convert absolute temp to C
float t = steinhart*(1.8) + 32; // convert to *F
return t;
}
void ThermADC::setThermistorParameters(uint8_t pin, uint16_t RNOM, uint8_t TNOM, uint16_t BCOE, uint16_t SRES)
{
this->pin = pin;
this->RNOM = RNOM;
this->TNOM = TNOM;
this->BCOE = BCOE;
this->SRES = SRES;
}
uint8_t ThermADC::getPin()
{
return this-> pin;
}
uint16_t ThermADC::getRNOM()
{
return this-> RNOM;
}
uint8_t ThermADC::getTNOM()
{
return this-> TNOM;
}
uint16_t ThermADC::getBCOE()
{
return this-> BCOE;
}
uint16_t ThermADC::getSRES()
{
return this-> SRES;
}
.ino file:
#include <thermistor.h>
//Timer Variables
unsigned long current_millis = 0;
//Timer Definitions
#define sec_period 1000
//Thermistor: ThermADC [varible]([analog pin],[Thermistor nominal resistance],[Nominal Temperature],[Beta Coefficient],[Value of R1])
ThermADC t1(A0, 10000, 25, 3950, 10000);
ThermADC t2(A1, 10000, 25, 3950, 10000);
void setup()
{
Serial.begin(9600);
}
void loop()
{
//Timer/////////////////////////////////////////////////////////////////////////////////
current_millis = millis();
//Thermistor Readings////////////////////////////////////////////////
float o= t1.read();
float r = t2.read();
//Print Statments
Serial.print("Thermistor 1: ");
Serial.print(o);
Serial.println(" *F");
Serial.print("Thermistor 2: ");
Serial.print(r);
Serial.println(" *F");
Serial.println();
while(millis() < current_millis + sec_period)
{
// 1 second delay
}
}
Here is what the serial output is:
Thermistor 1: 8717.00 *F
Thermistor 2: 260.00 *F
Thermistor 1: 8717.00 *F
Thermistor 2: 260.00 *F
Thermistor 1: 8717.00 *F
Thermistor 2: 260.00 *F