#define LED_PIN A1
void setup() {
// Initialize LED pin as output
pinMode(LED_PIN, OUTPUT);
}
void loop() {
// Fade in
for (int brightness = 0; brightness <= 255; brightness++) {
setBrightness(LED_PIN, brightness);
delay(505); // Small delay to make the fading smooth
}
// Fade out
for (int brightness = 255; brightness >= 0; brightness--) {
setBrightness(LED_PIN, brightness);
delay(505); // Small delay to make the fading smooth
}
}
// A function to simulate analogWrite using software PWM
void setBrightness(int pin, int brightness) {
int onTime = brightness; // On time proportional to brightness
int offTime = 255 - brightness; // Off time inversely proportional
// Generate software PWM by turning the pin ON and OFF
digitalWrite(pin, HIGH);
delayMicroseconds(onTime); // LED ON for "onTime" microseconds
digitalWrite(pin, LOW);
delayMicroseconds(offTime); // LED OFF for "offTime" microseconds
}