Bar graph 8

Nedenstående er en af Arduino hjemmesiden’s eksempler jeg har bygget lidt videre på.

/*
  LED bar graph
  This example code is in the public domain.
  http://www.arduino.cc/en/Tutorial/BarGraph
 */

// these constants won't change:
const int analogPin = A1;   // The Arduino Uno pin that the potentiometer is attached to
const int ledCount = 8;    // The number of LEDs in the bar graph

int ledPins[] = {
  0, 1, 2, 3, 4, 5, 6, 7 };   // An array of Arduino Uno pin numbers to which LEDs are attached

void setup() {
  // Loop over the pin array and set them all to output:
  for (int thisLed = 0; thisLed < ledCount; thisLed++) {
    pinMode(ledPins[thisLed], OUTPUT);
  }
}

void loop() {
  // Read the potentiometer:
  int sensorReading = analogRead(analogPin);
  // Map the result to a range from 0 to the number of LEDs:
  int ledLevel = map(sensorReading, 0, 1023, 0, ledCount);

  // Loop over the LED array:
  for (int thisLed = 0; thisLed < ledCount; thisLed++) {
    // If the array element's index is less than ledLevel,
    // Turn the pin for this element on:
    if (thisLed < ledLevel) {
      digitalWrite(ledPins[thisLed], HIGH);
    } 
    // Turn off all pins higher than the ledLevel:
    else {
      digitalWrite(ledPins[thisLed], LOW);
    }
  }
}