/* Arrays and Button (Christmas Tree) The circuit: - 5 LEDs from pins 8 through 12 to ground, with 100 Ohms resistors - pushbutton attached to pin 6 from +5V - 10K resistor attached to pin 6 from ground This example code is in the public domain. http://www.arduino.cc/en/Tutorial/Button http://www.arduino.cc/en/Tutorial/Array */ int timer = 300; // The higher the number, the slower the timing. int ledPins[] = { 12,11,10,9,8}; // an array of pin numbers to which LEDs are attached int pinCount = 5; // the number of pins (i.e. the length of the array) int buttonPin = 6; // the number of the pushbutton pin int buttonState = 0; // variable for reading the pushbutton status 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); } // initialize the pushbutton pin as an input: pinMode(buttonPin, INPUT); } void loop() { // read the state of the pushbutton value: buttonState = digitalRead(buttonPin); // check if the pushbutton is pressed. If it is, the buttonState is HIGH: if (buttonState == HIGH) { // turn LED on: digitalWrite(12, HIGH);digitalWrite(11, HIGH); digitalWrite(10, HIGH);digitalWrite(9, HIGH); digitalWrite(8, HIGH); delay(timer*10); } else { // turn LED off: // 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); } } }