RowColumnScanning

はじめに

2個のアナログセンサから読み取った値をそれぞれ、X軸Y軸ととらえ、相当する部分のLEDを点灯させます。

アナログピンの0番と1番にセンサが接続されています。また、LEDは以下のように接続されています。

デジタルピンの16番から19番は、アナログピンの2番から5番です。アノードをHIGH、カソードをLOWにするとLEDは点灯します。例えば、2番ピンをHIGH、6番ピンをLOWにすると上図の一番左上のLEDが点灯します。

アノードとカソードがともにHIGHあるいはLOW、アノードがLOW、カソードがHIGHのときには、LEDは消灯します。

アノード↓/カソード→ HIGH LOW
HIGH 消灯 点灯
LOW 消灯 消灯

プログラム

定義等

 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
/*
  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:
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
};

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

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

const int型の変数(配列)rowとcolを定義し、初期化します。

int型の2次元配列pixelsを定義します。

最後に、カーソル位置を示すためのint型の変数xとyとを定義し、初期化します。

setup()

51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
void setup() {
  // 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;
    }
  }
}
 

for文を使って、thisPinを0から7まで変化させ、col[]とrow[]で指定されているピンを、pinMode()を使い、出力モードにします。また、digitalWrite()を使い、col[]で指定されているピンをHIGHにします。これで、LEDのカソード側に電圧がかかり、LEDが消灯します。

for文を入れ子にして、pixels[][]をすべてHIGHに設定します。

loop()

69
70
71
72
73
74
75
76
void loop() {
  // 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
void readSensors() {
  // 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が光ります

次に、<a href="https://garretlab.web.fc2.com/arduino.cc/www/reference/ja/language/functions/analog-io/analogread/">analogRead()</a>を使いA0とA1の値を読み<a href="https://garretlab.web.fc2.com/arduino.cc/www/reference/ja/language/functions/math/map/">map()</a>で変換した値をそれぞれxとyに代入しますpixels[x][y]LOWにし、次に光らせるLEDが決まります
 

refreshScreen()

 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
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);
  }
}

for文を使って、アノードピンに関してループさせます。まず、digitalWrite()でアノードをすべてHIGHにします。

内側のループでは、カソード側の値を制御します。digitalWrite()を使いpixels[thisRow][thisCol]の値を、カソードに出力します。この値がLOWのときに、対応するLEDが点灯します。LOWを出力した場合は、続けてHIGHにします。これで、LEDが消灯します。

外側のfor文の最後で、アノードをLOWにして、全てのLEDを消灯します。

バージョン

Hardware:Arduino Uno
Software:Arduino 1.8.16

最終更新日

November 1, 2022

inserted by FC2 system