aboutsummaryrefslogtreecommitdiff
path: root/Arduino_STM32-MIDI/examples/Display/RowColumnScanning/RowColumnScanning.ino
blob: e3f2ef7a544271959bd2cdcba40a663f2787dcee (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
/*
  Row-Column Scanning an 8x8 LED matrix with X-Y input

  This example controls an 8x8 LED matrix using two analog inputs

  This example works for the Lumex  LDM-24488NI Matrix. See
  http://sigma.octopart.com/140413/datasheet/Lumex-LDM-24488NI.pdf
  for the pin connections

  For other LED cathode column matrixes, you should only need to change
  the pin numbers in the row[] and column[] arrays

  rows are the anodes
  cols are the cathodes
  ---------

  Pin numbers:
  Matrix:
  * Digital pins 2 through 13,
  * analog pins 2 through 5 used as digital 16 through 19
  Potentiometers:
  * center pins are attached to analog pins 0 and 1, respectively
  * side pins attached to +5V and ground, respectively.

  http://www.arduino.cc/en/Tutorial/RowColumnScanning

  see also http://www.tigoe.net/pcomp/code/category/arduinowiring/514 for more

  created 27 May 2009
  modified 29 Jun 2009
  by Tom Igoe

  modified for Maple
  by LeafLabs
*/


// 2-dimensional array of row pin numbers:
const int row[8] = {
    2, 7, 19, 5, 13, 18, 12, 16 };

// 2-dimensional array of column pin numbers:
const int col[8] = {
    6, 11, 10, 3, 17, 4, 8, 9 };

// Analog input pins:
const int analogPin1 = 0;
const int analogPin2 = 1;

// 2-dimensional array of pixels:
int pixels[8][8];

// cursor position:
int x = 5;
int y = 5;

void setup() {
    // initialize the analog input pins as INPUT_ANALOG
    pinMode(analogPin1, INPUT_ANALOG);
    pinMode(analogPin2, INPUT_ANALOG);

    // initialize the I/O pins as outputs:

    // iterate over the pins:
    for (int thisPin = 0; thisPin < 8; thisPin++) {
        // initialize the output pins:
        pinMode(col[thisPin], OUTPUT);
        pinMode(row[thisPin], OUTPUT);
        // take the col pins (i.e. the cathodes) high to ensure that
        // the LEDS are off:
        digitalWrite(col[thisPin], HIGH);
    }

    // initialize the pixel matrix:
    for (int x = 0; x < 8; x++) {
        for (int y = 0; y < 8; y++) {
            pixels[x][y] = HIGH;
        }
    }
}

void loop() {
    // read input:
    readSensors();

    // draw the screen:
    refreshScreen();
}

void readSensors() {
    // turn off the last position:
    pixels[x][y] = HIGH;
    // read the sensors for X and Y values:
    x = 7 - map(analogRead(analogPin1), 0, 4095, 0, 7);
    y = map(analogRead(analogPin2), 0, 4095, 0, 7);
    // set the new pixel position low so that the LED will turn on
    // in the next screen refresh:
    pixels[x][y] = LOW;
}

void refreshScreen() {
    // iterate over the rows (anodes):
    for (int thisRow = 0; thisRow < 8; thisRow++) {
        // take the row pin (anode) high:
        digitalWrite(row[thisRow], HIGH);
        // iterate over the cols (cathodes):
        for (int thisCol = 0; thisCol < 8; thisCol++) {
            // get the state of the current pixel;
            int thisPixel = pixels[thisRow][thisCol];
            // when the row is HIGH and the col is LOW,
            // the LED where they meet turns on:
            digitalWrite(col[thisCol], thisPixel);
            // turn the pixel off:
            if (thisPixel == LOW) {
                digitalWrite(col[thisCol], HIGH);
            }
        }
        // take the row pin low to turn off the whole row:
        digitalWrite(row[thisRow], LOW);
    }
}