#define LED_PIN 5 // Pin connected to the LED (change as needed)
int brightness = 0; // Initial brightness level (0 to 255)
int fadeAmount = 10; // Amount by which brightness will change per loop
unsigned long previousMillis = 0; // Stores the last time we updated the brightness
const long interval = 300; // Interval (in milliseconds) to change brightness
void setup() {
// Set the LED pin as output
pinMode(LED_PIN, OUTPUT);
}
void loop() {
// Get the current time
unsigned long currentMillis = millis();
// Check if it's time to update the brightness
if (currentMillis - previousMillis >= interval) {
// Save the last time the brightness was updated
previousMillis = currentMillis;
// Adjust the brightness
brightness = brightness + fadeAmount;
// If brightness exceeds 255 or falls below 0, reverse the fade direction
if (brightness <= 0 || brightness >= 255) {
fadeAmount = -fadeAmount; // Reverse the direction of brightness change
}
// Write the new brightness value to the LED (PWM control)
analogWrite(LED_PIN, brightness);
}
}