#include <MD_MAX72xx.h>
#include <SPI.h>
#define HARDWARE_TYPE MD_MAX72XX::PAROLA_HW
#define MAX_DEVICES 1
#define CS_PIN 10
#define DATA_PIN 11
#define CLK_PIN 13
MD_MAX72XX matrix = MD_MAX72XX(HARDWARE_TYPE, CS_PIN, MAX_DEVICES);
#define ENC_A 5 // CLK
#define ENC_B 6 // DT
#define ENC_BTN 7 // SW
int x = 0;
int y = 0;
int axis = 0;
int lastEncA = HIGH;
int lastEncBtn = HIGH;
unsigned long lastDebounceTime = 0;
unsigned long debounceDelay = 50;
void setup() {
matrix.begin();
matrix.clear();
pinMode(ENC_A, INPUT_PULLUP);
pinMode(ENC_B, INPUT_PULLUP);
pinMode(ENC_BTN, INPUT_PULLUP);
lastEncA = digitalRead(ENC_A);
lastEncBtn = digitalRead(ENC_BTN);
}
void loop() {
int encA = digitalRead(ENC_A);
int encB = digitalRead(ENC_B);
if (encA != lastEncA) {
if (encB != lastEncA) {
movePixel(1);
} else {
movePixel(-1);
}
lastEncA = encA;
}
int encBtn = digitalRead(ENC_BTN);
if (encBtn != lastEncBtn) {
if (encBtn == LOW) {
if (millis() - lastDebounceTime > debounceDelay) {
toggleAxis();
lastDebounceTime = millis();
}
}
lastEncBtn = encBtn;
}
matrix.clear();
matrix.setPoint(y, x, true);
delay(10);
}
void movePixel(int direction) {
if (axis == 0) {
if (direction > 0 && x < 7) {
x++;
} else if (direction < 0 && x > 0) {
x--;
}
} else {
if (direction > 0 && y < 7) {
y++;
} else if (direction < 0 && y > 0) {
y--;
}
}
}
void toggleAxis() {
axis = (axis + 1) % 2;
}