公告 欢迎您,我的朋友,目前开放免费注册,注册登录后查看更多内容!开通会员送积分,相应文档随便看,积分充值登录后个人中心内按需充值!

WS2812(R,G,B)三色灯切换源代码

WS2812三色灯代码(R,G,B)

一、WS2812三色灯显示代码

/*
* t_led.cpp — WS2812 彩灯 独立测试(自包含,可单独烧录)
* 烧录:platformio run -e led -t upload
* 现象:红→绿→蓝→白 循环,串口同步打印当前颜色
* 注意:PIN_WS2812=IO48 与摄像头 PCLK 共用,本测试不使用摄像头
* #include "esp32-hal-rgb-led.h" 引用 neopixelWrite
 */
#include <Arduino.h>
#include "pins.h"

void setup()
{
  Serial.begin(115200);
  Serial.setTxTimeoutMs(0); // 关闭监视器后不阻塞
  delay(1000);
  Serial.println("=== LED (WS2812) 独立测试 ===");
  Serial.printf("WS2812 引脚 = IO%d\n", PIN_WS2812);
}

void loop()
{
  static const uint8_t seq[4][3] = {
      {255, 0, 0}, {0, 255, 0}, {0, 0, 255}, {255, 255, 255}};
  static int i = 0;
  neopixelWrite(PIN_WS2812, seq[i][0], seq[i][1], seq[i][2]);
  Serial.printf("LED: R=%d G=%d B=%d\n", seq[i][0], seq[i][1], seq[i][2]);
  i = (i + 1) % 4;
  delay(1000);
  if (i == 0)
  {
    neopixelWrite(PIN_WS2812, 0, 0, 0); // 一轮结束灭一下
    delay(300);
  }
}

该文档需要「VIP会员」身份

请先登录后继续阅读。