const float RESOLUTION_ADC    = .0049; // Default ADC resolution for the 5V reference = 0.049 [V] / unit
const float RESOLUTION_SENSOR = .01;   // Sensor resolution = 0.01 V / °C

// Wokwi ONLY!!! In the labs we use the LM50, not the NTC available in Wokwi
const float BETA = 3950; // should match the Beta Coefficient of the thermistor

void setup() {
  Serial.begin(115200);
}

void loop() {
  Serial.print("Temp [°C]: ");
  const float temp = readTempInCelsius(10, A0); // Read temp. 10 times and returns the average
  Serial.println(temp); // Display the result
  delay(200);
}

float readTempInCelsius(int count, int pin) {
  // read temp. count times from the analog pin
  float sumTemp = 0;
  for (int i = 0; i < count; i++) {
    int reading = analogRead(pin);
    float voltage = reading * RESOLUTION_ADC;
    
    // Subtract the DC offset and converts the value to C degrees in Celsius (°C)
    float tempCelsius = (voltage - 0.5) / RESOLUTION_SENSOR ;
        
    // Uncomment for labs when not in Wokwi
    // sumTemp = sumTemp + tempCelsius; // Accumulates the readings

    // Wokwi ONLY!!!
    float wokwi_temp = 1 / (log(1 / (1023. / reading - 1)) / BETA + 1.0 / 298.15) - 273.15;
    sumTemp = sumTemp + wokwi_temp;
  }
  return sumTemp / (float)count; // Return the average value
}