} } //中断routinevoid display() { digitalwrite(COLS [西],低);//把以前的全部列了胶原+ +;如果(Col = = 8){ col = 0;}为(行= 0;排<8;排+ +){ if(LED [西]【7行】= = 1){ digitalwrite(行[行],低);/ /打开这导致其他digitalwrite } {(行[行],高);/ /关闭LED } } digitalwrite(COLS [西],高);//把整列在一旦(等于照明时代)}
【获取代码】
示例代码为“生命”(需要frequencytimer2图书馆):
/* * Conway's "Life" * * Adapted from the Life example * on the Processing.org site * * Needs FrequencyTimer2 library */#include byte col = 0;byte leds[8][8];// pin[xx] on led matrix connected to nn on Arduino (-1 is dummy to make array start at pos 1)int pins[17]= {-1, 5, 4, 3, 2, 14, 15, 16, 17, 13, 12, 11, 10,9, 8, 7, 6};// col[xx] of leds = pin yy on led matrixint cols[8] = {pins[13], pins[3], pins[4], pins[10], pins[06],pins[11], pins[15], pins[16]};// row[xx] of leds = pin yy on led matrixint rows[8] = {pins[9], pins[14], pins[8], pins[12], pins[1],pins[7], pins[2], pins[5]};#define DELAY 0#define SIZE 8extern byte leds[SIZE][SIZE];byte world[SIZE][SIZE][2];long density = 50;void setup() { setupLeds(); randomSeed(analogRead(5)); for (int i = 0; i < SIZE; i++) { for (int j = 0; j < SIZE; j++) { if (random(100) < density) { world[i][j][0] = 1; } else { world[i][j][0] = 0; } world[i][j][1] = 0; } }}void loop() { // Display current generation for (int i = 0; i < SIZE; i++) { for (int j = 0; j < SIZE; j++) { leds[i][j] = world[i][j][0]; } } delay(DELAY); // Birth and death cycle for (int x = 0; x < SIZE; x++) { for (int y = 0; y < SIZE; y++) { // Default is for cell to stay the same world[x][y][1] = world[x][y][0]; int count = neighbours(x, y); if (count == 3 && world[x][y][0] == 0) { // A new cell is born world[x][y][1] = 1; } if ((count < 2 || count > 3) && world[x][y][0] == 1) { // Cell dies world[x][y][1] = 0; } } } // Copy next generation into place for (int x = 0; x < SIZE; x++) { for (int y = 0; y < SIZE; y++) { world[x][y][0] = world[x][y][1]; } }}int neighbours(int x, int y) { return world[(x + 1) % SIZE][y][0] + world[x][(y + 1) % SIZE][0] + world[(x + SIZE - 1) % SIZE][y][0] + world[x][(y + SIZE - 1) % SIZE][0] + world[(x + 1) % SIZE][(y + 1) % SIZE][0] + world[(x + SIZE - 1) % SIZE][(y + 1) % SIZE][0] + world[(x + SIZE - 1) % SIZE][(y + SIZE - 1) % SIZE][0]+ world[(x + 1) % SIZE][(y + SIZE - 1) % SIZE][0]; }void setupLeds() { // sets the pins as output for (int i = 1; i <= 16; i++) { pinMode(pins[i], OUTPUT); } // set up cols and rows for (int i = 1; i <= 8; i++) { digitalWrite(cols[i - 1], LOW); } for (int i = 1; i <= 8; i++) { digitalWrite(rows[i - 1], LOW); } clearLeds(); // Turn off toggling of pin 11 and 3 FrequencyTimer2::disable(); // Set refresh rate (interrupt timeout period) FrequencyTimer2::setPeriod(2000); // Set interrupt routine to be called FrequencyTimer2::setOnOverflow(display);}void clearLeds() { // Clear display array for (int i = 0; i < 8; i++) { for (int j = 0; j < 8; j++) { leds[i][j] = 0; } }}// Interrupt routinevoid display() { digitalWrite(cols[col], LOW); // Turn whole previous column off col++; if (col == 8) { col = 0; } for (int row = 0; row < 8; row++) { if (leds[col][7 - row] == 1) { digitalWrite(rows[行],低);/ /打开这导致其他digitalwrite } {(行[行],高);/ /关闭LED } } digitalwrite(COLS [西],高);//把整列次(等于照明时代)}
步骤16:上传Arduino代码
1。一旦Arduino代码已经编译成功没有任何错误,点击工具> >板:“Arduino:****”并选择Arduino Uno / genuino(除非你使用的是不同的板)。
2。点击“上传代码”(第二按钮在菜单栏的左侧)。这将把当前的程序到Arduino板,控制LED。
我已经强调过,这是你的项目,我强烈建议你要创造你自己的个人风格独特,所以更改代码循环中创建不同的模式,其中0是,1是。
步骤17:谢谢你,请投票!
如果你正在阅读本文,那么你就要结束,希望行使你的艺术和电子技能,使自己的LED矩阵的太阳镜。
谢谢你花时间读这你!我已进入这个“LED竞赛可能的,所以如果你喜欢阅读和/或使它,请为我投票的比赛。
从科技的火星谢谢你!!!
(责任编辑:admin) |