hey, how can i use memcpy to animate sequences?

hey,

how can i use memcpy to animate sequences?

i have random8(0,255) to fill a circle of leds with random colours, and im trying to rotate the colours around.

using;
void moveRight() {
for (int i=0; i<NUM_LEDS; i++) {
memmove( &leds[i+1], &leds[i], 10 * sizeof(CRGB) );
delay(30);
FastLED.show();
}
}

i can see where im going wrong, as the memmove works, but i dont know how to move all the leds. whats happening at the moment, is that the first led [0] is copied to second led [0+1] as predicted, and then [1] is copied to [1+1], except, it does it after 0>1 so instead of getting 0,1,2,3,4,5 > 1,2,3,4,5 i get 0,1,2,3,4,5 > 0,0,0,0,0

any ideas?

So - let’s step through the loop - and say the first 10 leds are { a,b,c,d,e,f,g,h,i,j } (using letters so that they’re not getting confused with indexes).

When i = 0, you do a memmove, from leds[0] to leds[1] - which should give you (memmove understands how to handle overlapping memory, so that you don’t just get the first value copied X times over):

{ a, a, b, c, d, e, f, g, h, i }

Now, when i = 1, and you do the memmove from leds[1] to leds[2] you will get

{ a, a, a, b, c, d, e, f, g, h }

etc… until you will end up with { a, a, a, a, a, a, a, a, a, a }

Now, there’s a couple problems with your code as written. First of all, your memmove, because you’re starting at leds[i] is going to overwrite memory and cause a buffer overflow as soon as you get to NUM_LEDS-10.

If what you want to do is move all the leds right 1 and have things walk off the edge of the leds, then all you need for moveRight is:

for(int i = 0; i < NUM_LEDS; i++) {
memmove( &leds[1]], &leds[0], (NUM_LEDS-1) * sizeof(CRGB));
FastLED.show();
delay(30);
}

This will move all the leds every frame. Of course, it means that the value in leds[0] will eventually be the value in every led. Now - if what you want to do is you want them to rotate , that is, as colors move right off the end of the array of leds, then you need to save/restore:

for(int i = 0; i < NUM_LEDS; i++) {
CRGB last_led = leds[NUM_LEDS-1];
memmove(&leds[1], &leds[0], (NUM_LEDS-1)*(sizeof(CRGB));
leds[0] = last_led;
FastLED.show();
delay(30);
}

hey, this seems mostly like mine, with a couple of minor tweaks, but i cant find how the i variables intertwine?

ive tried memmove(&leds[i+1], &leds[i], (NUM_LEDS-1)*(sizeof(CRGB));

but this only half works.

Stop using i as a part of memmove. You don’t need it right now, and as part of learning to understand how memmove works, you’re just adding more complexity.

after playing;
void moveRightMemset() {
for (int i=0; i<NUM_LEDS; i++) {
CRGB last_led = leds[NUM_LEDS-1];
memmove( &leds[1], &leds[0], (NUM_LEDS-1)*sizeof(CRGB));
leds[i] = last_led;
FastLED.show();
delay(30);
}
}

@Daniel_Garcia i thought that i was a derivative of memmove. apologies.

if we’re not using it, then surely;

void moveRightMemset() {
CRGB last_led = leds[NUM_LEDS-1];
memmove( &leds[1], &leds[0], (NUM_LEDS-1)*sizeof(CRGB));
leds[0] = last_led;
FastLED.show();
delay(300);
}

is valid?

Change leds[i] = last_led; to leds[0] = last_led;. As Daniel has said a few times, you don’t need to use i inside the for-loop. The only purpose of the loop from what I can tell, is to repeat the move over and over again until everything has been rotated around once. That means that i is just the frame number of an animation. It has nothing to do with what you’re copying, from where, and to where.

EDIT: supposedly fixed formatting. Fancy formatting is really hard on mobile.

so i doesnt have to be in the formulas “inside the {} loops” it just runs as a counter?

void moveRightMemset() {
for (int i=0; i<NUM_LEDS; i++) {
CRGB last_led = leds[NUM_LEDS-1];
memmove( &leds[1], &leds[0], (NUM_LEDS-1)*sizeof(CRGB));
leds[0] = last_led;
FastLED.show();
delay(300);
}
}

working exactly as imagined! :slight_smile:

WOW! i never realised how the i worked in these loops!