Guest User

Andrew

a guest
Feb 15th, 2008
4,492
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.57 KB | None | 0 0
  1. /*
  2.  * Conway's "Life"
  3.  *
  4.  * Adapted from the Life example
  5.  * on the Processing.org site
  6.  */
  7.  
  8. #include <FrequencyTimer2.h>
  9.  
  10. byte analogPin = 5;
  11. byte col = 0;
  12. byte leds[8][8];
  13.  
  14. // pin[xx] on led matrix connected to nn on Arduino (-1 is dummy to make array start at pos 1)
  15. int pins[17]= {-1, 5, 4, 3, 2, 14, 15, 16, 17, 13, 12, 11, 10, 9, 8, 7, 6};
  16.  
  17. // col[xx] of leds = pin yy on led matrix
  18. int cols[8] = {pins[13], pins[3], pins[4], pins[10], pins[06], pins[11], pins[15], pins[16]};
  19.  
  20. // row[xx] of leds = pin yy on led matrix
  21. int rows[8] = {pins[9], pins[14], pins[8], pins[12], pins[1], pins[7], pins[2], pins[5]};
  22.  
  23. #define DELAY 0
  24. #define SIZE 8
  25. extern byte leds[SIZE][SIZE];
  26. byte world[SIZE][SIZE][2];
  27. long density = 50;
  28.  
  29. void setup() {
  30.   setupLeds();
  31.   randomSeed(analogRead(5));
  32.   for (int i = 0; i < SIZE; i++) {
  33.     for (int j = 0; j < SIZE; j++) {
  34.       if (random(100) < density) {
  35.         world[i][j][0] = 1;
  36.       }
  37.       else {
  38.         world[i][j][0] = 0;
  39.       }
  40.       world[i][j][1] = 0;
  41.     }
  42.   }
  43. }
  44.  
  45. void loop() {
  46.   // Display current generation
  47.   for (int i = 0; i < SIZE; i++) {
  48.     for (int j = 0; j < SIZE; j++) {
  49.       leds[i][j] = world[i][j][0];
  50.     }
  51.   }
  52.   delay(DELAY);
  53.  
  54.   // Birth and death cycle
  55.   for (int x = 0; x < SIZE; x++) {
  56.     for (int y = 0; y < SIZE; y++) {
  57.       // Default is for cell to stay the same
  58.       world[x][y][1] = world[x][y][0];
  59.       int count = neighbours(x, y);
  60.       if (count == 3 && world[x][y][0] == 0) {
  61.         // A new cell is born
  62.         world[x][y][1] = 1;
  63.       }
  64.       if ((count < 2 || count > 3) && world[x][y][0] == 1) {
  65.         // Cell dies
  66.         world[x][y][1] = 0;
  67.       }
  68.     }
  69.   }
  70.  
  71.   // Copy next generation into place
  72.   for (int x = 0; x < SIZE; x++) {
  73.     for (int y = 0; y < SIZE; y++) {
  74.       world[x][y][0] = world[x][y][1];
  75.     }
  76.   }
  77. }
  78.  
  79. int neighbours(int x, int y) {
  80.  return world[(x + 1) % SIZE][y][0] +
  81.          world[x][(y + 1) % SIZE][0] +
  82.          world[(x + SIZE - 1) % SIZE][y][0] +
  83.          world[x][(y + SIZE - 1) % SIZE][0] +
  84.          world[(x + 1) % SIZE][(y + 1) % SIZE][0] +
  85.          world[(x + SIZE - 1) % SIZE][(y + 1) % SIZE][0] +
  86.          world[(x + SIZE - 1) % SIZE][(y + SIZE - 1) % SIZE][0] +
  87.          world[(x + 1) % SIZE][(y + SIZE - 1) % SIZE][0];
  88. }
  89.  
  90. void setupLeds() {
  91.   // sets the pins as output
  92.   for (int i = 1; i <= 16; i++) {
  93.     pinMode(pins[i], OUTPUT);
  94.   }
  95.  
  96.   // set up cols and rows
  97.   for (int i = 1; i <= 8; i++) {
  98.     digitalWrite(cols[i - 1], LOW);
  99.   }
  100.  
  101.   for (int i = 1; i <= 8; i++) {
  102.     digitalWrite(rows[i - 1], LOW);
  103.   }
  104.  
  105.   clearLeds();
  106.  
  107.   // Turn off toggling of pin 11
  108.   FrequencyTimer2::disable();
  109.   // Set refresh rate (interrupt timeout period)
  110.   FrequencyTimer2::setPeriod(2000);
  111.   // Set interrupt routine to be called
  112.   FrequencyTimer2::setOnOverflow(display);
  113.  
  114. }
  115.  
  116. void clearLeds() {
  117.   // Clear display array
  118.   for (int i = 0; i < 8; i++) {
  119.     for (int j = 0; j < 8; j++) {
  120.       leds[i][j] = 0;
  121.     }
  122.   }
  123. }
  124.  
  125. // Interrupt routine
  126. void display() {
  127.   digitalWrite(cols[col], LOW);  // Turn whole previous column off
  128.   col++;
  129.   if (col == 8) {
  130.     col = 0;
  131.   }
  132.   for (int row = 0; row < 8; row++) {
  133.     if (leds[col][7 - row] == 1) {
  134.       digitalWrite(rows[row], LOW);  // Turn on this led
  135.     }
  136.     else {
  137.       digitalWrite(rows[row], HIGH); // Turn off this led
  138.     }
  139.   }
  140.   digitalWrite(cols[col], HIGH); // Turn whole column on at once (for equal lighting times)
  141.   delayMicroseconds(900);  // Delay so that on times are longer than off time = brighter leds
  142. }
Advertisement
Add Comment
Please, Sign In to add comment