Guest User

Untitled

a guest
Jul 4th, 2012
146
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.24 KB | None | 0 0
  1. #include <Adafruit_GFX.h>
  2. #include <Adafruit_PCD8544.h>
  3.  
  4. Adafruit_PCD8544 display = Adafruit_PCD8544(3, 4, 5, 7, 6);
  5.  
  6. byte e; //represents the X axis in for loops [life(),dispx(),start()]
  7. byte f; //represents the y axis in for loops [life(),dispx(),start()]
  8. byte u; //accumulator to reset the board [loop()]
  9. byte d; //accumulator determines number of living neighbors for each cell [life()]
  10. byte x=48; //X axis [life(),dispx(),start()]
  11. byte y=48; //Y axis [life(),dispx(),start()]
  12. byte xm=x-1; //boundery [life()]
  13. byte ym=y-1; //boundery [life()]
  14. byte a[48][48]; //array for current board [life(),dispx(),start()]
  15. byte b[48][48]; //array for next board [life()]
  16.  
  17.  
  18. void start(); //function initializes the current board
  19. void dispx(); //function displays to graphic LCD
  20. void life(); //function is Conway's Game of Life
  21.  
  22. void setup()
  23. {
  24.   display.begin();
  25.   display.setContrast(50);
  26.   display.fillRect(0,0,84,48,BLACK); //Black margins
  27. }
  28.  
  29. void loop()
  30. {
  31.   start();
  32.   for (u=0;u<200;u++) //avoid stagnation
  33.   {
  34.     dispx();
  35.     life();
  36.   }
  37. }
  38.  
  39. void life()
  40. {
  41.   for (e=0;e<x;e++)
  42.   {
  43.     for (f=0;f<y;f++)
  44.     {
  45.       d=0;
  46.       if (a[e-1][f]==1 && e!=0)
  47.       d++;
  48.       if (a[e-1][f-1]==1 && e!=0 && f!=0)
  49.       d++;
  50.       if (a[e-1][f+1]==1 && e!=0 && f!=ym)
  51.       d++;
  52.       if (a[e+1][f]==1 && e!=xm)
  53.       d++;
  54.       if (a[e+1][f-1]==1 && e!=xm && f!=0)
  55.       d++;
  56.       if (a[e+1][f+1]==1 && e!=xm && f!=ym)
  57.       d++;
  58.       if (a[e][f-1]==1 && f!=0)
  59.       d++;
  60.       if (a[e][f+1]==1 && f!=ym)
  61.       d++;
  62.       if (a[e][f]==1)
  63.       {
  64.         if (d<2 || d>3)
  65.         b[e][f]=0;
  66.         if (d==2 || d==3)
  67.         b[e][f]=1;
  68.       }
  69.       if (a[e][f]==0)
  70.       if (d==3)
  71.       b[e][f]=1;
  72.     }
  73.   }
  74.   for (e=0;e<x;e++)
  75.   for (f=0;f<y;f++)
  76.   a[e][f]=b[e][f];
  77. }
  78.  
  79. void start()
  80. {
  81.   for (e=0;e<x;e++)
  82.   {
  83.     for (f=0;f<y;f++)
  84.     {
  85.       a[e][f]=random(0,2);
  86.     }
  87.   }
  88. }
  89.  
  90. void dispx()
  91. {
  92.   for (e=0;e<x;e++)
  93.   {
  94.     for (f=0;f<y;f++)
  95.     {
  96.       if (a[e][f]==1)
  97.       {
  98.         display.drawPixel(e+18,f,BLACK); //+18 to have even margins on either side
  99.        }
  100.       if (a[e][f]==0)
  101.       {
  102.         display.drawPixel(e+18,f,WHITE);
  103.       }
  104.     }
  105.   }  
  106.   display.display();
  107.   //delay(500);
  108. }
Advertisement
Add Comment
Please, Sign In to add comment