I have a question about Arrays.

I'm building some animations for a 300 LED strip. WS2318b, if it matters at all.

Now I wanted to do some things just for a couple of LEDs and have discovered that I can do arrays.

int allOther[] = {0,  1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16,
                  17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31,
                  32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46,
                  47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61,
62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76,
77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91,
92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104,
106, 107, 109, 110, 112, 113, 115, 116,
118, 119, 121, 122, 124, 125, 127, 128,
130, 131, 133, 134, 136, 137, 139, 140,
142, 143, 145, 146, 148, 149   };

This is so that only the last couple of every LEDs ligth up, but writing this array was awfully awkward. I am pretty sure there are shorter/smarter ways to achieve that, but I'm not really sure what search term I'm looking for.

I thought something along the lines of "

array1[]= not the first 200, and every third among the last 100)
array2[] = the ones not in array1

or like:

array3[]= every third LED
array4[]= every third LED but start at position 1
array5[]= every third LED but start at position 2

I'm not asking you to code it for me, I just want to know where I should look for if I want to learn how to declare arrays the smart way

Sorry, don't understand the question - you describe 300 LEDs, but your array has only 150 entries.

yes, because I thought there must be smarter ways to do this as opposed to type out every number seperated by a comma. That's why I stopped midway through

Then don't use an array, use a for loop

I already did, I tried this: (assume 150 leds)

for(int dot = 100; dot < 150; dot+3) { 
     leds[dot] = CRGB::Blue;
     FastLED.show();
     // clear this led for the next time around the loop
     leds[dot] = CRGB::Black;
     delay(30);
 }

It looks like you are setting up a pattern and just dragging it along the strip, is this the case?

[edit] :

Wait a second.. If you want an array of 300 things, you just say :

thing myArrayName[300];

And there you have it. 300 thing(s).

[edit II] :

Your loop won't show anything. Why? Because the instant you show something, you turn the LED off. Then wait a bit as its off.

-jim lee

for(int dot = 100; dot < 150; dot+3) Oops

Now it might not fit the title anymore, but: Let's imagine a strip of 60 LEDs

Now the first two are solid Blue. The third one is Red. Now after 250ms, the lights 'move up', so that now the second and thrid are Blue, the fourth is red. After another 250 ms, the third and fourth are Blue, the fifth is Red... you get the idea. I thought I can do this by making an array and making every entry within it advance +1.
But as suggested, I tried it with a for loop, this is what I got so far:_

int a=0;
int b=1;
int c=2;

