如何实现6路LED同步渐亮渐灭?附现有逐路渐变代码
Hey there! I see you’ve got the sequential fading working perfectly, but hit a snag getting all 6 LEDs to fade in and out together. Let’s break down what needs to change and get you sorted.
First, let’s unpack the issue with your current code
Your existing loop handles LEDs one at a time: it fades LED 8 up and down, waits a second, then moves to LED 9, and so on. For synchronous fading, we need to update all LEDs to the same brightness level in each step of the fade, instead of processing them sequentially.
Here’s the adjusted code with both modes (sequential + synchronous)
We’ll add a mode tracker, a helper function to turn off all LEDs (required for clean mode switching), and implement the synchronous fade logic properly:
// Define our LED pin range (8 to 13, 6 pins total) const int FIRST_LED = 8; const int LAST_LED = 13; // Mode variable: 0 = sequential fade, 1 = synchronous fade int currentMode = 0; void setup() { // Initialize all LED pins as outputs for (int pin = FIRST_LED; pin <= LAST_LED; pin++) { pinMode(pin, OUTPUT); } } // Helper function to turn off all LEDs (critical for mode switching) void shutOffAllLEDs() { for (int pin = FIRST_LED; pin <= LAST_LED; pin++) { analogWrite(pin, 0); } delay(500); // Short delay to ensure all LEDs are fully off before switching } void loop() { if (currentMode == 0) { // Your original sequential fade logic (unchanged, since it works!) for (int pin = FIRST_LED; pin <= LAST_LED; pin++) { // Fade up for (int brightness = 0; brightness < 256; brightness++) { analogWrite(pin, brightness); delay(2); } // Fade down for (int brightness = 255; brightness >= 0; brightness--) { analogWrite(pin, brightness); delay(2); } delay(1000); } // Switch to synchronous mode after full sequential cycle shutOffAllLEDs(); currentMode = 1; } else { // Synchronous fade: update all LEDs at the same brightness level // Fade up all LEDs together for (int brightness = 0; brightness < 256; brightness++) { for (int pin = FIRST_LED; pin <= LAST_LED; pin++) { analogWrite(pin, brightness); } delay(2); } // Fade down all LEDs together for (int brightness = 255; brightness >= 0; brightness--) { for (int pin = FIRST_LED; pin <= LAST_LED; pin++) { analogWrite(pin, brightness); } delay(2); } delay(1000); // Switch back to sequential mode after full synchronous cycle shutOffAllLEDs(); currentMode = 0; } }
Key changes explained:
- Mode control: The
currentModevariable lets us toggle between the two fade styles. After one full cycle of a mode, we triggershutOffAllLEDs()to clear the board before switching—exactly what your requirement specifies. - Synchronous fade logic: Instead of looping through LEDs first, we loop through brightness values. For each brightness level, we set every LED to that value at the same time—this is what makes them fade in lockstep.
- Clean mode switching: The helper function ensures no LEDs are left lit between mode changes, so transitions look smooth and meet your requirement.
If you want to switch modes manually (like with a button) instead of automatically cycling, just modify the mode switching logic to read a digital input instead of toggling after a cycle—feel free to ask for help with that if you need it!
内容的提问来源于stack exchange,提问作者Moe

