Go Down

Topic: LED strip (WS2812B/5050) programming help (Read 2700 times) previous topic - next topic

immike

Jun 21, 2014, 02:26 pm Last Edit: Jun 22, 2014, 12:03 pm by immike Reason: 1
I am trying to work out how to do the following:

I have a circle of 64 LEDs  on a WS2812B strip.

I want to work out an easy way to address a 'block' of LEDs for the purposes of changing colour/brightness.

The attached image shows how I would like to 'block address' the groups of LEDs.

If you think of it as a primitive 'Simon' game :)

My programming is basic, but I am getting there... using the FASTLed library:

Code: [Select]

#define Area_A 1
#define Area_B 17
#define Area_C 33
#define Area_D 49
leds[Area_A] CRGB::Blue; leds[Area_A+1] CRGB::Blue;leds[Area_A+2] CRGB::Blue;...leds[Area_A+15] CRGB::Blue;
leds[Area_B] CRGB::Yellow; leds[Area_B+1] CRGB::Yellow;leds[Area_B+2] CRGB::Yellow;...leds[Area_B+15] CRGB::Yellow;


But it's really clunky...

Any help gratefully received!

PaulRB

#1
Jun 21, 2014, 02:52 pm Last Edit: Jun 21, 2014, 03:02 pm by PaulRB Reason: 1
Hi,

Use a function containing a for-loop like this:

Code: [Select]

void setArea (int a, CRGB c) {
 for (int n = a; n <= a+15; n++) {
   leds [n] = c;
 }
}

...
setArea (Area_A, CRGB::Yellow);
...



immike

#2
Jun 22, 2014, 06:54 am Last Edit: Jun 22, 2014, 12:01 pm by immike Reason: 1
That's great! Thanks Paul, it works perfectly.

Just in a couple of hours, thanks to your example, I have learned so much about controlling the Arduino and using the FastLED library. Thanks so much.

FWIW, this is my first piece of code that begins to give me what I want (You can see I'm a big fan of variables :) ):

And now I am able to create a display for any sized string with any sized fixed area. Now to get into the hard bits of code :) The ones where things have to move and be read and decisions made...

Code: [Select]
   
   #include <FastLED.h>
   
   #define DATA_PIN 6 // Arduino data pin > WS2812B
   #define TYPE WS2812B // LED String type according to FastLED
   #define ORDER GRB // Define colour display order - WS strings are GRB
   
   #define NAV 5 //How many pixels for NAV area
   #define Area_Size 20 //How many pixels per area total (including NAV)
   #define NUM_LEDS Area_Size*4 // size of LED String
   
   #define Area_NA 0 //Start of Nav Area A
   #define Area_NB Area_Size*1 //Start of Nav Area B
   #define Area_NC Area_Size*2 //Start of Nav Area C
   #define Area_ND Area_Size*3 //Start of Nav Area D
   #define Area_A Area_NA+NAV //Start of Area A
   #define Area_B Area_NB+NAV //Start of Area B
   #define Area_C Area_NC+NAV //Start of Area C
   #define Area_D Area_ND+NAV //Start of Area D
   
   CRGB leds[NUM_LEDS];
   
   
 void setup() {
   FastLED.addLeds<TYPE, DATA_PIN, ORDER>(leds, NUM_LEDS); // initialise string
   FastLED.showColor(CRGB::Black);  // clear LEDs
   FastLED.setBrightness(10);}

 void setNavArea (int a, CRGB c) { // set up fixed NAV area
   for (int n = a; n <= a+(NAV-1); n++) {
   leds [n] = c;
   }
 
 }
  // set up variable area
 void setArea (int a, CRGB c) {
   for (int n = a; n <= a+(Area_Size-NAV-1); n++) {
   leds [n] = c;
   }
 
 }
 
 void loop() {
     
   {
   
   // initialise NAV areas
   setNavArea (Area_NA, CRGB::Red);
   setNavArea (Area_ND, CRGB::Red);
   setNavArea (Area_NB, CRGB::Blue);
   setNavArea (Area_NC, CRGB::Blue);
   
   // initialise variable areas
   setArea (Area_A, CRGB::Orange);
   setArea (Area_B, CRGB::White);
   setArea (Area_C, CRGB::White);
   setArea (Area_D, CRGB::Orange);
   
   FastLED.show() ;} //display LEDs
   
 }

 
 

PaulRB

When posting code on the forum you should use Code tags. You have been using Quote tags. Look at my list compared to yours to see the difference. Its the # button to the left of the quote button when composing a message. The Code tags preserve formatting so the code looks like it does in the Arduino IDE. Quote tags can make code difficult to read, especially with C/C++ where brackets and semicolons can get turned into smileys! ;)

immike

Thanks Paul :)

Too many years yacking on fora and not enough time coding, I now see...

And thanks again for your help -I've had a most productive day!

Go Up