/*
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
https://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
created 27 May 2009
modified 30 Aug 2011
by Tom Igoe
This example code is in the public domain.
https://www.arduino.cc/en/Tutorial/BuiltInExamples/RowColumnScanning
*/// 2-dimensional array of row pin numbers:
constintrow[8]={2,7,19,5,13,18,12,16};// 2-dimensional array of column pin numbers:
constintcol[8]={6,11,10,3,17,4,8,9};// 2-dimensional array of pixels:
intpixels[8][8];// cursor position:
intx=5;inty=5;
voidsetup(){// initialize the I/O pins as outputs iterate over the pins:
for(intthisPin=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(intx=0;x<8;x++){for(inty=0;y<8;y++){pixels[x][y]=HIGH;}}}
voidloop(){// read input:
readSensors();// draw the screen:
refreshScreen();}
readSensors()とrefreshScreen()を呼び出します。
readSensors()
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
voidreadSensors(){// turn off the last position:
pixels[x][y]=HIGH;// read the sensors for X and Y values:
x=7-map(analogRead(A0),0,1023,0,7);y=map(analogRead(A1),0,1023,0,7);// set the new pixel position low so that the LED will turn on in the next
// screen refresh:
pixels[x][y]=LOW;}最初に前回の位置をHIGHにします。pixelsがLOWの位置のLEDが光ります。次に、<ahref="https://garretlab.web.fc2.com/arduino.cc/www/reference/ja/language/functions/analog-io/analogread/">analogRead()</a>を使いA0とA1の値を読み、<ahref="https://garretlab.web.fc2.com/arduino.cc/www/reference/ja/language/functions/math/map/">map()</a>で変換した値をそれぞれxとyに代入します。pixels[x][y]をLOWにし、次に光らせるLEDが決まります。
voidrefreshScreen(){// iterate over the rows (anodes):
for(intthisRow=0;thisRow<8;thisRow++){// take the row pin (anode) high:
digitalWrite(row[thisRow],HIGH);// iterate over the cols (cathodes):
for(intthisCol=0;thisCol<8;thisCol++){// get the state of the current pixel;
intthisPixel=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);}}