#include <MD_MAX72xx.h>
#define JOYSTICK_VERT_PIN A0
#define JOYSTICK_HORZ_PIN A1
#define JOYSTICK_SEL_PIN 2
#define LED_MATRIX_CS_PIN 10
#define LED_MATRIX_MAX_DEVICES 1
#define LED_MATRIX_ROW 7
#define LED_MATRIX_COLUMN 7
#define DEFAULT_POS_X 4
#define DEFAULT_POS_y 4
struct stPos {
uint8_t x;
uint8_t y;
};
void callbackSel();
MD_MAX72XX mx = MD_MAX72XX(MD_MAX72XX::PAROLA_HW, LED_MATRIX_CS_PIN, LED_MATRIX_MAX_DEVICES);
volatile stPos pos = {.x = DEFAULT_POS_X, .y = DEFAULT_POS_y};
void setup() {
pinMode(JOYSTICK_VERT_PIN, INPUT);
pinMode(JOYSTICK_HORZ_PIN, INPUT);
pinMode(JOYSTICK_SEL_PIN, INPUT_PULLUP);
attachInterrupt(digitalPinToInterrupt(JOYSTICK_SEL_PIN), callbackSel, FALLING);
mx.begin();
mx.control(MD_MAX72XX::INTENSITY, MAX_INTENSITY / 2);
mx.clear();
}
void loop() {
uint16_t horz = analogRead(JOYSTICK_HORZ_PIN);
uint16_t vert = analogRead(JOYSTICK_VERT_PIN);
if (vert < 300) {
pos.y = min(pos.y + 1, LED_MATRIX_COLUMN);
}
if (vert > 700) {
pos.y = max(pos.y - 1, 0);
}
if (horz > 700) {
pos.x = min(pos.x + 1, LED_MATRIX_ROW);
}
if (horz < 300) {
pos.x = max(pos.x - 1, 0);
}
mx.setPoint(pos.y, pos.x, true);
mx.update();
delay(200);
}
void callbackSel() {
mx.clear();
pos.x = DEFAULT_POS_X;
pos.y = DEFAULT_POS_y;
}