/* A demonstration program to show the difference between using a manual
calibration of a sensor vs map(). Basically comes down to rounding and speed.
map is faster, but integer only. Manual calibration can use floating point
operations to get a more accurate result.
map uses truncation to convert to integer. This may produce numbers that are too
low. With floating point you can use the round() function which correctly
rounds to the nearest integer.
This example uses thermistor data from a KY-013
Troubleshooting: If you consistently get the intercept back when trying to convert
sensor readings, then you probably are getting a zero for your sensor reading.
this probably means you have your sensor S and G pins reversed.
PM Wiegand July 2025
*/
const int lowTemp = 33;
const int hiTemp = 101;
const int lowAD = 750;
const int hiAD = 360;
const float manSlope = ((float)hiTemp - (float)lowTemp) / ((float)hiAD-(float)lowAD);
const float manInt = (float)hiTemp - (manSlope * (float)hiAD);
void setup() {
Serial.begin(9600);
Serial.println ("Calculated slope=" + String(manSlope,5));
Serial.println("Calculated intercept=" + String(manInt,4));
}
void loop() {
float manTemp;
int mapTemp;
int startRead = 400;
do {
manTemp = startRead * manSlope + manInt;
mapTemp = map(startRead, lowAD, hiAD, lowTemp, hiTemp);
Serial.println("A/D reading:" + String(startRead) + " manTemp:" + String(manTemp) + " mapTemp:" + String(mapTemp) + " manRounded:" + String(round(manTemp)));
startRead = startRead + 50;
} while (startRead <= 700);
do {} while(true);
}