简体   繁体   中英

Arduino Uno array fails

I am trying to flash colors using neoPixel and atm 8 led strip (will be longer later). What I am trying to do is give a list of pixel information and loop through the list and flash the lights as the 'script array' says.

Here is the code I have done so far:

#include <Adafruit_NeoPixel.h>
#ifdef __AVR__
  #include <avr/power.h>
#endif

#define PIN 6

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

void setup() {

  strip.begin();
  strip.show();
  int array[2][8][3] = {
    {{120, 100, 40},{120, 100, 40},{120, 100, 40},{120, 100, 40},{120, 100, 40},{120, 100, 40},{120, 100, 40},{120, 100, 40}},
    {{50, 90, 200}, {50, 90, 200},{50, 90, 200},{50, 90, 200},{50, 90, 200},{50, 90, 200},{50, 90, 200},{50, 90, 200}}
  }; // flashing two colors on all leds
}

void loop() {
  fromArray(50);
}

void fromArray(uint8_t wait){
  for(int i=0; i<2; i++){
    for (int j=0; j<8; j++){
      strip.setPixelColor(j, strip.Color(array[i][j][0],array[i][j][1],array[i][j][2]))
    }
    strip.show();
    delay(wait)
  } 
} 

When I check this code I get the error 'array' was not declared in this scope from line strip.setPixelColor(j, strip.Color(array[i][j][0],array[i][j][1],array[i][j][2])) .

Your array variable is declared inside the setup function and is only available inside this function. You simply need to move the declaration of array into the global scope (outside the setup function.

#include <Adafruit_NeoPixel.h>
#ifdef __AVR__
  #include <avr/power.h>
#endif

#define PIN 6

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

int array[2][8][3] = {
    {{120, 100, 40},{120, 100, 40},{120, 100, 40},{120, 100, 40},{120, 100, 40},{120, 100, 40},{120, 100, 40},{120, 100, 40}},
    {{50, 90, 200}, {50, 90, 200},{50, 90, 200},{50, 90, 200},{50, 90, 200},{50, 90, 200},{50, 90, 200},{50, 90, 200}}
  }; // flashing two colors on all leds

void setup() {

  strip.begin();
  strip.show();
}

void loop() {
  fromArray(50);
}

void fromArray(uint8_t wait){
  for(int i=0; i<2; i++){
    for (int j=0; j<8; j++){
      strip.setPixelColor(j, strip.Color(array[i][j][0],array[i][j][1],array[i][j][2]));
    }
    strip.show();
    delay(wait);
  } 
} 

You are also missing some semi-colons in your fromArray function which I have added in my version.

You are getting this error because you array is declared in your setup() function and is not visible to the rest of your code.

You should move it to the top.

int array[2][8][3] = {
    {{120, 100, 40},{120, 100, 40},{120, 100, 40},{120, 100, 40},{120, 100, 40},{120, 100, 40},{120, 100, 40},{120, 100, 40}},
    {{50, 90, 200}, {50, 90, 200},{50, 90, 200},{50, 90, 200},{50, 90, 200},{50, 90, 200},{50, 90, 200},{50, 90, 200}}
  }; // flashing two colors on all leds

void setup() {

  strip.begin();
  strip.show();

}

void loop() {
  fromArray(50);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM