~ubuntu-branches/debian/experimental/arduino/experimental

« back to all changes in this revision

Viewing changes to build/shared/examples/7.Display/barGraph/barGraph.pde

  • Committer: Package Import Robot
  • Author(s): Scott Howard
  • Date: 2012-03-11 18:19:42 UTC
  • mfrom: (1.1.5) (5.1.14 sid)
  • Revision ID: package-import@ubuntu.com-20120311181942-be2clnbz1gcehixb
Tags: 1:1.0.1~rc1+dfsg-1
New upstream release, experimental.

Show diffs side-by-side

added added

removed removed

Lines of Context:
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:
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
27
 
 
28
 
int ledPins[] = { 
29
 
  2, 3, 4, 5, 6, 7,8,9,10,11 };   // an array of pin numbers to which LEDs are attached
30
 
 
31
 
 
32
 
void setup() {
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); 
36
 
  }
37
 
}
38
 
 
39
 
void loop() {
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);
44
 
 
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);
51
 
    } 
52
 
    // turn off all pins higher than the ledLevel:
53
 
    else {
54
 
      digitalWrite(ledPins[thisLed], LOW); 
55
 
    }
56
 
  }
57
 
}
58
 
 
59
 
 
60