arduino - FastLed 库使用 CRGB 作为属性
问题描述
我正在开发一个可寻址 LED 灯条的程序。它正在工作,此时我正在努力使我的代码更好。我有 3 个 LED 灯条,我做了一个所有三个都必须做的功能。在函数中我想指定哪一个需要更新,所以我使用了属性,但这似乎不起作用。我在 FastLed 文档中找不到这个。
//Number of leds powered
int led_state_1 = 0;
int led_state_2 = 0;
int led_state_3 = 0;
// This is an array of leds. One item for each led in your strip.
CRGB leds1[NUM_LEDS];
CRGB leds2[NUM_LEDS];
CRGB leds3[NUM_LEDS];
void CheckAndUpdateLed(CRGB LedArray, int led_state){
resetLedStrip(LedArray);
for(int whiteLed = 0; whiteLed < led_state; whiteLed = whiteLed + 1) {
// Turn our current led on to white, then show the leds
LedArray[whiteLed] = CRGB::White;
// Show the leds (only one of which is set to white, from above)
FastLED.show();
}
}
当我将 LedArray 更改为 leds1 时,它正在工作。我将该函数称为 CheckAndUpdateLed(leds1, led_state_1);
解决方案
我认为我的问题有点不清楚,对此感到抱歉。我想出了另一种方法。我不是用 1 个 LED 灯条检查它们,而是在同一个功能中检查它们。
#define NUM_STRIPS 3
#define NUM_LEDS_PER_STRIP 15
CRGB leds[NUM_STRIPS][NUM_LEDS_PER_STRIP];
int led_states[] = {0, 0, 0};
void CheckAndUpdateLeds(){
//resets the leds to black
resetLedStrips();
// This outer loop will go over each LED strip, one at a time
for(int x = 0; x < NUM_STRIPS; x++) {
// This inner loop will go over each led in the current strip, one at a time till the amount of light is the same as in the led_state
for(int led = 0; led < led_states[x]; led = led + 1) {
// Turn our current led on to white, then show the leds
leds[x][led] = CRGB::White;
FastLED.show();
}
}
}
void resetLedStrips(){
for(int x = 0; x < NUM_STRIPS; x++) {
for(int led = 0; led < NUM_LEDS_PER_STRIP; led = led + 1) {
leds[x][led] = CRGB::Black;
}
}
}
推荐阅读
- hyperledger-fabric - 我想构建一个 Hyperledger Fabric 网络,在多个组织的多个主机上运行
- java - 如何将临时授予的 ContentProvider 权限传递给 Service?
- c# - 登录网站并使用 Html Agility Pack 设置用户代理
- swift - 如何使用枚举创建计算函数?
- docker - Dockerfile 的第一行 - 什么操作系统?
- authorization - 为动态角色和属性建模 XACML 策略
- kubernetes - Traefik 入口不适用于集群 IP
- ionic-framework - 如何将我的 Ionic 4 应用程序升级到最新的 Ionic 版本?
- css - 如何使用来自其父级的 ::after 的通用兄弟选择器
- r - 使用 NA 删除行和列但保留 R 中的值