void loop() {
if(a<3) {
for(int dot = a, mot = b; dot, mot < NUM_LEDS; dot+=3, mot +=3) {
     leds[dot] = CRGB::Blue;
     leds[mot] = CRGB::Blue;
     FastLED.show();
  //   fadeToBlackBy(leds, NUM_LEDS, 100);
     // clear this led for the next time around the loop
     //leds[dot] = CRGB::Black;
     //delay(30);
     }
for(int zot = c; zot < NUM_LEDS; zot+=3) {
  leds[zot] = CRGB::Red;
  FastLED.show();
}

fill_solid( leds, NUM_LEDS, CRGB::Black);
delay(2500);
a+=1;
b+=1;
c+=1;
}

Now if the pattern is to move through the strip continuously, after a=2 the pattern repeats itself. Now I just need to figure out how to shove Pixels back in, right now once the if condition is met the animation stops

Perhaps you intended dot+=3 instead of dot+3 but why you call show inside the loop is beyond me.

What I do..

Set up a function that, using a loop moves each pixel up by one then saves the last one and, if you like, stuffs it in position 0;

Here's mine. I have an object that stores a color for my pixels that I use. But you can at a least see what I'm doing.

When I call this, I get the color that is pushed off the end. This way I can, If I want, use it to replace the other end that everything was shifted from. Making it cycle around possibly forever. The bool toEnd just sets the direction I'm going in.

colorObj neoPixel::shiftPixels(bool toEnd) {

  colorObj  aColor;
  colorObj  lastColor;
  int       last;

  last = numPixels()-1;
  if (toEnd) {                              // Feed new colors onto pixel 0, pull 'em through to the end.
    lastColor = getPixelColor(last);
    for(int i=last;i>0;i--) {
      aColor = getPixelColor(i-1);
      setPixelColor(i,&aColor);
    }
  } else {                                  // Feed new colors onto last pixel, push 'em to the start..
    lastColor = getPixelColor(0);
    for(int i=0;i<last;i++) {
      aColor = getPixelColor(i+1);
      setPixelColor(i,&aColor);
    }
  }
  return lastColor;
}

Now using this function you set up the initial pattern on your string of lights. Then, cvery x ms call your function to shift them all over, then call show(). Your pattern will run along the string.

Something like this.

loop() {

colorObj aColor;

aColor = myPixels.shiftPixels(true); // Shift everyone down by one, pass back the shifted off end.
myPixels.setPixelColor(0,&aColor); // Stuff the saved color into the first pixel position.
myPixels.show(); // Everything is set up, fire off the "drawing" function.
delay(30); // Wait for the human to see it.
}

Hope this helps.

-jim lee

vaj4088:
Perhaps you intended dot+=3 instead of dot+3 but why you call show inside the loop is beyond me.

I already fixed that, also I got the code from the basics of FastLED, I put my show where the example is. Where does it belong?

Coulomb-d:
Where does it belong?

Where / when you want to show the pattern you've just made.
Not usually while you're making it.

Now I've defined a function

void movingdots() {

  for(int dot = a, mot = b; dot, mot < NUM_LEDS; dot+=3, mot +=3) {
       leds[dot] = CRGB::Blue;
       leds[mot] = CRGB::Blue;
//       FastLED.show();
       leds[dot] = CRGB::Black;
       leds[mot] = CRGB::Black;
       delay(100);
       }
  for(int zot = c; zot < NUM_LEDS; zot+=3) {
    leds[zot] = CRGB::Red;
    delay(30);
    //FastLED.show();
    leds[zot] = CRGB::Black;
  }


}

I commented out show.

void loop() {

movingdots();
FastLED.show();

delay(75);


} //loop

This way I the Leds stay black, I can only see the pattern I made by calling FastLED.show(); within the for loops. That's just how it is.

See reply #11

then what is wrong with the place where I called the show function? I believe it is supposed to be right where I put it, so I don't understand what the comment was about...

jimLee:
thing myArrayName[300];

And there you have it. 300 thing(s).

This is what I wanted to know in the first place.
but what if I want only a subset out of the 300, say 100.
The only way I know is to type out all 100 numbers, and I really thought there must be a quicker way to declare such a thing.

Btw, you are not using the FastLED library, why?

The "for" loop executes things many times. show() should only get called once AFTER everything is set up.

I have a wandering pixel in this function. So the first pixel needs to be shown, then follows the delay, then the next pixel is shown. If I call show only once after it is set up, the whole strip would light up at once. I can literally just test it with my test strip in front of me. so what is our misunderstanding here?

Your misunderstanding is that you only call "show" when you want to see the pattern that you've just drawn into memory.

Ok, maybe you can clear my misunderstanding by un-commenting the correct place to call show in this sample sketch - or put it in a entirely new position:

void loop() {

movingdots();

// FastLED.show();




} //loop


void movingdots() {

  for(int dot = a, mot = b; dot, mot < NUM_LEDS; dot+=3, mot +=3) {
       leds[dot] = CRGB::Blue;
       leds[mot] = CRGB::Blue;
//       FastLED.show();
       leds[dot] = CRGB::Black;
       leds[mot] = CRGB::Black;
       delay(100);
       }
  for(int zot = c; zot < NUM_LEDS; zot+=3) {
    leds[zot] = CRGB::Red;
    delay(30);
    //FastLED.show();
    leds[zot] = CRGB::Black;
  }

draw two dots in blue.
show
delay
draw same two dots in black
show

Rinse, repeat.