// Source: chapter_03.OMeter.ino
// Description: This program controls the blinking LEDs based on temperature readings from a sensor.
// Programmed by: Douglas Ferreira / February 05, 2024
const int tempSensorPin = A0;
const float baseTemperature = 20.0;
void setup() {
Serial.begin(9600); // Start serial communication at 9600 bps
// Initialize LED pins as outputs and set them to LOW (off)
for (int ledPin = 2; ledPin < 5; ledPin++) {
pinMode(ledPin, OUTPUT);
digitalWrite(ledPin, LOW);
}
}
void loop() {
// Read the value from the temperature sensor
int sensorValue = analogRead(tempSensorPin);
Serial.print("Sensor Value: ");
Serial.print(sensorValue);
// Convert the ADC reading to voltage
float voltage = (sensorValue / 1024.0) * 5.0;
Serial.print(", Volts: ");
Serial.print(voltage);
// Convert the voltage to temperature in degrees Celsius
float temperatureC = (voltage - 0.5) * 100;
Serial.print(", degrees C: ");
Serial.println(temperatureC);
// Control LED states based on temperature readings
if (temperatureC < baseTemperature) {
// If temperature is below the baseline, turn all LEDs off
digitalWrite(2, LOW);
digitalWrite(3, LOW);
digitalWrite(4, LOW);
} else if (temperatureC >= baseTemperature + 2 && temperatureC < baseTemperature + 4) {
// Turn on the first LED if temperature is in the first range above the baseline
digitalWrite(2, HIGH);
digitalWrite(3, LOW);
digitalWrite(4, LOW);
} else if (temperatureC >= baseTemperature + 4 && temperatureC < baseTemperature + 6) {
// Turn on the first two LEDs if temperature is in the second range above the baseline
digitalWrite(2, HIGH);
digitalWrite(3, HIGH);
digitalWrite(4, LOW);
} else if (temperatureC >= baseTemperature + 6) {
// Turn on all LEDs if temperature is above the second range
digitalWrite(2, HIGH);
digitalWrite(3, HIGH);
digitalWrite(4, HIGH);
}
delay(1);
}