2016-12-07 18 views
1

私は宣言した配列のリストをトリガーするループを作ろうとしています。しかし、これまでのところ何も動作していないようです。Arduinoで変数名をループする方法は?

目的は、ループにネオピクセルでアニメーションを作成させることです。配列はそのアニメーションのキーフレームですが、おそらくこれを行うためのより効率的な方法があります。しかし、これはおそらく私の要件を満たすでしょう。

は、だから私はすでにこのようにそれを試してみた:

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]); 
    } 
    } 
} 

をしかし、これは「配列の添字には無効なタイプ 『のchar [INT]』」このエラーが発生しますが、それは私の配列名と同じ名前を印刷しません。

助けてください!ありがとう!

+0

このアプローチは、いくつかのスクリプト言語のために右のようなものですが、Arduinoのは、* *コンパイルされます。プログラムが実行されると、変数名のようなものは意味を持ちません。そのような名前はあなたとコンパイラの間にあります。 – unwind

答えて

1

アニメーションは、別々の配列ではなく、2次元配列として宣言する必要があります。そうすれば、2つのfor-loopでそれらを循環させることができます。

RGB値を32ビット整数として保存できるNeoPixel関数があります。ストリップ上で制御したい3つのLEDがあるとしましょう(簡略化のため)。あなたのアニメーションに4つの "キーフレーム"を宣言しましょう。私たちは、赤、緑、青、白の色合いに行きます。

#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); 
    } 
} 

ご覧のとおり、アニメーションを明示的に定義するのはちょっと面倒です。そういうわけで、ほとんどの人は、必要なアニメーションの種類を実行するための別注アルゴリズムを書いています。コードの軽量化と変更の迅速化。しかし、ちょっとホー、それはあなたのために働く場合、誰が私はあなたを停止する!

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

関連する問題