// Reference https://docs.wokwi.com/parts/wokwi-ntc-temperature-sensor
#define sensorPin A0
#define BETA 3950
#define LED_COUNT 10
int ledArray[] = {4,5,6,7,8,9,10,11,12,13};
int led_ON_OFF[] = {LOW,LOW,LOW,LOW,LOW,LOW,LOW,LOW,LOW,LOW};
float quantize_temp[] = { -24.00, -13.60, -3.20, 7.20, 17.60, 28.00, 38.40, 48.80, 59.20, 69.60 , 80.00};
void setup() {
// initialize LEDs and sensor pin
for( int num = 0; num < LED_COUNT; num++ )
{
pinMode(ledArray[num], OUTPUT);
}
pinMode(sensorPin, INPUT);
//Serial.begin(9600);
}
void loop() {
// Read the sensor value , calculate the temperature, light up the leds
int analog_Value = analogRead(sensorPin);
float celsius = 1 / (log(1 / (1023.0 / analog_Value - 1)) / BETA + 1.0 / 298.15) - 273.15;
quantize_temperture(celsius);
for( int num = 0; num < LED_COUNT; num++ )
{
if (led_ON_OFF[num] == HIGH) {
digitalWrite(ledArray[num], HIGH); // Turn ON
} else {
digitalWrite(ledArray[num], LOW); // Turn OFF
}
led_ON_OFF[num] == LOW;
}
delay(10);
}
void quantize_temperture(float celsius)
{
for( int arr = 0; arr < LED_COUNT; arr++ )
{
if( (celsius >= quantize_temp[arr] )&& (celsius < quantize_temp[arr+1]))
{
led_ON_OFF[arr] = HIGH;
}
else{
led_ON_OFF[arr] = LOW;
}
if( celsius >= quantize_temp[10])
led_ON_OFF[9] = HIGH;
}
}