Examples

| Swing LED by BARRAGAN <http://barraganstudio.com> Demonstrates the use of digital pins to turn on one light (LED) at a time in a row of 8, one after the other. Created 5 December 2003 Revised 1 May 2007 |

    // Swing LED
    // by BARRAGAN <http://barraganstudio.com>

    int which = 0;                 // variable the keeps which light must be turn on next

    void turn_all_off()            // function to turn off all the lights
    {                              // connected to digital pins 0 to 7
      int i;
      for(i=0; i<8; i++)
      {
        digitalWrite(i, LOW);
      }
    }

    void setup()
    {
      int i;
      for(i=0; i<8; i++)           // initializes pins 0 to 7 as outputs
      {
        pinMode(i, OUTPUT);
      }
    }

    void loop()
    {
      turn_all_off();              // turns all lights off
      digitalWrite(which, HIGH);   // sets on the current light on
      delay(200);                  // waits for 200 milli seconds
      which = which + 1;           // increment the variable to turn on the next one next time
      if(which > 7)                // check for the range, if greater then 7 goes back to 0
      {
        which = 0;
      }
    }

|  | Circuit schematics: each LED is individually connected to Wiring digital pins 0 to 7.