Join the Stack Overflow Community
Stack Overflow is a community of 6.4 million programmers, just like you, helping each other.
Join them; it only takes a minute:
Sign up

I'm trying to make a loop that triggers a list of arrays I've declared. But nothing seems to work so far.

The goal is to let the loop create an animation on Neopixels. The arrays are keyframes of that animation, I know, there are probably better and more efficient ways to do this. But this will probably meet my requirements.

So I've tried it like this already:

const int startSwipe0[][4] = {whole list of numbers};
const int startSwipe1[][4] = {whole list of numbers};
const int startSwipe2[][4] = {whole list of numbers};
const int startSwipe3[][4] = {whole list of numbers};
const int startSwipe4[][4] = {whole list of numbers};
const int startSwipe5[][4] = {whole list of numbers};

void setup() {
  strip.begin();
  strip.setBrightness(100);   // 0-255 brightness
  strip.show();               // Initialize all pixels to 'off'
}

void loop() {
  currentTime = millis();
  animation_a();
  strip.show();
}

void animation_a() {
  for (int j=0; j<6; j++) {
    for (int i=0; i<NUM_LEDS; i++) {
      String swipeInt = String(j);
      String swipeName = "startSwipe"+swipeInt;
      Serial.println(swipeName);
      strip.setPixelColor(i, swipeName[i][1], swipeName[i][2], swipeName[i][3]);
    }
  }
}

But this gives this error "invalid types 'char[int]' for array subscript" but it does print the same name as my array names.

Please help! Thanks!

share|improve this question
    
This approach is kind of right for some scripting languages, but Arduino is compiled. Things like variable names have absolutely no meaning once the program is running, such names are just between you and the compiler. – unwind 2 days ago

You should declare your animation as a two-dimensional array, not separate arrays. That way, you can cycle through them with two for-loops.

There is a NeoPixel function that enables you to store RGB values as 32-bit integers. Let's assume you have three LEDs that you want to control on your strip (for the sake of simplicity). Let's declare four "keyframes" in your animation. We'll go from red, to green, to blue, to white-ish.

#include <Adafruit_NeoPixel.h>

#define PIN 6
#define LEDS 3

Adafruit_NeoPixel strip = Adafruit_NeoPixel(LEDS, PIN, NEO_GRB + NEO_KHZ800);

// Define animation
#define FRAMES 4
uint32_t animation[FRAMES][LEDS] = {
  {strip.Color(255, 0, 0), strip.Color(255, 0, 0), strip.Color(255, 0, 0)},
  {strip.Color(0, 255, 0), strip.Color(0, 255, 0), strip.Color(0, 255, 0)},
  {strip.Color(100, 100, 100), strip.Color(100, 100, 100), strip.Color(100, 100, 100)}
};

void setup() {
  strip.begin();
  strip.show();
}

void loop() {
  // Play the animation, with a small delay
  playAnimation(100);
}

void playAnimation(int speed) {
  for(int j=0; j<FRAMES; j++) {
    // Set colour of LEDs in this frame of the animation.
    for(int i=0; i<LEDS; i++) strip.setPixelColor(i, animation[j][i]);
    strip.show();
    // Wait a little, for the frame to be seen by humans.
    delay(speed);
  }
}

As you can see, explicitly defining animations is a little clunky. That's why most people write a bespoke algorithm to perform the type of animation they desire. It's lighter in code and much quicker to change. But hey-ho, if it works for you, then who am I to stop you!

Sources: https://learn.adafruit.com/adafruit-neopixel-uberguide/arduino-library

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.