source: arduino-1-6-7/trunk/fuentes/arduino-ide-amd64/examples/07.Display/barGraph/barGraph.ino @ 4837

Last change on this file since 4837 was 4837, checked in by daduve, 2 years ago

Adding new version

File size: 1.6 KB
Line 
1/*
2  LED bar graph
3
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.
8
9  This method can be used to control any series of digital outputs that
10  depends on an analog input.
11
12  The circuit:
13   * LEDs from pins 2 through 11 to ground
14
15 created 4 Sep 2010
16 by Tom Igoe
17
18 This example code is in the public domain.
19
20 http://www.arduino.cc/en/Tutorial/BarGraph
21 */
22
23
24// these constants won't change:
25const int analogPin = A0;   // the pin that the potentiometer is attached to
26const int ledCount = 10;    // the number of LEDs in the bar graph
27
28int ledPins[] = {
29  2, 3, 4, 5, 6, 7, 8, 9, 10, 11
30};   // an array of pin numbers to which LEDs are attached
31
32
33void setup() {
34  // loop over the pin array and set them all to output:
35  for (int thisLed = 0; thisLed < ledCount; thisLed++) {
36    pinMode(ledPins[thisLed], OUTPUT);
37  }
38}
39
40void loop() {
41  // read the potentiometer:
42  int sensorReading = analogRead(analogPin);
43  // map the result to a range from 0 to the number of LEDs:
44  int ledLevel = map(sensorReading, 0, 1023, 0, ledCount);
45
46  // loop over the LED array:
47  for (int thisLed = 0; thisLed < ledCount; thisLed++) {
48    // if the array element's index is less than ledLevel,
49    // turn the pin for this element on:
50    if (thisLed < ledLevel) {
51      digitalWrite(ledPins[thisLed], HIGH);
52    }
53    // turn off all pins higher than the ledLevel:
54    else {
55      digitalWrite(ledPins[thisLed], LOW);
56    }
57  }
58}
59
60
61
Note: See TracBrowser for help on using the repository browser.