Beetle_ESP32_C3 驱动 WS2812-24炫彩灯环

2022-07-24640
AI 快速预览详细 收起
本文介绍了如何使用Beetle_ESP32_C3驱动WS2812-24炫彩灯环。首先列出了所需的材料清单,包括Beetle_ESP32_C3、WS2812-24 RGB LED Ring等。接着详细描述了连线步骤和代码编写方法。通过Adafruit_NeoPixel库控制灯环,实现了多种颜色和模式的灯光效果。适合STEM教育场景,初学者也能轻松上手。

材料清单

步骤1 参考下图连线:

project-image

务必注意:WS2812需要单独供电,电压为5V

步骤2 编写代码:

代码
// 调用Adafruit_NeoPixel模块
#include <Adafruit_NeoPixel.h>

// 设置灯珠数量
#define NUMPIXELS        24

// 设置SPI输出数据引脚
#define PIN_NEOPIXEL    6

// LED引脚设置
#define PIN_LED         10

// 初始化灯珠控制实例
Adafruit_NeoPixel pixels(NUMPIXELS, PIN_NEOPIXEL, NEO_GRB + NEO_KHZ800);

// 当前灯珠指向
int16_t idx = 0;

// 上一课灯珠指向
int16_t idx_prev = 0;

// 方向
int16_t direction = 0;

// 转换方向
int16_t type = 1;

// 启动设置
void setup() {
  // 调试串口速率设置
  Serial.begin(115200);
  pinMode(PIN_LED, OUTPUT);

  // 灯珠控制开始
  pixels.begin();

  // 设置亮度为20
  pixels.setBrightness(20);

  // 设置灯珠颜色,全部关闭
  pixels.fill(0x000000);

  delay(100);

  // 设置灯珠颜色
  pixels.fill(0xFF0000);

  delay(100);

  pixels.clear();

  if(direction==0) {
    // 当前灯珠指向
    idx = 0;
    
    // 上一课灯珠指向
    idx_prev = NUMPIXELS - 1;    
  } else {
    // 当前灯珠指向
    idx = 0;
    
    // 上一课灯珠指向
    idx_prev = 1;
  }
}


// 循环主体程序
void loop() {
  if (idx % 2 == 0) {
    // 点亮LED
    digitalWrite(PIN_LED, HIGH);
  } else {
    digitalWrite(PIN_LED, LOW);
  }

  // 设置灯珠颜色
  if(direction==0) {
    if (idx == 0) {
      idx_prev = NUMPIXELS - 1;
    } else {
      idx_prev = idx - 1;
    }
  } else {
    if (idx == NUMPIXELS - 1) {
        idx_prev = 0;
    } else {
      idx_prev = idx + 1;
    }    
  }

  // 串口输出
  Serial.print("type=");
  Serial.print(type);
  Serial.print(" direction=");
  Serial.print(direction);
  Serial.print(" index=");
  Serial.print(idx);
  Serial.print(" prev=");
  Serial.print(idx_prev);

  // 熄灭上一课灯珠
  pixels.setPixelColor(idx_prev, pixels.Color(0, 0, 0));

  // 点亮当前灯珠
  uint16_t c1 = idx % 3 == 0 ? (100 + random(100)) : (0 + random(50));
  uint16_t c2 = idx % 3 == 1 ? (100 + random(100)) : (0 + random(50));
  uint16_t c3 = idx % 3 == 2 ? (100 + random(100)) : (0 + random(50));
  Serial.print(" c1=");
  Serial.print(c1);
  Serial.print(" c2=");
  Serial.print(c2);
  Serial.print(" c3=");
  Serial.print(c3);
  Serial.println("");
  pixels.setPixelColor(idx, pixels.Color(c1, c2, c3));

  // 显示
  pixels.show();

  // 延时
  delay(50);

  if(direction==0) {
    idx++;
  
    if (idx >= NUMPIXELS) {
      if(type==0) {
        idx = 0;
      } else {
        idx = NUMPIXELS - 1;
        direction = 1;
      }
    }
  } else {
    idx--;
    if (idx<0) {
      if(type==0) {
        idx = NUMPIXELS - 1;
      } else {
        idx = 0;
        direction = 0;
      }
    }    
  }
}

步骤3 编译下载:

project-image

参考上图,选择好对应的配置选项

步骤4 实际效果:

project-image

创作许可协议

本项目采用 None(不开放任何权利,保留所有权利) 进行许可。

所需材料/产品

加载全部

    Beetle ESP32-C3 (RISC-V芯片)

    Beetle ESP32-C3 (RISC-V芯片)

    Beetle ESP32-C3 (RISC-V芯片)

    WS2812-24 RGB LED Ring

    WS2812-24 RGB LED Ring

    WS2812-24 RGB LED Ring

    面包板杜邦试验线(公公头)-65根

    面包板杜邦试验线(公公头)-65根

    面包板杜邦试验线(公公头)-65根

相关推荐

评论(0)
- 没有更多了 -

创作许可协议

本项目采用 None(不开放任何权利,保留所有权利) 进行许可。

相关推荐

所需材料/产品

Beetle ESP32-C3 (RISC-V芯片)

Beetle ESP32-C3 (RISC-V芯片)

Beetle ESP32-C3 (RISC-V芯片)

WS2812-24 RGB LED Ring

WS2812-24 RGB LED Ring

WS2812-24 RGB LED Ring

面包板杜邦试验线(公公头)-65根

面包板杜邦试验线(公公头)-65根

面包板杜邦试验线(公公头)-65根

加载全部