#include <Arduino.h>

// 硬件定义

const uint8_t ledPins[] = {5, 6, 7, 8};  // 4路LED引脚

const uint8_t ledCh[]   = {1, 2, 3, 4};  // 对应4个LEDC通道

const uint8_t ledNum    = sizeof(ledPins) / sizeof(ledPins[0]);

// PWM参数

#define PWM_FREQ     5000

#define PWM_RES      8   // 0~255亮度

#define STEP_DELAY   8   // 步进延时,越小速度越快

// PWM初始化

void pwmInit()

{

  for(uint8_t i = 0; i < ledNum; i++)

  {

    ledcSetup(ledCh[i], PWM_FREQ, PWM_RES);

    ledcAttachPin(ledPins[i], ledCh[i]);

    ledcWrite(ledCh[i], 0); // 初始全灭

  }

}

// 单路完整呼吸:渐亮→渐暗

void singleBreathe(uint8_t ch)

{

  // 渐亮

  for(int bri = 0; bri <= 255; bri++)

  {

    ledcWrite(ch, bri);

    delay(STEP_DELAY);

  }

  // 渐暗

  for(int bri = 255; bri >= 0; bri--)

  {

    ledcWrite(ch, bri);

    delay(STEP_DELAY);

  }

}

// 跑马逻辑:5→6→7→8依次完整呼吸一轮

void horseRun()

{

  for(uint8_t i = 0; i < ledNum; i++)

  {

    singleBreathe(ledCh[i]);

  }

}

void setup()

{

  pwmInit();

}

void loop()

{

  horseRun();

}

Logo

智能硬件社区聚焦AI智能硬件技术生态,汇聚嵌入式AI、物联网硬件开发者,打造交流分享平台,同步全国赛事资讯、开展 OPC 核心人才招募,助力技术落地与开发者成长。

更多推荐