[英]Control RGB LEDs ON/OFF - A smarter way than using “if”? (Arduino, C++)
I want to control three RGB LEDs in color (eff. 9 LEDs in total) and whether to be on or off.我想控制三个 RGB LED 的颜色(总共 9 个 LED)以及是打开还是关闭。 I am using Arduino with PlatformIO and the FastLED library to turn them on using FastLED.show();
我正在使用带有 PlatformIO 和 FastLED 库的 Arduino 使用FastLED.show(); .
.
There are six buttons on my display: LED1 , LED2 , LED3 , RED , GREEN , BLUE (dual state buttons) which give me我的显示器上有六个按钮: LED1 、 LED2 、 LED3 、 RED 、 GREEN 、 BLUE (双 state 按钮)给我
true when pressed and按下时为真
false when released释放时为假
void bt0PopCallback(void *ptr){uint32_t dual_state; NexDSButton *btn = (NexDSButton *)ptr; bt0.getValue(&dual_state); if(dual_state){ LED1 = true; } else{ LED1 = false; } }
void bt1PopCallback(void *ptr){uint32_t dual_state; NexDSButton *btn = (NexDSButton *)ptr; bt1.getValue(&dual_state); if(dual_state){ LED2 = true; } else{ LED2 = false; } }
void bt2PopCallback(void *ptr){uint32_t dual_state; NexDSButton *btn = (NexDSButton *)ptr; bt2.getValue(&dual_state); if(dual_state){ LED3 = true; } else{ LED3 = false; } }
Eg: LED1 = true when LED1 button is pressed, LED1 = false when button is released.例如:按下 LED1 按钮时 LED1 = true ,松开按钮时LED1 = false 。
If LED1 = true and RED = true, this led shall turn red.如果 LED1 = true 且 RED = true,则此 LED 应变为红色。 When RED is released and GREEN is pressed the LED1 is now green.
当 RED 被释放并按下 GREEN 时,LED1 现在是绿色的。 If LED1 is released and LED2 is pressed LED1 goes off and LED2 turns on GREEN.
如果释放 LED1 并按下 LED2,则 LED1 熄灭,LED2 亮起绿色。
I came up with this one:我想出了这个:
if(BLUE == true){BLUEV = 255;} else { BLUEV = 0;}
if(RED == true){REDV = 255;} else { REDV = 0;}
if(GREEN == true){GREENV = 255;} else { GREENV = 0;}
if(LED1 == true){leds[0] = CRGB(REDV, GREENV, BLUEV); } else { leds[0] = CRGB(0, 0, 0); }
if(LED2 == true){leds[1] = CRGB(REDV, GREENV, BLUEV); } else { leds[1] = CRGB(0, 0, 0); }
if(LED3 == true){leds[2] = CRGB(REDV, GREENV, BLUEV); } else { leds[2] = CRGB(0, 0, 0); }
FastLED.show();
but it's obviously imperfect, I reckon.但我认为它显然是不完美的。
Anyone has suggestions how to code it better?任何人都有如何更好地编码的建议? Maybe using a switch case?
也许使用开关盒?
Cheers!干杯!
For starters, you can assign dual_state
to the corresponding LED without if-else
in your callbacks.对于初学者,您可以在回调中不
if-else
将dual_state
分配给相应的 LED。
Here's an example:这是一个例子:
void bt0PopCallback( void* ptr )
{
uint32_t dual_state;
NexDSButton* btn = (NexDSButton*) ptr;
bt0.getValue( &dual_state );
LED1 = dual_state; // direct assignment
}
For other if-else
variants in your code for colors, you can use the ternary operator ?:
to clean things up like this:对于 colors 代码中的其他
if-else
变体,您可以使用三元运算符?:
进行如下清理:
BLUEV = BLUE ? 255 : 0;
REDV = RED ? 255 : 0;
GREENV = GREEN ? 255 : 0;
And, for setting the values of leds
array, use a for
loop.并且,要设置
leds
数组的值,请使用for
循环。 The LED1
, LED2
, and LED3
may be grouped using an std::array
to be used in the loop for the corresponding values of leds
. LED1
、 LED2
和LED3
可以使用std::array
进行分组,以用于循环中对应的leds
值。
Here's an example with std::array
:这是
std::array
的示例:
#include <array>
const std::array<decltype(LED1), NUM_LEDS> LED { LED1, LED2, LED3 };
for ( int i = 0; i < NUM_LEDS; ++i ) // loop to set the colors of LEDs
{
leds[i] = LED[i] ? CRGB(REDV, GREENV, BLUEV) : CRGB(0, 0, 0);
}
NUM_LEDS
represents the size of the leds
array ie: NUM_LEDS
代表leds
阵列的大小,即:
#define NUM_LEADS 3
CRGB leds[NUM_LEADS];
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.