// Reference https://docs.wokwi.com/parts/wokwi-ntc-temperature-sensor

#define sensorPin A0
#define BETA 3950

int ledArray[] = {4,5,6,7,8,9,10,11,12,13};
const int numLEDs = 10;
const float minTemp = -24.0;
const float maxTemp = 80.0;
void setup() {
    // initialize LEDs and sensor pin
    for (int i = 0; i < numLEDs; i++) {
    pinMode(ledArray[i], OUTPUT);
  }
}

void loop() {
  // Read the sensor value , calculate the temperature, light up the leds
   // Read the sensor value
  int sensorValue = analogRead(sensorPin);

  // Convert the analog reading to temperature in Celsius
  float celsius = 1.0 / (log(1.0 * (1023.0 / sensorValue - 1.0)) / BETA + 1.0 / 298.15) - 273.15;

  // Map the temperature to the range of 0 to 10
  int ledLevel = map(celsius, minTemp, maxTemp, 0, numLEDs);

  // Ensure the ledLevel is within bounds
  ledLevel = constrain(ledLevel, 0, numLEDs);

  // Light up the corresponding number of LEDs
  for (int i = 0; i < numLEDs; i++) {
    if (i < ledLevel) {
      digitalWrite(ledArray[i],LOW );
    } else {
      digitalWrite(ledArray[i], HIGH);
    }
  }

  delay(10); // this speeds up the simulation
}