Debian Codegraph @

examples/07.Display/RowColumnScanning/RowColumnScanning.ino

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