|
| 1 | +//After Komputerstrukturen 1969/70 by Peter Struycken with MAX7219 |
| 2 | + |
| 3 | +/* |
| 4 | + * dataPin pin on the Arduino where data gets shifted out |
| 5 | + * clockPin pin for the clock |
| 6 | + * csPin pin for selecting the device |
| 7 | + * numDevices maximum number of devices that can be controled |
| 8 | +LedControl(int dataPin, int clkPin, int csPin, int numDevices=1); |
| 9 | +*/ |
| 10 | + |
| 11 | +#include "LedControl.h" //Imports the library |
| 12 | + |
| 13 | +static const int DATA_PIN = 20; |
| 14 | +static const int CLK_PIN = 5; |
| 15 | +static const int CS_PIN = 21; |
| 16 | + |
| 17 | +LedControl lc = LedControl(DATA_PIN, CLK_PIN, CS_PIN, 1); |
| 18 | + |
| 19 | +#define brightness 1 //Values from 1 to 15 to set the brightness |
| 20 | +int analogPin = 0; //analogPin for the random seed data |
| 21 | + |
| 22 | +unsigned char world[8][8]; |
| 23 | +unsigned char state[4][4]; |
| 24 | +int prev_i; |
| 25 | +int prev_j; |
| 26 | + |
| 27 | +void setWorld() |
| 28 | +{ |
| 29 | + for (int i = 0; i < 8; i++) |
| 30 | + for (int j = 0; j < 8; j++) |
| 31 | + world[i][j] = 0; |
| 32 | + |
| 33 | + for (int i = 0; i < 4; i++) |
| 34 | + for (int j = 0; j < 4; j++) |
| 35 | + { |
| 36 | + int i2 = i * 2; |
| 37 | + int j2 = j * 2; |
| 38 | + unsigned char s = state[i][j]; |
| 39 | + if (s == 0) |
| 40 | + world[i2][j2] = 1; |
| 41 | + else if (s == 1) |
| 42 | + world[i2][j2+1] = 1; |
| 43 | + else if (s == 2) |
| 44 | + world[i2+1][j2+1] = 1; |
| 45 | + else |
| 46 | + world[i2+1][j2] = 1; |
| 47 | + } |
| 48 | + for (int i = 0; i < 8; i++) |
| 49 | + { |
| 50 | + int r = 0; |
| 51 | + for (int j = 0; j < 8; j++) |
| 52 | + r = (r << 1) | world[i][j]; |
| 53 | + lc.setColumn(0, 7-i, r); |
| 54 | + } |
| 55 | +} |
| 56 | + |
| 57 | +void setup() { |
| 58 | + lc.shutdown(0, false); |
| 59 | + lc.setIntensity(0, brightness); |
| 60 | + lc.clearDisplay(0); |
| 61 | + randomSeed(analogRead(analogPin)); |
| 62 | + for (int i = 0; i < 4; i++) |
| 63 | + for (int j = 0; j < 4; j++) |
| 64 | + state[i][j] = random(4); |
| 65 | + setWorld(); |
| 66 | + prev_i = 0; |
| 67 | + prev_j = 0; |
| 68 | +} |
| 69 | + |
| 70 | +/*******************************************************************************/ |
| 71 | + |
| 72 | +void loop() |
| 73 | +{ |
| 74 | + delay(300); |
| 75 | + |
| 76 | + int i = random(3); |
| 77 | + if (i >= prev_i) |
| 78 | + i++; |
| 79 | + int j = random(3); |
| 80 | + if (j >= prev_j) |
| 81 | + j++; |
| 82 | + int old_s = state[i][j]; |
| 83 | + state[i][j] = (old_s + (random(2) ? 1 : 3)) % 4; |
| 84 | + for (int i = 0; i < 3; i++) |
| 85 | + for (int j = 0; j < 3; j++) |
| 86 | + if ( state[i][j] == 2 && state[i][j+1] == 3 |
| 87 | + && state[i+1][j+1] == 0 && state[i+1][j] == 1) |
| 88 | + { |
| 89 | + state[i][j] = old_s; |
| 90 | + return; |
| 91 | + } |
| 92 | + setWorld(); |
| 93 | + delay(1000); |
| 94 | + prev_i = i; |
| 95 | + prev_j = j; |
| 96 | + |
| 97 | +} |
0 commit comments