天成高科(深圳)有限公司欢迎您! 全国服务热线:4008-320-328
中文 | English

LED灯珠知识

相关文章

灯珠行业动态

LED幻彩灯编程

发布时间:2024-05-29 11:50:37

Arduino 代码

```c++

LED幻彩灯编程

#include

#define LED_COUNT 10

#define LED_PIN 6

Adafruit_NeoPixel strip = Adafruit_NeoPixel(LED_COUNT, LED_PIN, NEO_GRB + NEO_KHZ800);

const uint32_t colors[] = {

strip.Color(255, 0, 0), // Red

strip.Color( 0, 255, 0), // Green

strip.Color( 0, 0, 255), // Blue

strip.Color(255, 255, 0), // Yellow

strip.Color( 0, 255, 255), // Cyan

strip.Color(255, 0, 255), // Magenta

strip.Color(255, 255, 255) // White

};

unsigned long lastMillis = 0;

int colorIndex = 0;

void setup() {

strip.begin();

strip.show();

}

void loop() {

unsigned long currentMillis = millis();

if (currentMillis - lastMillis > 500) {

colorIndex = (colorIndex + 1) % (sizeof(colors) / sizeof(colors[0]));

strip.fill(colors[colorIndex], 0, LED_COUNT);

strip.show();

lastMillis = currentMillis;

}

}

```

说明:

  • NeoPixel 库:此代码使用 Adafruit 的 NeoPixel 库,该库用于控制 WS2812B LED。
  • LED 数量:定义为 10,可根据您的 LED 数量进行调整。
  • LED 引脚:定义为 6,可根据您的 Arduino 板的 LED 引脚进行调整。
  • 颜色数组:包含可循环浏览的不同颜色。
  • lastMillis:用于跟踪上次颜色更改的时间。
  • colorIndex:用于跟踪当前显示的颜色索引。

如何使用:

1. 导入 Adafruit_NeoPixel 库。

2. 定义您的 LED 数量和引脚。

3. 创建一个 Adafruit_NeoPixel 对象来控制 LED。

4. 定义一个颜色数组,其中包含您希望 LED 显示的不同颜色。

5. 初始化串行监视器(可选)以打印调试信息。

6. 在主循环中,使用时间来循环浏览颜色并更新 LED。

此代码将创建幻彩灯效果,其中 LED 会缓慢地循环显示不同的颜色。

二维码
关注我们
友情链接: 5050RGB灯珠
粤ICP备13010073号 Copyright 2012-2022 天成高科(深圳)有限公司 版权所有
 
QQ在线咨询
全国免费咨询热线
4008-320-328