Arduino程式碼
const int led = 9; // PWM pins: ~3, ~5, ~6, ~9, ~10, ~11
int brightness = 0; // LED brightness variable
int fadeAmount = 5; // Brightness change variable
void setup() {
pinMode(led, OUTPUT); // Set led pin as OUTPUT
}
void loop() {
analogWrite(led, brightness); // PWM output
brightness += fadeAmount; // Adjust brightness
// Reverse the direction of fading at the extremes
if (brightness <= 0 || brightness >= 255) {
fadeAmount = -fadeAmount;
}
delay(30); // Delay to improve breathing effect
}