#include #include #include #define SDA_PIN 6 #define SCL_PIN 7 #define SCREEN_WIDTH 128 #define SCREEN_HEIGHT 64 Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1); #define W 128 #define H 64 uint8_t grid[W][H]; uint8_t newGrid[W][H]; void setup() { Wire.begin(SDA_PIN, SCL_PIN); if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { while (1); } display.clearDisplay(); randomSeed(analogRead(0)); for (int x = 0; x < W; x++) { for (int y = 0; y < H; y++) { grid[x][y] = random(2); // 0 or 1 } } } int countNeighbors(int x, int y) { int count = 0; for (int dx = -1; dx <= 1; dx++) { for (int dy = -1; dy <= 1; dy++) { if (dx == 0 && dy == 0) continue; int nx = (x + dx + W) % W; int ny = (y + dy + H) % H; count += grid[nx][ny]; } } return count; } void loop() { for (int x = 0; x < W; x++) { for (int y = 0; y < H; y++) { int neighbors = countNeighbors(x, y); if (grid[x][y] == 1) { // Alive if (neighbors < 2 || neighbors > 3) newGrid[x][y] = 0; else newGrid[x][y] = 1; } else { // Dead if (neighbors == 3) newGrid[x][y] = 1; else newGrid[x][y] = 0; } } } for (int x = 0; x < W; x++) { for (int y = 0; y < H; y++) { grid[x][y] = newGrid[x][y]; } } display.clearDisplay(); for (int x = 0; x < W; x++) { for (int y = 0; y < H; y++) { if (grid[x][y]) { display.drawPixel(x, y, SSD1306_WHITE); } } } display.display(); delay(50); }