Arduino示例代碼講解:Row-Column Scanning an 8x8 LED matrix with X-Y input LED矩陣
- Row-Column Scanning an 8x8 LED matrix with X-Y input LED矩陣
- 功能概述
- 硬件部分:
- 軟件部分:
- 代碼逐行解釋
- 定義常量
- 定義變量
- `setup()` 函數
- `loop()` 函數
- `readSensors()` 函數
- `refreshScreen()` 函數
- 工作原理
Row-Column Scanning an 8x8 LED matrix with X-Y input LED矩陣
這段代碼是一個Arduino示例程序,用于通過行-列掃描控制一個8x8的LED矩陣。它使用兩個模擬輸入(例如電位器)來控制光標的位置,并在LED矩陣上顯示光標的位置。
/*Row-Column Scanning an 8x8 LED matrix with X-Y inputThis example controls an 8x8 LED matrix using two analog inputscreated 27 May 2009modified 30 Aug 2011by Tom IgoeThis example works for the Lumex LDM-24488NI Matrix. Seehttp://sigma.octopart.com/140413/datasheet/Lumex-LDM-24488NI.pdffor the pin connectionsFor other LED cathode column matrixes, you should only need to changethe pin numbers in the row[] and column[] arraysrows are the anodescols are the cathodes---------Pin numbers:Matrix:* Digital pins 2 through 13,* analog pins 2 through 5 used as digital 16 through 19Potentiometers:* center pins are attached to analog pins 0 and 1, respectively* side pins attached to +5V and ground, respectively.This example code is in the public domain.http://www.arduino.cc/en/Tutorial/RowColumnScanningsee also http://www.tigoe.net/pcomp/code/category/arduinowiring/514 for more*/// 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;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:f