Page 1 of 1
Forum

Welcome to the Tweaking4All community forums!
When participating, please keep the Forum Rules in mind!

Topics for particular software or systems: Start your topic link with the name of the application or system.
For example “MacOS X – Your question“, or “MS Word – Your Tip or Trick“.

Please note that switching to another language when reading a post will not bring you to the same post, in Dutch, as there is no translation for that post!



LED strip effect: c...
 
Share:
Notifications
Clear all

[Solved] LED strip effect: check gif

6 Posts
2 Users
0 Reactions
1,441 Views
(@Anonymous)
Joined: 1 second ago
Posts: 0
Topic starter  

 

Hello I would like to achieve this loop.

but I dont know how I can skip some LEDs, can someone help pls?

I do not have preferences to use NeoPixel or FastLED.


   
ReplyQuote
 Hans
(@hans)
Famed Member Admin
Joined: 11 years ago
Posts: 2741
 

Hey there! Happy New Year!

Ehm ... what is your starting point? Did you try any code already?
I'm not quite sure where you're getting stuck ...

Both NeoPixel and FastLED (recommended) use an array to hold all LED colors.

So if you fill the entire array with black, all leds will become black once you call the "show" command.

If you only set LED #7 to green, and you call the show command again, then all LEDs will still be black, except for #7 which will be green.
If you after that set only LED #8 to red, and you call the show command again, then all LEDs will still be black, except for #7 which will be green and #8 will be red.

Point being: a LED color will only change if you set that particular LED to a color, AND you call the show function (which updates all LEDs).


   
ReplyQuote
(@Anonymous)
Joined: 1 second ago
Posts: 0
Topic starter  

yes I was trying to do something like this, but it was weird, so I decided to ask for help, I think I'm going the wrong way.

void test2(uint32_t color, unsigned long duration, uint32_t off, int ledLightAmount) {
 
      for (int i = 0; i<=ledLightAmount; i++){
       strip.setPixelColor(i, green);
       strip.show();
      }
      delay(1000);
      
      for (int j = 9; j<=(9+ledLightAmount); j++){
        strip.setPixelColor(j, green);
        strip.show();
      }
      
      delay(1000);
       for (int k = 16; k<=(16+ledLightAmount); k++){
        strip.setPixelColor(k, green);
        strip.show();
      }
      delay(1000);
      
}


void loop (){
test2(green, 1000, off,4);
}

   
ReplyQuote
(@Anonymous)
Joined: 1 second ago
Posts: 0
Topic starter  

I kept working with my function and now I got all LEDs I need to light, but I still stuck on blink function. Your hit that all LEDs are stored as array was good for me. 

I created an array with the LEDs I need to light. 

int ledsUsed[25] = {0,1,2,3,4,11,12,13,14,15,22,23,24,25,26,33,34,35,36,37,44,45,46,47,48};

 

then I modified my funtion and set the leds to green depends on my array size and specified leds there:

void test2(uint32_t color, unsigned long duration, uint32_t off, int ledsUsed[] , int ledsUsedSize) {
 
for (int i=0; i<ledsUsedSize; i++){
   strip.setPixelColor(ledsUsed[i], green);
       strip.show();
 }
}

 

Funtion call:

test2(green, 1000, off, ledsUsed, 25);

 

 

Now I need to make the first 5 leds show up, 1 second later the secont 5 and so on. How to to that? do I have to create for each block of 5 leds its own array? Or maybe I can use for loop to go on, because the space between the 5 light leds is always 7 leds that are off.


   
ReplyQuote
 Hans
(@hans)
Famed Member Admin
Joined: 11 years ago
Posts: 2741
 

Actually, the idea was this: FastLED (and NeoPixel) use an array to represent all LEDs.
I assume you're already doing this, so I'm kind-a adding this explanation for others that may be reading this.

I'm assuming FastLED here, even though you seem to be using NeoPixel.
FastLED is (in my opinion) a much better, faster and more mature LED library (documentation: here and here).

So for a strip of 60 LEDs, connected to pin 5, using FastLED, we start with something like this:

#include "FastLED.h"

#define NUM_LEDS 60
CRGB leds[NUM_LEDS];
#define PIN 5

....

void setup()
{
  ...

  FastLED.addLeds<WS2811, PIN, GRB>(leds, NUM_LEDS).setCorrection( TypicalLEDStrip );

  ...
}

 

The array is called "leds" and each element in the array are of the type "CRGB" (a color).
The function call in the loop initializes the strip, so FastLED knows how to work with it.

Next we set a color with one of these methods - where we set element "i" from the CRGB array called "leds":

// set individual R, G, and B fields, the classic way:

    leds[i].r = 255; 
    leds[i].g = 0; 
    leds[i].b = 0;

// set RGB from a single (hex) color code
    leds[i] = 0xFF0000;  // red

// set RGB from a standard named web/HTML color code
    leds[i] = CRGB::Red;

