Help! HOW TO Control Multiple LEDs at once

Hi,

I am now doing a project involving controlling 168 WS2812B LEDs by sending an ASCII char one at a time. Which means i can only on one LED at once. This making me take a long time to control all LEDs.
Could i know any method for me to send a series of ASCII char to control all LED at once? (I am sending the ASCII char using LabVIEW VI)
Please help! Thanks a lot. I have attached my code here.

RGB_LED_V1.0.ino (4.19 KB)

You need to send out 168 x 3 bytes = 504 bytes of data to update the string.
Why not use the Adafruit library for sending the data out from an Arduino?
Have Arduino interacth with Labview (on the PC?) to receive the data, and then send it to the WS2812Bs.

Example of driving 144 LEDs:

Hi,

Actually i am using the Adafruit for sending the data to Arduino. However, i have to modified the example to suit my application.

My application is, from the 168 LEDs, every four LEDs is a set. Means in a set, the first LED must be red color, second LED must be green, third LED must be blue and last led must be white.
When user send a command(in my code is an unsigned integer) from labVIEW, let say "1". Arduino receive "1" and the first LED, which is supposed to be red color will ON.
This will be slow if my Arduino can only receive one command at a time in case i need to ON more than one LED. That's y i hope to send more than one integer at once to on all LEDs that i wanted them to on.

For example, i want on LED 1,6,100 and 400. I can simply send 1, 6, 100 and 400 to my arduino and it will ON all the four at the same without needing me to send 1 then it ON LED 1, follow by sending 6 then it only ON LED 6 and so on..

I know it's complicated but hopefully you can understand my poor explanation and give me some suggestion. Thank you.

Keep a byte array[] in Arduino RAM, and update the appropriate bytes when received from the host... then send the whole array to the WS2812s even when only one byte has been changed.

hint: only keep the required 42 states in memory, then send each byte value four times as needed to illuminate the associated block of 4 LEDs for that incoming byte.

Hi could you please elaborate more on your hints? I am sorry i am still a beginner for Arduino as well as C programming. Thank you.

I would start by dropping the useless color arrays. This now takes 336 bytes of memory! And all that's in there is easy to calculate...

if( (pin & 0x07) = 0){ //for red
}
if( (pin & 0x07) = 1){ //for green
}
if( (pin & 0x07) = 2){ //for blue
}
if( (pin & 0x07) = 3){ //for white
}

Next, clean up unused variable. ser and inData are not used. (and ser is just a bad name and inData a evil String object...)

Then, remove d (which is simple x-1...) and give x a proper name.

And to be clear, do you want to have multiple leds on at the same time?

Then simply make an array to hold which leds are on. And to not make an array of 168 I would make an array of 42 (although it can be done with halve of it) that holds for each set of red green blue white in a bitwise manner if it's on. So
0b0000 for non on
0b0001 for red on
0b0101 for blue and red on
etc

An then just use that to drive the leds.

septillion:
I would start by dropping the useless color arrays. This now takes 336 bytes of memory! And all that's in there is easy to calculate...

if( (pin & 0x07) = 0){ //for red

}
if( (pin & 0x07) = 1){ //for green
}
if( (pin & 0x07) = 2){ //for blue
}
if( (pin & 0x07) = 3){ //for white
}




Next, clean up unused variable. ser and inData are not used. (and ser is just a bad name and inData a evil String object...)

Then, remove d (which is simple x-1...) and give x a proper name.

And to be clear, do you want to have multiple leds on at the same time? 

Then simply make an array to hold which leds are on. And to not make an array of 168 I would make an array of 42 (although it can be done with halve of it) that holds for each set of red green blue white in a bitwise manner if it's on. So
0b0000 for non on
0b0001 for red on
0b0101 for blue and red on
etc

An then just use that to drive the leds.

Hi septilion,

Could i know what does "pin" represented in your code? Is it a variable?

It's what you now call d in your code and look up in huge arrays.

Let's say d is 34. Looking at your arrays that's a blue led.

But if we look at how 34 is in binary it is 0b00100010. If we look at the last 2 bits we mask them with 0x03 = 0b00000011 (oops, wrote 0x07...) 0b00100010 && 0b00000011 leaves us with the last two bits: 0b00000010 which is 2. So if the outcome is 2 we know we want a blue led :wink: We can do the same for the other colors. 0 => red, 1 => green, 2=> blue, 3 => white. Now you don't need a lookup table.

Okay, I gave you some answers, can you now answer my questions?

septillion:
And to be clear, do you want to have multiple leds on at the same time?

And only turn off all led's when you send 199?

septillion:
It's what you now call d in your code and look up in huge arrays.

Let's say d is 34. Looking at your arrays that's a blue led.

But if we look at how 34 is in binary it is 0b00100010. If we look at the last 2 bits we mask them with 0x03 = 0b00000011 (oops, wrote 0x07...) 0b00100010 && 0b00000011 leaves us with the last two bits: 0b00000010 which is 2. So if the outcome is 2 we know we want a blue led :wink: We can do the same for the other colors. 0 => red, 1 => green, 2=> blue, 3 => white. Now you don't need a lookup table.

Okay, I gave you some answers, can you now answer my questions?And only turn off all led's when you send 199?

Hi septilion,

Thank you for your explanation. I am now much more clearer!

Yes i need to on multiple LEDs at once and OFF all LEDs when i get an input from user (not necessary must be 199). That's what i am now struggling for. :frowning:

Then instead off setting all leds when you receive serial, just update the led you want (aka received) and then update the pixels.

septillion:
Then instead off setting all leds when you receive serial, just update the led you want (aka received) and then update the pixels.

Hi septillion,

If i am controlling and updating my LEDs one by one. That will be very slow and not really efficient. So i am thinking a way of sending a series of byte to on all LEDs that i want them to on. Hopefully you can share some ideas if you have any better suggestion.. Thanks.

negotxj:
That will be very slow and not really efficient.

Then that's simple an error of how you send the data... That's not going to be faster no matter how you set the leds.

One thing that can speed it up (or actually two possible implementations)

  1. Don't update (aka .show() ) after each led update but just limit it to let's say 10 times a second.
  2. Don't update after each led but make a command for that as well.