Trying to move arrays of LEDS on 2812

Hi guys,

So I'm trying to move this array of leds on a LED strip. I have 162 leds on my 2812 strip. Below is a snippet of my code. The code lights up 4 groups of 5 LEDS on the strip. What I'm wanting to do is move them all to the left 10 spots and all to the right 10 spots simutaneosly. I don't know where to start. Been scouring online and the forum and am lost. Any advice? Thanks!

int myBases[ ] = {14,15,16,17,18,  60,61,62,63,64,  97,98,99,100,101,  143,144,145,146,147};      //address of LED's to be passed into the for loop
int b;

........
{
    TouchThemAll();
  }


void TouchThemAll()
{
 FastLED.clear();
Bases();
}
void Bases() {

  for (b = 0; b < 20; b++)  //To me this says, turn every led on at led[i address]
  {
    leds[myBases[b]] = CRGB::White;
   
  }
FastLED.show();
}

Hello

leds[myBases[b] + n], where n is your shift position ( -10 to 10 )

Thanks for the reply guix!
So I would (+ n) then the same line after with (-n) to get the arrays of LEDs to move left then right? Sounds easy!

So here is my complete sketch. I'm not able to get the arrays of leds to simultaneosly shift over 4 spots then back. to the original position.
When its loaded to the arduino, the leds light up 4 spots over from their array position. What I'm wanting is an animation for them all to shift over 4 spots then back and repeat. Sorry I wasn't more clear....Any suggestions?

#include "FastLED.h"

#define NUM_LEDS 50

#define DATA_PIN 5
//#define CLOCK_PIN 13


int myBases[ ] = {7,8,9,10,  17,18,19,20,  28,29,30,31,  39,40,41,42};      //address of LED's to be passed into the for loop
int b;
#define n 4

CRGB leds[NUM_LEDS];

void setup() 
{     
       FastLED.addLeds<WS2812, DATA_PIN, RGB>(leds, NUM_LEDS);
}

void loop() 
{
TouchThemAll();
}

void TouchThemAll()
{
 FastLED.clear();
Bases();
}
void Bases() {

 
  for (b = 0; b < 16; b++)  //To me this says, turn every led on at led[i address]
  {
    leds[myBases[b]+n] = CRGB::White;
    delay(20);
    leds[myBases[b]-n] = CRGB::White;
    delay(20);
  }
  
FastLED.show();
}

Not that easy, but not much harder :slight_smile:

Here is an example : t938915.ino - Wokwi Arduino and ESP32 Simulator

Guix!
THANK YOU! I believe I can extrapolate what I need out of your sketch. Thank you for your guidance. Much appreciated!

Glad it helped :wink:

I have simplified the previous example

But if you wanted to move directly to target position and not one pixel at a time, here is another example using a simpler method : t938915_2.ino - Wokwi Arduino and ESP32 Simulator

Note that in my examples I never check if the leds array indices are valid, so in your code make sure to add protection to never read or write outside of the leds array

GUIX: Thanks again man! One last question: if i just wanted to run the sequence say 5 times for example, would it look something like this below? Sorry not in front of my setup at the moment so I'm thinking out loud...

{
  #define NUM_PATTERN1 5

---------

for (int i = 0; i < NUM_PATTERN1; i++) {
    pattern1();
  }
  
void pattern1()

//your code:
    for ( uint8_t i = 0; i < basesCount; i++ )
      {
        for ( uint8_t j = basesStart[i], k = j + basesLength; j < k; j++ )
        {
          leds[j] = CRGB::White;
        }
  }

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.