Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <SPI.h>
- #include <Adafruit_GFX.h>
- #include <Adafruit_ST7735.h>
- #include <Adafruit_seesaw.h>
- #include <Adafruit_TFTShield18.h>
- Adafruit_TFTShield18 ss;
- const int w = 20;
- const int h = 16;
- const int initialPopulation = 70;
- byte board[2][w][h];
- byte g = 0;
- #define SD_CS 4
- #define TFT_CS 10
- #define TFT_DC 8
- #define TFT_RST -1
- Adafruit_ST7735 tft = Adafruit_ST7735(TFT_CS, TFT_DC, TFT_RST);
- void setup(void) {
- ss.begin();
- ss.tftReset();
- tft.initR(INITR_GREENTAB);
- tft.fillScreen(0x0000);
- tft.setRotation(3);
- seedRandomGeneration();
- Serial.begin(9600);
- for (int32_t i=TFTSHIELD_BACKLIGHT_OFF; i<TFTSHIELD_BACKLIGHT_ON; i+=100) {
- ss.setBacklight(i);
- delay(1);
- }
- }
- void seedRandomGeneration()
- {
- randomSeed(analogRead(A1));
- for (byte i=0; i < initialPopulation; i++)
- {
- int x = random(w);
- int y = random(h);
- board[g][x][y] = 1;
- }
- }
- void displayBoard()
- {
- tft.fillScreen(0x0000);
- for (int x = 0; x < w; x++)
- {
- for (int y = 0; y < h; y++)
- {
- if (board[g][x][y])
- {
- tft.fillRect(x * 8, y * 8, 8, 8, ST7735_BLUE);
- }
- }
- }
- }
- void loop()
- {
- if (analogRead(A3) < 512)
- {
- displayBoard();
- updateGeneration();
- g = !g;
- }
- delay(7000);
- }
- void updateGeneration()
- {
- for (int x = 0; x < w; x++)
- {
- for (int y = 0; y < h; y++)
- {
- board[!g][x][y] = updateCell(x, y);
- }
- }
- }
- byte updateCell(int x, int y)
- {
- int n = numNeighbors(x, y);
- if (board[g][x][y] == 1)
- {
- if (n < 2) return 0;
- else if (n == 2 || n == 3) return 1;
- else if (n > 3) return 0;
- }
- else
- {
- return (n == 3);
- }
- }
- int numNeighbors(int x, int y)
- {
- int n = 0;
- for (int dx = -1; dx <= 1; dx++)
- {
- for (int dy = -1; dy <= 1; dy++)
- {
- int x1 = x + dx;
- int y1 = y + dy;
- boolean middle = (dx == 0 && dy == 0);
- if (!middle && x1 > 0 && x1 < w && y1 > 0 && y1 < h)
- {
- n += board[g][x1][y1];
- }
- }
- }
- Serial.print(n);
- return n;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement