array
arrayはデータ型の一つで、変数を集めて配列を作成します。
変数は { } に , で区切って配列します。[ ]は配列の数を初期化します。
int myArray[10]={9,3,2,4,3,2,7,8,9,11};
インデックス([0]からはじまる整数、[9]なら配列の10番目を指す)で、変数を呼び出すことが出来る。
// myArray[9] contains 11 // myArray[10] is invalid and contains random information (other memory address)
forループと組み合わせて使われることが多い。https://www.arduino.cc/en/Tutorial/Arrays
int timer = 100; // The higher the number, the slower the timing.
int ledPins[] = { 2, 7, 4, 6, 5, 3
}; // an array of pin numbers to which LEDs are attached
int pinCount = 6; // the number of pins (i.e. the length of the array)
void setup() {
// the array elements are numbered from 0 to (pinCount - 1).
// use a for loop to initialize each pin as an output:
for (int thisPin = 0; thisPin < pinCount; thisPin++) {
pinMode(ledPins[thisPin], OUTPUT); } }
void loop() {
// loop from the lowest pin to the highest:
for (int thisPin = 0; thisPin < pinCount; thisPin++) {
// turn the pin on:
digitalWrite(ledPins[thisPin], HIGH); delay(timer); // turn the pin off: digitalWrite(ledPins[thisPin], LOW); } // loop from the highest pin to the lowest:
for (int thisPin = pinCount - 1; thisPin >= 0; thisPin--) {
// turn the pin on: digitalWrite(ledPins[thisPin], HIGH); delay(timer); // turn the pin off: digitalWrite(ledPins[thisPin], LOW); } }