93 lines
1.8 KiB
C++
Executable File
93 lines
1.8 KiB
C++
Executable File
#define THERMISTOR_PIN 0
|
|
// r0: 100000
|
|
// t0: 25
|
|
// r1: 0
|
|
// r2: 4700
|
|
// beta: 4092
|
|
// max adc: 1023
|
|
|
|
//https://learn.adafruit.com/thermistor/using-a-thermistor
|
|
//https://github.com/miguel5612/Arduino-ThermistorLibrary
|
|
|
|
//Use a basic example.
|
|
//Connect the termistor to GND and A0 pin.
|
|
//Connect a 4k7 resistor series to VCC.
|
|
//Connect a 10Uf into A0 to GND.
|
|
//And Have fun, the library works
|
|
|
|
#define NUMTEMPS 20
|
|
short temptable[NUMTEMPS][2] = {
|
|
{1, 821},
|
|
{54, 252},
|
|
{107, 207},
|
|
{160, 182},
|
|
{213, 165},
|
|
{266, 152},
|
|
{319, 141},
|
|
{372, 131},
|
|
{425, 123},
|
|
{478, 115},
|
|
{531, 107},
|
|
{584, 100},
|
|
{637, 93},
|
|
{690, 86},
|
|
{743, 78},
|
|
{796, 70},
|
|
{849, 60},
|
|
{902, 49},
|
|
{955, 34},
|
|
{1008, 3}
|
|
};
|
|
void setup()
|
|
{
|
|
Serial.begin(9600);
|
|
Serial.println("Starting temperature exerciser.");
|
|
}
|
|
|
|
void loop()
|
|
{
|
|
int rawvalue = analogRead(THERMISTOR_PIN);
|
|
int celsius = read_temp();
|
|
int fahrenheit = (((celsius * 9) / 5) + 32);
|
|
|
|
Serial.print("Current temp: ");
|
|
Serial.print(celsius);
|
|
Serial.print("C / ");
|
|
Serial.print(fahrenheit);
|
|
Serial.println("F");
|
|
|
|
Serial.print("Raw value: ");
|
|
Serial.println(rawvalue);
|
|
Serial.println(" ");
|
|
|
|
delay(1000);
|
|
}
|
|
|
|
int read_temp()
|
|
{
|
|
int rawtemp = analogRead(THERMISTOR_PIN);
|
|
int current_celsius = 0;
|
|
|
|
byte i;
|
|
for (i=1; i<NUMTEMPS; i++)
|
|
{
|
|
if (temptable[i][0] > rawtemp)
|
|
{
|
|
int realtemp = temptable[i-1][1] + (rawtemp - temptable[i-1][0]) * (temptable[i][1] - temptable[i-1][1]) / (temptable[i][0] - temptable[i-1][0]);
|
|
|
|
if (realtemp > 255)
|
|
realtemp = 255;
|
|
|
|
current_celsius = realtemp;
|
|
|
|
break;
|
|
}
|
|
}
|
|
|
|
// Overflow: We just clamp to 0 degrees celsius
|
|
if (i == NUMTEMPS)
|
|
current_celsius = 0;
|
|
|
|
return current_celsius;
|
|
}
|