Devices & Components
Arduino Nano
Breadboard - 400 contacts
MAX7219 8x8 LED matrix
Software & Tools
Arduino IDE
Project description
Code
source
cpp
An additional library required
1#include <GyverMAX7219.h> 2#define ARRAY_SIZE 64 3 4MAX7219 < 1, 1, 5 > mtrx; 5 6bool current[ARRAY_SIZE] = {}; 7bool next[ARRAY_SIZE] = {}; 8 9void setup() 10{ 11 mtrx.begin(); 12 mtrx.setBright(0); 13 make_random(32); 14} 15 16void make_random(int cells) 17{ 18 randomSeed(analogRead(0)); 19 int alive = 0; 20 while(alive < cells) 21 { 22 int x = random(0, 8); 23 int y = random(0, 8); 24 if(!current[x * 8 + y]) 25 { 26 make_live(x, y); 27 alive++; 28 } 29 } 30} 31 32void make_glider(int pos_x, int pos_y) 33{ 34 make_live(pos_x, pos_y); 35 make_live(pos_x + 1, pos_y); 36 make_live(pos_x + 1, pos_y + 1); 37 make_live(pos_x + 2, pos_y + 1); 38 make_live(pos_x, pos_y + 2); 39} 40 41void make_live(int x, int y) 42{ 43 current[y * 8 + x] = true; 44} 45 46void display() 47{ 48 mtrx.clear(); 49 for(int i = 0; i < 8; i++) 50 { 51 for(int j = 0; j < 8; j++) 52 { 53 if(current[i * 8 + j]) 54 mtrx.dot(i, j); 55 } 56 } 57 mtrx.update(); 58} 59 60void step() 61{ 62 memset(next, 0, sizeof(next)); 63 for(int i = 0; i < 8; i++) 64 { 65 for(int j = 0; j < 8; j++) 66 { 67 int neighbours = get_neighbours_count(i, j); 68 if(neighbours == 2 && current[i * 8 + j] || neighbours == 3) 69 next[i * 8 + j] = true; 70 } 71 } 72} 73 74int get_neighbours_count(int x, int y) 75{ 76 int count = 0; 77 for(int dx = -1; dx <= 1; dx++) 78 { 79 for(int dy = -1; dy <= 1; dy++) 80 { 81 if(dx == 0 && dy == 0) 82 continue; 83 int u = (x + dx + 8) % 8; 84 int v = (y + dy + 8) % 8; 85 if(current[u * 8 + v]) 86 count++; 87 } 88 } 89 return count; 90} 91 92void loop() 93{ 94 display(); 95 step(); 96 memcpy(current, next, sizeof(next)); 97 delay(500); 98}
Documentation
Scheme of the project
Try it your own
maxsch.png

Comments
Only logged in users can leave comments