#include <GyverMAX7219.h>
#include <Wire.h>
#include <Adafruit_MPU6050.h>
#include <Adafruit_Sensor.h>
#define SIZE 24
#define BORDER SIZE - 2
MAX7219<3, 3, 5> display;
Adafruit_MPU6050 mpu;
sensors_event_t g;
struct Speed {
int8_t x;
int8_t y;
};
struct Point {
int8_t x;
int8_t y;
Speed speed;
};
void drawPoint(Point &p, bool value) {
display.dot(p.x, p.y, value);
display.dot(p.x + 1, p.y, value);
display.dot(p.x, p.y + 1, value);
display.dot(p.x + 1, p.y + 1, value);
display.update();
}
void updatePos(Point &p) {
drawPoint(p, false);
p.x += p.speed.x;
p.y += p.speed.y;
if (p.x > BORDER) p.x = BORDER;
if (p.x < 0) p.x = 0;
if (p.y > BORDER) p.y = BORDER;
if (p.y < 0) p.y = 0;
drawPoint(p, true);
}
void updateSpeed(Point &p) {
mpu.getGyroSensor()->getEvent(&g);
if (g.gyro.x > 1)
p.speed.x = 1;
else if (g.gyro.x < -1)
p.speed.x = -1;
else
p.speed.x = 0;
if (g.gyro.y > 1)
p.speed.y = 1;
else if (g.gyro.y < -1)
p.speed.y = -1;
else
p.speed.y = 0;
}
Point ball = {(SIZE - 1) / 2, (SIZE - 1) / 2, {-1, 0}};
void setup() {
Serial.begin(115200);
display.begin(); // Инициализация матрицы
display.setBright(5); // Яркость 0..15
display.setFlip(true, false); // зеркальное отражение МАТРИЦ (8x8) по x и y
if (!mpu.begin()) {
Serial.println("MPU6050 не найден!");
while (1) delay(10);
}
}
void loop() {
updateSpeed(ball);
updatePos(ball);
delay(500);
}