// 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};
void setup() {Serial.begin(115200);
// Set all LED pins as OUTPUT
// We loop through the ledPins array to set each one
for (int i = 0; i < ledCount; i++) {
pinMode(ledPins[i], OUTPUT);
}
// initialize LEDs and sensor pin
}
void loop() {
// 1. Read the analog sensor value
// This gives a value from 0 to 1023 on the Arduino Uno
int sensorVal = analogRead(sensorPin);
// 2. Convert the analog value (0-1023) to a voltage (0-5V)
// We use 5.0 and 1023.0 to force floating-point math
float voltage = sensorVal * (5.0 / 1023.0);
// 3. Convert the voltage to temperature in Celsius
// For the TMP36 sensor: (voltage - 0.5V offset) * 100 degrees/Volt
float tempC = (voltage - 0.5) * 100.0;
// 4. Print the current temperature to the Serial Monitor
Serial.print("Temperature: ");
Serial.print(tempC);
Serial.println(" C");
// 5. Map the temperature to the number of LEDs (0-10)
// First, we constrain the temperature to our defined min/max range.
// If tempC is 19, constrainedTempC will be 20.
// If tempC is 40, constrainedTempC will be 35.
float constrainedTempC = constrain(tempC, minTempC, maxTempC);
// Now, map the constrained temperature to the 0-10 LED level.
// The map() function works with 'long' integers.
// We multiply by 100 to preserve decimal precision during the mapping.
int ledLevel = map(constrainedTempC * 100, minTempC * 100, maxTempC * 100, 0, ledCount);
/*
// --- Alternative (and simpler) if/else logic ---
// This logic also perfectly matches the README requirements.
int ledLevel;
if (tempC <= minTempC) {
ledLevel = 0;
} else if (tempC >= maxTempC) {
ledLevel = 10;
} else {
// We are in the active range (20...35)
// Map the temperature to a level from 0-10
// We multiply by 100 to maintain precision for map()
ledLevel = map(tempC * 100, minTempC * 100, maxTempC * 100, 0, 10);
}
// You can use this block instead of the constrain/map block above.
*/
// Print the calculated LED level for debugging
Serial.print("LED Level (0-10): ");
Serial.println(ledLevel);
// 6. Light up the corresponding number of LEDs
for (int i = 0; i < ledCount; i++) {
if (i < ledLevel) {
digitalWrite(ledPins[i], HIGH);
} else {
// Otherwise, turn it off
digitalWrite(ledPins[i], LOW);
}
delay(10); // this speeds up the simulation
}