You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何实现6路LED同步渐亮渐灭?附现有逐路渐变代码

Fixing 6-LED Synchronous Fading & Mode Switching

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 currentMode variable lets us toggle between the two fade styles. After one full cycle of a mode, we trigger shutOffAllLEDs() 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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.13 09:24:33