~ubuntu-branches/ubuntu/precise/arduino/precise

« back to all changes in this revision

Viewing changes to examples/Display/RowColumnScanning/RowColumnScanning.pde

  • Committer: Bazaar Package Importer
  • Author(s): Scott Howard
  • Date: 2010-04-13 22:32:24 UTC
  • Revision ID: james.westby@ubuntu.com-20100413223224-jduxnd0xxnkkda02
Tags: upstream-0018+dfsg
ImportĀ upstreamĀ versionĀ 0018+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
  Row-Column Scanning an 8x8 LED matrix with X-Y input
 
3
 
 
4
 This example controls an 8x8 LED matrix using two analog inputs
 
5
 
 
6
 created 27 May 2009
 
7
 modified 29 Jun 2009
 
8
 by Tom Igoe
 
9
 
 
10
 This example works for the Lumex  LDM-24488NI Matrix. See 
 
11
 http://sigma.octopart.com/140413/datasheet/Lumex-LDM-24488NI.pdf
 
12
 for the pin connections
 
13
 
 
14
 For other LED cathode column matrixes, you should only need to change 
 
15
 the pin numbers in the row[] and column[] arrays
 
16
 
 
17
 rows are the anodes
 
18
 cols are the cathodes
 
19
 ---------
 
20
 
 
21
 Pin numbers:
 
22
 Matrix:
 
23
 * Digital pins 2 through 13,
 
24
 * analog pins 2 through 5 used as digital 16 through 19
 
25
 Potentiometers:
 
26
 * center pins are attached to analog pins 0 and 1, respectively
 
27
 * side pins attached to +5V and ground, respectively.
 
28
 
 
29
 http://www.arduino.cc/en/Tutorial/RowColumnScanning
 
30
 
 
31
 see also http://www.tigoe.net/pcomp/code/category/arduinowiring/514 for more
 
32
 */
 
33
 
 
34
 
 
35
// 2-dimensional array of row pin numbers:
 
36
const int row[8] = {
 
37
  2,7,19,5,13,18,12,16 };
 
38
 
 
39
// 2-dimensional array of column pin numbers:
 
40
const int col[8] = {
 
41
  6,11,10,3,17,4,8,9  };
 
42
 
 
43
// 2-dimensional array of pixels:
 
44
int pixels[8][8];           
 
45
 
 
46
// cursor position:
 
47
int x = 5;
 
48
int y = 5;
 
49
 
 
50
void setup() {
 
51
  Serial.begin(9600);
 
52
  // initialize the I/O pins as outputs:
 
53
 
 
54
  // iterate over the pins:
 
55
  for (int thisPin = 0; thisPin < 8; thisPin++) {
 
56
    // initialize the output pins:
 
57
    pinMode(col[thisPin], OUTPUT); 
 
58
    pinMode(row[thisPin], OUTPUT);  
 
59
    // take the col pins (i.e. the cathodes) high to ensure that
 
60
    // the LEDS are off: 
 
61
    digitalWrite(col[thisPin], HIGH);    
 
62
  }
 
63
 
 
64
  // initialize the pixel matrix:
 
65
  for (int x = 0; x < 8; x++) {
 
66
    for (int y = 0; y < 8; y++) {
 
67
      pixels[x][y] = HIGH;
 
68
    }
 
69
  }
 
70
}
 
71
 
 
72
void loop() {
 
73
  // read input:
 
74
  readSensors();
 
75
 
 
76
  // draw the screen:
 
77
  refreshScreen();
 
78
}
 
79
 
 
80
void readSensors() {
 
81
  // turn off the last position:
 
82
  pixels[x][y] = HIGH;
 
83
  // read the sensors for X and Y values:
 
84
  x = 7 - map(analogRead(0), 0, 1023, 0, 7);
 
85
  y = map(analogRead(1), 0, 1023, 0, 7);
 
86
  // set the new pixel position low so that the LED will turn on
 
87
  // in the next screen refresh:
 
88
  pixels[x][y] = LOW;
 
89
 
 
90
}
 
91
 
 
92
void refreshScreen() {
 
93
  // iterate over the rows (anodes):
 
94
  for (int thisRow = 0; thisRow < 8; thisRow++) {
 
95
    // take the row pin (anode) high:
 
96
    digitalWrite(row[thisRow], HIGH);
 
97
    // iterate over the cols (cathodes):
 
98
    for (int thisCol = 0; thisCol < 8; thisCol++) {
 
99
      // get the state of the current pixel;
 
100
      int thisPixel = pixels[thisRow][thisCol];
 
101
      // when the row is HIGH and the col is LOW,
 
102
      // the LED where they meet turns on:
 
103
      digitalWrite(col[thisCol], thisPixel);
 
104
      // turn the pixel off:
 
105
      if (thisPixel == LOW) {
 
106
        digitalWrite(col[thisCol], HIGH);
 
107
      }
 
108
    }
 
109
    // take the row pin low to turn off the whole row:
 
110
    digitalWrite(row[thisRow], LOW);
 
111
  }
 
112
}