#include <MD_MAX72xx.h>
#define HARDWARE_TYPE MD_MAX72XX::PAROLA_HW
#define MAX_DEVICES 2
#define DATA_PIN 2
#define CS_PIN 3
#define CLK_PIN 4
MD_MAX72XX mx = MD_MAX72XX(HARDWARE_TYPE, DATA_PIN, CLK_PIN, CS_PIN, MAX_DEVICES);
int bally = 0;
int ballx = 0;
int xDir = 0;
int yDir = 0;
int speed = 500;
long time = 0;
void setup()
{
Serial.begin(115200);
pinMode(5, INPUT);
mx.begin();
mx.clear();
}
void threePoint(int x, int y)
{
mx.setPoint(x, y, true);
if (x == 7 && y == 0)
{
mx.setPoint(7, 1, true);
mx.setPoint(6, 0, true);
}
if (x == 0 && y == 0)
{
mx.setPoint(1, 0, true);
mx.setPoint(0, 1, true);
}
if (x == 0 && y == 7)
{
mx.setPoint(0, 6, true);
mx.setPoint(1, 7, true);
}
if (x == 7 && y == 7)
{
mx.setPoint(7, 6, true);
mx.setPoint(6, 7, true);
}
if ((x < 7 && x > 0) && (y == 0 || y == 7))
{
mx.setPoint(x - 1, y, true);
mx.setPoint(x + 1, y, true);
}
if ((y < 7 && y > 0) && (x == 0 || x == 7))
{
mx.setPoint(x, y - 1, true);
mx.setPoint(x, y + 1, true);
}
}
void loop()
{
mx.setPoint(bally, ballx, true);
threePoint(0, 3);
if (millis() - time > speed)
{
mx.setPoint(bally, ballx, false);
if (ballx == 7 && bally == 7)
{
xDir = -1;
yDir = 0;
}
if (ballx == 7 && bally == 0)
{
xDir = 0;
yDir = 1;
}
if (ballx == 0 && bally == 7)
{
xDir = 0;
yDir = -1;
}
if (ballx == 0 && bally == 0)
{
xDir = 1;
yDir = 0;
}
ballx += xDir;
bally += yDir;
time = millis();
}
}