/*
=== Task 4 - Potentiometer Dimmer With a Low-Brightness Warning ===
Author: Roberto Palozzo
===================================================================
*/
const int potPin = 1; // potentiometer wiper connected to GPIO 1
const int ledPin = 9; // dimmable LED connected to GPIO 9
const int warningPin = 10; // warning LED connected to GPIO 10
void setup() {
pinMode(ledPin, OUTPUT); // set the dimmable LED pin as an output
pinMode(warningPin, OUTPUT); // set the warning LED pin as an output
Serial.begin(115200);
}
void loop() {
int potValue = analogRead(potPin); // read the potentiometer (0 to 4095)
int brightness = map(potValue, 0, 4095, 0, 255); // convert the reading to PWM range (0 to 255)
analogWrite(ledPin, brightness); // set the dimmable LED's brightness
// turn on the warning LED whenever the brightness is too low to be useful
if (brightness < 25) {
digitalWrite(warningPin, HIGH);
}
else {
digitalWrite(warningPin, LOW);
}
Serial.println(potValue); // raw potentiometer reading
Serial.println(brightness); // mapped PWM value
delay(100); // small pause: keeps the dimmer smooth while the Serial Monitor stays readable
}