[ad_1]
Downside when altering MAX_X from 8 to 24. I do know that the issue is that I’m utilizing to many international variables however I do not know the right way to repair it.
#embrace <LedControl.h>
#embrace "LedControl.h"
// "DIN" information in pin
#outline DIN_PIN 12
// "CLK" clock pin
#outline CLK_PIN 11
// "CS" pin
#outline CS_PIN 10
// grid dimensions. shouldn't be bigger than 8x8
#outline MAX_Y 8
#outline MAX_X 24
// what number of customized beginning grid patterns
#outline MAX_C 15
// time to attend between turns
#outline TURN_DELAY 100
// what number of turns per recreation earlier than beginning a brand new recreation
// you may also use the reset button on the board
#outline TURNS_MAX 40
// what number of turns to attend if there are not any modifications earlier than beginning a brand new recreation
#outline NO_CHANGES_RESET 4
int TURNS = 0; // counter for turns
int NO_CHANGES = 0; // counter for turns with out modifications
// recreation state. 0 is useless cell, 1 is dwell cell
boolean grid[MAX_Y][MAX_X] = {
{0, 0, 0, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 0, 0, 0},
};
// customized beginning grid patterns
boolean cgrids[MAX_C][MAX_Y][MAX_X] = {
{
{1, 1, 0, 0, 1, 1, 0, 0},
{1, 1, 0, 0, 1, 1, 0, 0},
{0, 0, 0, 0, 0, 0, 1, 1},
{0, 0, 0, 0, 0, 0, 1, 1},
{1, 1, 0, 0, 0, 0, 0, 0},
{1, 1, 0, 0, 0, 0, 0, 0},
{0, 0, 1, 1, 0, 0, 1, 1},
{0, 0, 1, 1, 0, 0, 1, 1},
},
{
{1, 0, 1, 0, 0, 0, 1, 0},
{0, 1, 1, 0, 0, 0, 1, 0},
{0, 1, 0, 0, 0, 0, 1, 0},
{0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0},
{0, 1, 0,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0, 0, 0, 0, 0, 0, 0, 0},
{1, 1, 0, 0, 1, 1, 1, 0},
{1, 0, 1, 0, 1, 0, 0, 0},
{0, 1, 1, 0, 0, 0, 0, 0},
},
{
{0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 1, 0, 0, 0, 0},
{0, 0, 1, 1, 0, 0, 0, 0},
{0, 1, 0, 0, 0, 0, 0, 0},
{0, 0, 1, 1, 0, 0, 0, 0},
{0, 0, 0, 1, 0, 0, 0, 0},
},
{
{0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0},
{0, 1, 1, 0, 0, 1, 1, 0},
{0, 1, 1, 0, 0, 1, 1, 0},
{0, 1, 1, 0, 0, 1, 1, 0},
{0, 1, 1, 0, 0, 1, 1, 0},
{0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0},
},
{
{1, 0, 0, 0, 0, 1, 0, 0},
{0, 1, 0, 0, 0, 0, 1, 0},
{0, 0, 1, 0, 0, 1, 0, 1},
{0, 0, 0, 1, 0, 0, 0, 0},
{0, 0, 0, 0, 1, 0, 0, 0},
{1, 0, 1, 0, 0, 1, 0, 0},
{0, 1, 0, 0, 0, 0, 1, 0},
{0, 0, 1, 0, 0, 0, 0, 1},
},
};
LedControl lc = LedControl(DIN_PIN, CS_PIN, CLK_PIN, 1);
void setup() {
// seed random from unused analog pin
randomSeed(analogRead(0));
// initialise the LED matrix
lc.shutdown(0, false);
lc.setIntensity(0, 0);
lc.clearDisplay(0);
reset_grid();
display_grid();
// Serial.start(9600);
// debug_grid();
}
void loop() {
delay(TURN_DELAY);
play_gol();
TURNS++;
// reset the grid if no modifications have occured lately
// for when the sport enters a static secure state
if (NO_CHANGES > NO_CHANGES_RESET) {
reset_grid();
}
// reset the grid if the loop has been working a very long time
// for when the sport cycles between just a few secure states
if (TURNS > TURNS_MAX) {
reset_grid();
}
display_grid();
}
// play recreation of life
void play_gol() {
/*
1. Any dwell cell with fewer than two neighbours dies, as if by loneliness.
2. Any dwell cell with greater than three neighbours dies, as if by
overcrowding.
3. Any dwell cell with two or three neighbours lives, unchanged, to the following
era.
4. Any useless cell with precisely three neighbours involves life.
*/
boolean new_grid[MAX_Y][MAX_X] = {
{0, 0, 0, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 0, 0, 0},
};
for (int y = 0; y < 8; y++) {
for (int x = 0; x < 8; x++) {
int neighboughs = count_neighboughs(y, x);
if (grid[y][x] == 1) {
if ((neighboughs == 2) || (neighboughs == 3)) {
new_grid[y][x] = 1;
} else {
new_grid[y][x] = 0;
}
} else {
if (neighboughs == 3) {
new_grid[y][x] = 1;
} else {
new_grid[y][x] = 0;
}
}
}
}
// replace the present grid from the brand new grid and depend what number of modifications
// occured
int modifications = 0;
for (int y = 0; y < MAX_Y; y++) {
for (int x = 0; x < MAX_X; x++) {
if (new_grid[y][x] != grid[y][x]) {
modifications++;
}
grid[y][x] = new_grid[y][x];
}
}
// replace international counter when no modifications occured
if (modifications == 0) {
NO_CHANGES++;
}
}
// depend the variety of neighbough dwell cells for a given cell
int count_neighboughs(int y, int x) {
int depend = 0;
// -- Row above us ---
if (y > 0) {
// above left
if (x > 0) {
depend += grid[y - 1][x - 1];
}
// above
depend += grid[y - 1][x];
// above proper
if ((x + 1) < 8) {
depend += grid[y - 1][x + 1];
}
}
// -- Similar row -------
// left
if (x > 0) {
depend += grid[y][x - 1];
}
// proper
if ((x + 1) < 8) {
depend += grid[y][x + 1];
}
// -- Row beneath us ---
if ((y + 1) < 8) {
// beneath left
if (x > 0) {
depend += grid[y + 1][x - 1];
}
// beneath
depend += grid[y + 1][x];
// beneath proper
if ((x + 1) < 8) {
depend += grid[y + 1][x + 1];
}
}
return depend;
}
// reset the grid
void reset_grid() {
NO_CHANGES = 0;
TURNS = 0;
int grid_type = random(0, 4);
int custom_grid_choice = random(0, MAX_C - 1);
for (int y = 0; y < MAX_Y; y++) {
for (int x = 0; x < MAX_X; x++) {
if (grid_type == 0) {
// use a customized beginning grid sample
grid[y][x] = cgrids[custom_grid_choice][y][x];
} else {
// create a random beginning grid sample
if (random(0, MAX_X) <= 1) {
grid[y][x] = 1;
}
}
}
}
}
// show the present grid to the LED matrix
void display_grid() {
for (int y = 0; y < MAX_Y; y++) {
for (int x = 0; x < MAX_X; x++) {
lc.setLed(0, y, x, grid[y][x]);
}
}
}
/*
// dump the state of the present grid to the serial connection
void debug_grid() {
for (int y = 0; y < MAX_Y; y++) {
Serial.print("y(");
Serial.print(y);
Serial.print("): ");
for (int x = 0; x < MAX_X; x++) {
Serial.print(grid[y][x]);
Serial.print(", ");
}
Serial.println("");
}
Serial.println("");
}
*/
[ad_2]