#include <MD_MAX72xx.h>
#include <MD_Parola.h>
#define HARDWARE_TYPE MD_MAX72XX::FC16_HW
#define CS_PIN 10
#define MAX_DEVICES 2
#define DIN_PIN 11
#define CLOCK_PIN 13
// Create a display object for Parola
MD_Parola mx = MD_Parola(MD_MAX72XX::PAROLA_HW, CS_PIN, MAX_DEVICES);
textEffect_t texteffect[] = {
PA_SCROLL_LEFT, PA_SCROLL_RIGHT, PA_SCROLL_UP, PA_SCROLL_DOWN,
};
// Create a display object for controlling individual LEDs
MD_MAX72XX mm = MD_MAX72XX(HARDWARE_TYPE, CS_PIN, MAX_DEVICES);
const int rows = 8;
const int columns = 16;
int grid[rows][columns];
void initializeGrid() {
int litLEDs = 0;
for (int row = 0; row < rows; row++) {
for (int col = 0; col < columns; col++) {
// Assign all cells to dead (0)
grid[row][col] = 0;
}
}
while (litLEDs < 6) {
int row = random(rows); // Random row index
int col = random(columns); // Random col index
if (grid[row][col] == 0) {
grid[row][col] = 1; // Light up this LED
litLEDs++; // Increment the counter
}
}
}
void setup() {
Serial.begin(9600);
// Initialize randomness
randomSeed(analogRead(A0));
// Initialize the Parola display
mx.begin();
mx.displayClear();
mx.setTextAlignment(PA_CENTER);
mx.setSpeed(100);
mx.setPause(1000);
// Initialize the MD_MAX72XX display
mm.begin();
mm.clear();
// Initialize the grid with random values
initializeGrid();
// Display the grid pattern
displayGrid();
}
void loop() {
// Handle text animation (this part can be customized or removed)
if (mx.displayAnimate()) {
// Uncomment to display text on the Parola display
// mx.displayText("WELCOME", mx.getTextAlignment(), mx.getSpeed(), mx.getPause(), texteffect[0], texteffect[2]);
}
updateGrid();
delay(500);
displayGrid();
delay(500);
}
void displayGrid() {
// Clear the Parola display before rendering the grid
mx.displayClear();
// Loop through the grid and turn on/off LEDs based on the grid's state
for (int row = 0; row < rows; row++) {
for (int col = 0; col < columns; col++) {
if (grid[row][col] == 1) {
// Print row and col values for debugging
Serial.print("Row: ");
Serial.print(row);
Serial.print(", Col: ");
Serial.println(col);
// Turn on the LED at (row, col) on the MD_MAX72XX display
mm.setPoint(row, col, true);
}
}
}
}
int countAliveNeighbours(int row, int col) // row and col are the peramiters
// peramiters are the values pased as an input to the function
// example row = 4 and col = 5
// we have to check row number 3, 4 and 5
{
int count = 0;
for (int i = -1; i <= 1; i++) {
for (int j = -1; j <= 1; j++) {
if (i == 0 && j == 0) {
// we are looking at the cell, we are curently counting the neighbours for
continue;
}
else {
int newRow = row+i;
int newCol = col+j;
if (newRow >= 0 && newRow < rows && newCol >= 0 && newCol < columns) {
count+=grid[newRow][newCol];
}
}
}
}
return count;
}
void updateGrid() {
int tempGrid[rows][columns];
for (int row = 0; row < rows; row++) {
for (int col = 0; col < columns; col++){
int aliveNeighbours = countAliveNeighbours(row, col);
Serial.println(aliveNeighbours);
if (grid[row][col] == 1) {
if (aliveNeighbours < 2 || aliveNeighbours > 3 ) {
tempGrid[row][col] = 0;
}
else {
tempGrid[row][col] = 1;
}
}
else {
if (aliveNeighbours == 3) {
tempGrid[row][col] = 1;
}
}
}
}
// copy the tempGrid to og grid
for (int row = 0; row < rows; row++) {
for (int col = 0; col < columns; col++){
grid[row][col] = tempGrid[row][col];
}
}
}