4
Turns on a series of LEDs based on the value of an analog sensor.
5
This is a simple way to make a bar graph display. Though this graph
6
uses 10 LEDs, you can use any number by changing the LED count
7
and the pins in the array.
9
This method can be used to control any series of digital outputs that
10
depends on an analog input.
13
* LEDs from pins 2 through 11 to ground
18
This example code is in the public domain.
20
http://www.arduino.cc/en/Tutorial/BarGraph
24
// these constants won't change:
25
const int analogPin = A0; // the pin that the potentiometer is attached to
26
const int ledCount = 10; // the number of LEDs in the bar graph
29
2, 3, 4, 5, 6, 7,8,9,10,11 }; // an array of pin numbers to which LEDs are attached
33
// loop over the pin array and set them all to output:
34
for (int thisLed = 0; thisLed < ledCount; thisLed++) {
35
pinMode(ledPins[thisLed], OUTPUT);
40
// read the potentiometer:
41
int sensorReading = analogRead(analogPin);
42
// map the result to a range from 0 to the number of LEDs:
43
int ledLevel = map(sensorReading, 0, 1023, 0, ledCount);
45
// loop over the LED array:
46
for (int thisLed = 0; thisLed < ledCount; thisLed++) {
47
// if the array element's index is less than ledLevel,
48
// turn the pin for this element on:
49
if (thisLed < ledLevel) {
50
digitalWrite(ledPins[thisLed], HIGH);
52
// turn off all pins higher than the ledLevel:
54
digitalWrite(ledPins[thisLed], LOW);