const int potPin = A0; // Analog pin connected to potentiometer
// Resistance-temperature data points
const float resistance[] = {47.0, 667.0}; // Resistance values in ohms
const float temperature[] = {320.0, 140.0}; // Corresponding temperature values in Fahrenheit
void setup() {
Serial.begin(9600); // Initialize serial communication
}
void loop() {
int sensorValue = analogRead(potPin); // Read analog input
float voltage = sensorValue * (5.0 / 1023.0); // Convert analog reading to voltage
float resistanceValue = (500 * voltage) / (5 - voltage); // Calculate resistance value (assuming a 5kΩ potentiometer)
float tempF = mapResistanceToTemperature(resistanceValue);
int angle1 = map(tempF, 140, 320, 0, 180);;
Serial.print("Analog Value: ");
Serial.print(sensorValue);
Serial.print(", Voltage: ");
Serial.print(voltage);
Serial.print("V, Resistance: ");
Serial.print(resistanceValue);
Serial.print("Ω, Temperature: ");
Serial.print(tempF);
Serial.print("°F ");
Serial.println(angle1);
delay(500); // Delay for stability
}
float mapResistanceToTemperature(float resistanceValue) {
// Linear interpolation to map resistance to temperature
if (resistanceValue <= resistance[0]) {
return temperature[0];
} else if (resistanceValue >= resistance[1]) {
return temperature[1];
} else {
// Linear interpolation formula: y = mx + b
float m = (temperature[1] - temperature[0]) / (resistance[1] - resistance[0]);
float b = temperature[0] - m * resistance[0];
return m * resistanceValue + b;
}
}