// Stacker Game Code
#include <MD_MAX72xx.h>
#include <MD_Parola.h>
#include <SPI.h>
#define HARDWARE_TYPE MD_MAX72XX::PAROLA_HW
#define MAX_DEVICES 4
#define CLK_PIN 13 // or SCK
#define DATA_PIN 11 // or MOSI
#define CS_PIN 10 // or SS
MD_MAX72XX mx = MD_MAX72XX(HARDWARE_TYPE, CS_PIN, MAX_DEVICES);
MD_Parola P = MD_Parola(HARDWARE_TYPE, CS_PIN, MAX_DEVICES);
MD_MAX72XX::fontType_t MZFStackerFont[] PROGMEM = {
'F', 1, 32, 127, 5,
2, 0, 0, // 32 - 'Space'
6, 24, 24, 36, 102, 102, 66, // 2 - '!'
3, 3, 0, 3, // 34 - '"'
6, 102, 102, 102, 102, 102, 102, // 35 - '#'
6, 126, 126, 0, 0, 126, 126, // 36 - '$'
8, 129, 129, 129, 129, 129, 129, 129, 129, // 37 - '%'
8, 129, 129, 0, 129, 129, 0, 129, 129, // 38 - '&'
1, 3, // 39
2, 60, 66, // 40 - '('
2, 66, 60, // 41 - ')'
3, 10, 4, 10, // 42 - '*'
6, 24, 24, 126, 126, 24, 24, // 43 - '+'
2, 16, 8, // 44 - ','
6, 0, 0, 126, 126, 0, 0, // 45 - '-'
1, 16, // 46 - '.'
3, 8, 4, 2, // 47 - '/'
5, 24, 36, 36, 36, 24, // 17 - '0'
5, 8, 24, 8, 8, 8, // 18 - '1'
5, 60, 4, 60, 32, 60, // 19 - '2'
5, 60, 4, 28, 4, 60, // 20 - '3'
5, 36, 36, 60, 4, 4, // 21 - '4'
5, 60, 32, 60, 4, 60, // 22 - '5'
5, 60, 32, 60, 36, 60, // 23 - '6'
5, 60, 4, 4, 4, 4, // 24 - '7'
5, 60, 36, 60, 36, 60, // 25 - '8'
5, 60, 36, 60, 4, 60, // 26 - '9'
1, 10, // 58 - ':'
2, 16, 10, // 59 - ';'
3, 4, 10, 17, // 60 - '<'
5, 126, 126, 0, 126, 126, // 61 - '='
3, 17, 10, 4, // 62 - '>'
3, 1, 21, 3, // 63 - '?'
6, 66, 102, 102, 36, 24, 24, // 33 - '@'
8, 60, 126, 102, 126, 126, 102, 102, 102, // 34 - 'A'
8, 124, 126, 102, 124, 124, 102, 126, 124, // 35 - 'B'
8, 62, 126, 96, 96, 96, 96, 126, 62, // 36 - 'C'
8, 124, 126, 102, 102, 102, 102, 126, 124, // 37 - 'D'
8, 126, 126, 96, 120, 120, 96, 126, 126, // 38 - 'E'
8, 126, 126, 96, 120, 120, 96, 96, 96, // 39 - 'F'
8, 62, 126, 96, 96, 110, 98, 126, 62, // 40 - 'G'
8, 102, 102, 102, 126, 126, 102, 102, 102, // 41 - 'H'
8, 24, 24, 24, 24, 24, 24, 24, 24, // 42 - 'I'
8, 12, 12, 12, 12, 12, 12, 60, 56, // 43 - 'J'
8, 102, 110, 108, 120, 120, 108, 110, 102, // 44 - 'K'
8, 96, 96, 96, 96, 96, 96, 126, 126, // 45 - 'L'
8, 102, 126, 126, 102, 102, 102, 102, 102, // 46 - 'M'
8, 102, 118, 126, 110, 102, 102, 102, 102, // 47 - 'N'
8, 60, 126, 102, 102, 102, 102, 126, 60, // 48 - 'O'
8, 124, 126, 102, 126, 124, 96, 96, 96, // 49 - 'P'
8, 60, 126, 102, 102, 102, 126, 60, 12, // 50 - 'Q'
8, 124, 126, 102, 124, 124, 102, 102, 102, // 51 - 'R'
8, 126, 126, 96, 124, 62, 6, 126, 126, // 52 - 'S'
8, 126, 126, 24, 24, 24, 24, 24, 24, // 53 - 'T'
8, 102, 102, 102, 102, 102, 102, 126, 60, // 54 - 'U'
8, 102, 102, 102, 102, 102, 102, 60, 24, // 55 - 'V'
8, 102, 102, 102, 102, 102, 126, 126, 102, // 56 - 'W'
8, 66, 102, 102, 60, 60, 102, 102, 66, // 57 - 'X'
8, 102, 102, 102, 60, 24, 24, 24, 24, // 58 - 'Y'
8, 126, 126, 14, 28, 56, 112, 126, 126, // 59 - 'Z'
2, 31, 17, // 91 - '['
3, 2, 4, 8, // 92 - '\'
2, 17, 31, // 93 - ']'
3, 2, 1, 2, // 94 - '^'
3, 16, 16, 16, // 95 - '_'
2, 1, 2, // 96 - '`'
5, 60, 36, 60, 36, 36, // 66 - 'a'
5, 56, 36, 60, 36, 56, // 67 - 'b'
5, 24, 36, 32, 36, 24, // 68 - 'c'
5, 56, 36, 36, 36, 56, // 69 - 'd'
5, 60, 32, 56, 32, 60, // 70 - 'e'
5, 60, 32, 56, 32, 32, // 71 - 'f'
5, 60, 32, 44, 36, 60, // 72 - 'g'
5, 36, 36, 60, 36, 36, // 73 - 'h'
5, 24, 24, 24, 24, 24, // 74 - 'i'
5, 4, 4, 4, 36, 28, // 75 - 'j'
5, 36, 40, 48, 40, 36, // 76 - 'k'
5, 32, 32, 32, 32, 60, // 77 - 'l'
5, 126, 90, 90, 82, 66, // 78 - 'm'
5, 36, 52, 44, 36, 36, // 79 - 'n'
5, 60, 36, 36, 36, 60, // 80 - 'o'
5, 56, 36, 56, 32, 32, // 81 - 'p'
5, 60, 36, 36, 60, 8, // 82 - 'q'
5, 60, 36, 60, 40, 36, // 83 - 'r'
5, 60, 32, 60, 4, 60, // 84 - 's'
5, 60, 24, 24, 24, 24, // 85 - 't'
5, 36, 36, 36, 36, 60, // 86 - 'u'
5, 36, 36, 36, 60, 24, // 87 - 'v'
5, 66, 90, 90, 126, 126, // 88 - 'w'
5, 36, 36, 24, 36, 36, // 89 - 'x'
5, 36, 36, 24, 8, 16, // 90 - 'y'
5, 60, 4, 8, 16, 60, // 91 - 'z'
3, 4, 27, 17, // 123 - '{'
1, 27, // 124 - '|'
3, 17, 27, 4, // 125 - '}'
3, 6, 2, 3, // 126 - '~'
3, 31, 31, 31, // 127 - 'Full Block'
};
bool scrl = 0;
uint8_t scrollDirection = PA_SCROLL_LEFT;
unsigned long previousMillis = 0; // Stores the last time an action was taken
int index = 0; // Index to track the current character to print
void setup()
{
Serial.begin(9600);
Serial.println("STACKER MATRIX MAX 7219");
mx.begin();
P.begin();
P.setFont(MZFStackerFont);
P.setTextAlignment(PA_CENTER);
}
void pingpong(const char* curMessage, uint8_t d) {
P.displayClear();
P.displayScroll(curMessage, PA_CENTER, scrollDirection, d);
P.displayReset(); // Initialize the display with the current scroll direction
}
void scroll(const char* curMessage, uint8_t s, uint8_t d) {
P.displayClear();
switch (s) {
case 0:
P.displayScroll(curMessage, PA_CENTER, PA_SCROLL_LEFT, d);
break;
case 1:
P.displayScroll(curMessage, PA_CENTER, PA_SCROLL_RIGHT, d);
break;
case 2:
P.displayScroll(curMessage, PA_CENTER, PA_SCROLL_UP, d);
break;
case 3:
P.displayScroll(curMessage, PA_CENTER, PA_SCROLL_DOWN, d);
break;
default:
P.displayScroll(curMessage, PA_CENTER, PA_SCROLL_LEFT, d);
break;
}
P.displayReset(); // Initialize the display with the current scroll direction
}
void loop()
{
// P.print("DEFG");
// v_pingpong("coba", 30);
for (int j = -30; j < 40; j++) {
for (int i = 0; i < 10; i++) {
int offset = i * 3; // Menghitung jarak antar pola
// Menggambar pola utama
mx.setPoint(1, j + offset, true);
mx.setPoint(6, j + offset, true);
mx.setPoint(2, j + 1 + offset, true);
mx.setPoint(5, j + 1 + offset, true);
mx.setPoint(3, j + 2 + offset, true);
mx.setPoint(4, j + 2 + offset, true);
// Menghapus jejak yang sudah terlalu lama
if (j - 1 >= -40) {
mx.setPoint(1, j - 1 + offset, false);
mx.setPoint(6, j - 1 + offset, false);
mx.setPoint(2, j - 1 + 1 + offset, false);
mx.setPoint(5, j - 1 + 1 + offset, false);
mx.setPoint(3, j - 1 + 2 + offset, false);
mx.setPoint(4, j - 1 + 2 + offset, false);
}
}
delay(50);
}
// v_scroll("ada",2, 300); //0 atas 1 bawah 2 kanan 3 kiri
// drawBorder();
}
void v_scroll(const char* curMessage, uint8_t s, uint8_t d) {
if (P.displayAnimate()) {
scroll(curMessage, s, d); // Restart scrolling dengan arah baru
}
}
void v_pingpong(const char* curMessage, uint8_t d) {
if (P.displayAnimate()) {
// Ubah arah berdasarkan nilai scrollDirection saat ini
if (scrl == 0) {
scrollDirection = PA_SCROLL_LEFT;
scrl = 1;
} else {
scrollDirection = PA_SCROLL_RIGHT;
scrl = 0;
}
pingpong(curMessage, d); // Restart scrolling dengan arah baru
}
}
void v_1huruf(const char* curMessage, int d) {
unsigned long currentMillis = millis();
// Check if enough time has passed to print the next character
if (currentMillis - previousMillis >= d) {
previousMillis = currentMillis; // Update the last action time
if (index < strlen(curMessage)) {
P.print(curMessage[index]); // Print the current character
index++; // Move to the next character
} else {
P.print(""); // Clear display after the message is printed
index = 0; // Reset the index to restart
previousMillis += 1000; // Add an additional delay after printing all characters
}
}
}
void drawBorder() {
// Top and Bottom edges
for (int x = 0; x < 8; x++) {
mx.setPoint(x, 0, true); // Top row
mx.setPoint(x, 31, true); // Bottom row
}
// Left and Right edges
for (int y = 0; y < 32; y++) {
mx.setPoint(0, y, true); // Left column
mx.setPoint(7, y, true); // Right column
}
}