I'm using ws2812b 2x2 LEDs and have it switching between green and red, but only one of my LEDs on my 2x2 is lighting up.
I'm using the "blink" FastLED sketch with Data pin 3 on my Arduino Uno, define led 4.
void loop() {
// Turn the LED on, then pause
leds[0] = CRGB::Red;
FastLED.show();
delay(5000);
// Now turn the LED off, then pause
leds[0] = CRGB::Green;
FastLED.show();
delay(5000);
}
-
Sorry i'm a Total Rookie, my question is how come only 1 of my 4 led's is blinking, did i miss something in the code.Darcy Billings– Darcy Billings2020年10月27日 03:29:38 +00:00Commented Oct 27, 2020 at 3:29
-
your code blinks only one led ... have a close look at your code ... see if you can figure out how to light a different led ... also have a look at the example code in the arduino IDEjsotola– jsotola2020年10月27日 06:49:04 +00:00Commented Oct 27, 2020 at 6:49
2 Answers 2
For changing the led color or making any other led to glow you have to put the led number instead of leds[led number]
in this case so your code will basically look like.
void loop() {
// Turn the LED on, then pause
leds[0] = CRGB::Red;
leds[1] = CRGB::Red;
leds[2] = CRGB::Red;
leds[3] = CRGB::Red;
FastLED.show();
delay(5000);
// Now turn the LED off, then pause
leds[0] = CRGB::Green;
leds[1] = CRGB::Green;
leds[2] = CRGB::Green;
leds[3] = CRGB::Green;
FastLED.show();
delay(5000);
}
Hope this helps
-
1Thanks Guys, i assumed that my #define NUM_LEDS 4 would have made all 4 work but it is doing what i want it to now, thanks alot you guysDarcy Billings– Darcy Billings2020年10月27日 17:38:13 +00:00Commented Oct 27, 2020 at 17:38
This is an addition to the answer of xbox gamer (which I upvoted), to prevent much duplicated code.
For
loop
First we see, the 4 LEDs are made red or green (using green in the following case):
leds[0] = CRGB::Green;
leds[1] = CRGB::Green;
leds[2] = CRGB::Green;
leds[3] = CRGB::Green;
For this a for
loop can be used counting from 0 to (excluding) 4:
for (led = 0; led < 4; led++)
{
leds[led] = CRGB::Green;
}
Variant 1: Color argument
Next we see the first and last lines are almost identical. For this we can make a function, and change the variable items (like the color) with a variable passed to that function, which is called an argument:
First we create the loop
function:
void loop()
{
turnLEDs(CRGB::Red); // Turn LEDs on (i.e. red), then pause
turnLEDs(CRGB::Green); // Turn LEDs off (i.e. green), then pause
}
Then we need to make just a single function to change the color (place this before the loop
function. The type of the color is enum HTMLColorCode
, see http://fastled.io/docs/3.1/struct_c_r_g_b.html.
void turnLEDs(enum HTMLColorCode color)
{
for (led = 0; led < 4; led++)
{
leds[led] = color;
}
FastLED.show();
delay(5000);
}
Variant 2: Boolean argument
Another way, is to not pass the color, but pass ON or OFF, which is a boolean.
For this we can use the so-called ternary operator: a ? b : c
which means: if a
is true, use b
, otherwise c
. We get:
void turnLEDs(bool enable)
{
for (led = 0; led < 4; led++)
{
leds[led] = enable ? CRGB::Red : CRGB::Green;
}
FastLED.show();
delay(5000);
}
void loop()
{
turnLEDs(true);
turnLEDs(false);
}