#include <MD_MAX72xx.h>
#include <ezButton.h>
// matrix controls
const byte data_pin = 11;
const byte chip_select_pin = 10;
const byte clock_pin = 13;
const byte max_devices = 4;
int headY = 0;
int headX = 0;
String direction = "", prevDirection = "";
int maxX = 31;
int maxY = 7;
ezButton button (8);
int flag = 0;
// creating matrix object
MD_MAX72XX matrix = MD_MAX72XX(MD_MAX72XX::PAROLA_HW, chip_select_pin, max_devices);
// define the joystick controls
const byte hpin = A0;
const byte vpin = A1;
void setup(){
Serial.begin(9600);
matrix.begin();
matrix.clear();
button.setDebounceTime(25);
matrix.setPoint(headX,headY,true);
}
void loop(){
// put your main code here, to run repeatedly:
button.loop();
if (flag==0){
checkDirection();
moveSprite();
}
matrix.setPoint(headY,headX,true);
}
void checkDirection(){
if (analogRead(hpin)>512&&prevDirection!="right"){
direction = "left";
}
else if (analogRead(hpin)<512&&prevDirection!="left"){
direction = "right";
}
else if (analogRead(vpin)>512&&prevDirection!="down"){
direction = "up";
}
else if (analogRead(vpin)<512&&prevDirection!="up"){
direction = "down";
}
prevDirection = direction;
}
void moveSprite(){
if (direction=="left") headX++;
else if (direction=="right") headX--;
else if (direction=="up") headY--;
else if (direction=="down") headY++;
windowCheck();
if (button.isPressed()){
Serial.println("The Button is Pressed");
flag = 1;
}
}
void windowCheck(){
if (headX>maxX) headX=0;
else if (headX<0) headX=maxX;
else if (headY>maxY) headY=0;
else if (headY<0) headY=maxY;
}