Examples

| Light controller by BARRAGAN <http://barraganstudio.com> Demonstrates the use of analog input to turn on a a number of lights according to the analog value read, the lower the fewer lights are turn on, the higher, more of them are on. Lights (LEDs) are attached to digital pins 0 to 7 Created 5 February 2004 Revised 4 April 2007 |

    // Light controller
    // by BARRAGAN <http://barraganstudio.com>

    int resistorPin = 0;           // slider variable connected to analog pin 0
    int value = 0;                 // variable to read the value from the analog pin

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

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

    void loop()
    {
      turn_all_off();                    // turns off all lights
      value = analogRead(resistorPin);   // reads the value of the variable resistor
                                         // connected to analog pin 0.
                                         // posible values are betwwen 0 and 1024
                                         // where 0 corresponds to 0 volts and 1024 to 5 volts

      if(value >= 0)                     // turns on as many lights according to the value read
        digitalWrite(0, HIGH);
      if(value >= 128)
        digitalWrite(1, HIGH);
      if(value >= 256)
        digitalWrite(2, HIGH);
      if(value >= 384)
        digitalWrite(3, HIGH);
      if(value >= 512)
        digitalWrite(4, HIGH);
      if(value >= 640)
        digitalWrite(5, HIGH);
      if(value >= 768)
        digitalWrite(6, HIGH);
      if(value >= 896)
        digitalWrite(7, HIGH);
      delay(100);                        // waits 100 milli seconds for next reading
    }

|  | Circuit schematics: each LED is individually connected to Wiring digital pins 0 to 7. The potentiometer is connected to Wiring analog input pin 0.