// set RGB using 'setRGB' and three values at once
    leds[i].setRGB( 255, 0, 0);

// copy RGB color from another led (v2)
    leds[i] = leds[j];

 

Now with that we have only set the color, but have not yet asked FastLED to apply the change. This is done with the show() function:

FastLED.show();

 

In the next reply I'll look at how to change the LEDs.


   
ReplyQuote
 Hans
(@hans)
Famed Member Admin
Joined: 11 years ago
Posts: 2741
 

It is a little challenging (and I would do some experimenting).
We should probably first look at what exactly happens in your animation. I've just uploaded your animation as a video in your initial post.

So when looking at your animation, we see these steps:

  1. Make LEDs 0, 1, 2, 3, 4 fade to green
  2. While LEDs 0, 1, 2, 3, 4 stay green,
    Make LEDs 9, 10, 11, 12, 13 fade to green.
  3. While LEDs 9, 10, 11, 12, 13 stay green,
    Make LEDs 16, 17, 18, 19, 20 fade to green,
    Make LEDs 0, 1, 2, 3, 4 fade to OFF
  4. While LEDs 16, 17, 18, 19, 20 stay green,
    Make LEDs 25, 26, 27, 28, 29 fade to green,
    Make LEDs 9, 10, 11, 12, 13 fade to OFF
  5. etc

So with each step we kind-a see this:

- "block" X fades to green (where X is the current block or column)
- "block" X-2 fades to black
(- the rest ("block" X-1) remains untouched, either green or black, but we do not need to do anything with that)

For reference the numbers we're looking at:

Seems we have 6 "blocks", each of 5 LEDs, and the blocks start at LEDs 0, 9, 16, 25, 32, 41. (alternating +9 (5+4)  and +7 (5+2))

Block X, X-1 and X-2 can potentially be invisible.
For example:
- when X = 1 then we see only Block 1, since X-1 and X-2 are negative, or
- when X=8 (passing the 6th block), then blocks X and X-1 are invisible.

I hope that made sense - took me a minute to figure that one out as well 😜 

So ideally I'd like to have a function that draws the 2 (3) steps we just found, based on the position.
Meaning: in the list above we have items 1, 2, 3, 4, etc and I'd like to pass that number to a function so the proper leds will be drawn or faded.

Something like this, so we can say what column we want and what the target color should be (so we can use this later for another color as well);

void drawVertical(int Column, CRGB targetColor) {
  ...
}

 

In this function we want to do the steps we found:

- Draw the column (X) if visible
- Fade a column if visible (X-2)

Since you do fade to green and fade to black at the same time, we should actually do these changes simultaneously.

So a for-loop should fade one column in (X = ColumnXStart) and one column out (X-2 = ColumnX2Start) at the same time.
We will need to calculate the starting points of X and X-2 (I know the calculation looks a little funny - below I'll post code that shows the calculation works)

ColumnXStart = (int)*( (Column+1)/2 )*9 + (int)*( Column/2 )*7;
ColumnX2Start = (int)(( (Column-2) +1)/2)*9 + (int)( (Column-2) /2)*7;

for(int i=0; i<=5; i++) {
  if( (ColumnXStart>=0) && ( (ColumnXStart+i)<NUM_LEDS) ) {
    leds[ColumnXStart+i] = targetColor / (5-i); // something to scale up brightness - probably not the best method
  }
  if( (ColumnX2Start>=0) && ( (ColumnX2Start+i)<NUM_LEDS) ) {
    leds[ColumnX2Start+i] = leds[ColumnX2Start+i].fadeToBlackBy(25); // experiment with this number (25)
  }
  FastLed.show():
}

Hope this gibberish gets you started ... 😉 
Note that calling the function, probably should go from zero to 8 (for 6 columns).  

 

 


 

Calculation X and X-2 example/test code:

void setup() {
  Serial.begin(9600);
  
  int ColumnXStart;
  int ColumnX2Start;
  
  for(int Column=0; Column<8; Column++) {
    ColumnXStart = (int)((Column+1)/2)*9 + (int)(Column/2)*7;
    ColumnX2Start = (int)(( (Column-2) +1)/2)*9 + (int)( (Column-2) /2)*7;
    Serial.print("Column=");
    Serial.print(Column);
    Serial.print(" X= ");
    Serial.print(ColumnXStart);
    Serial.print(" X2= ");
    Serial.println(ColumnX2Start);
  }
}

void loop() {
  
}

 

Will output something like this:

Column=0 X= 0 X2= -7
Column=1 X= 9 X2= 0
Column=2 X= 16 X2= 0
Column=3 X= 25 X2= 9
Column=4 X= 32 X2= 16
Column=5 X= 41 X2= 25
Column=6 X= 48 X2= 32
Column=7 X= 57 X2= 41

 

 Note: Only set a LED for X or X2 when it's a positive number and smaller than NUM_LEDS.


   
ReplyQuote
Share: