From 8e10fc489c3e26fa5e1a4bc76c1c9f194248454b Mon Sep 17 00:00:00 2001 From: Tom Igoe Date: Sun, 5 Jul 2009 19:07:08 +0000 Subject: [PATCH] Added Array example, separate from for loop --- .../dist/examples/Control/Arrays/Arrays.pde | 55 +++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 build/shared/dist/examples/Control/Arrays/Arrays.pde diff --git a/build/shared/dist/examples/Control/Arrays/Arrays.pde b/build/shared/dist/examples/Control/Arrays/Arrays.pde new file mode 100644 index 000000000..990dcc28c --- /dev/null +++ b/build/shared/dist/examples/Control/Arrays/Arrays.pde @@ -0,0 +1,55 @@ +/* + Arrays + + Demonstrates the use of an array to hold pin numbers + in order to iterate over the pins in a sequence. + Lights multiple LEDs in sequence, then in reverse. + + Unlike the For Loop tutorial, where the pins have to be + contiguous, here the pins can be in any random order. + + The circuit: + * LEDs from pins 2 through 7 to ground + + created 2006 + by David A. Mellis + modified 5 Jul 2009 + by Tom Igoe + + http://www.arduino.cc/en/Tutorial/Array + */ + +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() { + int thisPin; + // 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; i < 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 (thisPin = pinCount - 1; thisPin >= 0; thisPin--) { + // turn the pin on: + digitalWrite(ledPins[thisPin], HIGH); + delay(timer); + // turn the pin off: + digitalWrite(ledPins[thisPin], LOW); + } +}