Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <Adafruit_GFX.h>
- #include <Adafruit_PCD8544.h>
- Adafruit_PCD8544 display = Adafruit_PCD8544(3, 4, 5, 7, 6);
- byte e; //represents the X axis in for loops [life(),dispx(),start()]
- byte f; //represents the y axis in for loops [life(),dispx(),start()]
- byte u; //accumulator to reset the board [loop()]
- byte d; //accumulator determines number of living neighbors for each cell [life()]
- byte x=48; //X axis [life(),dispx(),start()]
- byte y=48; //Y axis [life(),dispx(),start()]
- byte xm=x-1; //boundery [life()]
- byte ym=y-1; //boundery [life()]
- byte a[48][48]; //array for current board [life(),dispx(),start()]
- byte b[48][48]; //array for next board [life()]
- void start(); //function initializes the current board
- void dispx(); //function displays to graphic LCD
- void life(); //function is Conway's Game of Life
- void setup()
- {
- display.begin();
- display.setContrast(50);
- display.fillRect(0,0,84,48,BLACK); //Black margins
- }
- void loop()
- {
- start();
- for (u=0;u<200;u++) //avoid stagnation
- {
- dispx();
- life();
- }
- }
- void life()
- {
- for (e=0;e<x;e++)
- {
- for (f=0;f<y;f++)
- {
- d=0;
- if (a[e-1][f]==1 && e!=0)
- d++;
- if (a[e-1][f-1]==1 && e!=0 && f!=0)
- d++;
- if (a[e-1][f+1]==1 && e!=0 && f!=ym)
- d++;
- if (a[e+1][f]==1 && e!=xm)
- d++;
- if (a[e+1][f-1]==1 && e!=xm && f!=0)
- d++;
- if (a[e+1][f+1]==1 && e!=xm && f!=ym)
- d++;
- if (a[e][f-1]==1 && f!=0)
- d++;
- if (a[e][f+1]==1 && f!=ym)
- d++;
- if (a[e][f]==1)
- {
- if (d<2 || d>3)
- b[e][f]=0;
- if (d==2 || d==3)
- b[e][f]=1;
- }
- if (a[e][f]==0)
- if (d==3)
- b[e][f]=1;
- }
- }
- for (e=0;e<x;e++)
- for (f=0;f<y;f++)
- a[e][f]=b[e][f];
- }
- void start()
- {
- for (e=0;e<x;e++)
- {
- for (f=0;f<y;f++)
- {
- a[e][f]=random(0,2);
- }
- }
- }
- void dispx()
- {
- for (e=0;e<x;e++)
- {
- for (f=0;f<y;f++)
- {
- if (a[e][f]==1)
- {
- display.drawPixel(e+18,f,BLACK); //+18 to have even margins on either side
- }
- if (a[e][f]==0)
- {
- display.drawPixel(e+18,f,WHITE);
- }
- }
- }
- display.display();
- //delay(500);
- }
Advertisement
Add Comment
Please, Sign In to add comment