#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;
// creating matrix object
MD_MAX72XX matrix = MD_MAX72XX(MD_MAX72XX::PAROLA_HW, chip_select_pin, max_devices);
ezButton button(8);
// Hold joystick Position
int head_x = 0;
int head_y = 0;
// Hold maximum column and row number for dot matrix
int flag = 0;
int max_x = 31;
int max_y = 7;
String direction = "";
String prev_direction = "";
// define the joystick controls
const byte hpin = A0;
const byte vpin = A1;
void setup(){
Serial.begin(9600);
matrix.begin();
matrix.clear();
matrix.setPoint(head_y,head_x,true);
button.setDebounceTime(25);
}
void loop(){
button.loop();
if(flag ==0){
check_direction();
move_sprite();
}
matrix.setPoint(head_y,head_x,true);
delay(200);
}
void check_direction(){
if(analogRead(hpin)>512 && prev_direction !="Right"){
direction = "Left";
}
else if(analogRead(hpin)<512 && prev_direction !="Left"){
direction = "Right";
}
else if(analogRead(vpin)>512 && prev_direction !="Down"){
direction = "Up";
}
else if(analogRead(vpin)<512 && prev_direction !="Up"){
direction = "Down";
}
// Updating previous direction
prev_direction = direction;
}
void move_sprite(){
if(direction == "Left"){
head_x++;
}
if(direction == "Right"){
head_x--;
}
if(direction == "Up"){
head_y--;
}
if(direction == "Down"){
head_y++;
}
window_check();
if(button.isPressed()){
Serial.println("The Button is pressed");
flag =1;
}
}
void window_check(){
if(head_x > max_x){
head_x = 0;
}
else if(head_x < 0){
head_x = max_x;
}
else if(head_y > max_y){
head_y = 0;
}
else if(head_y < 0){
head_y = max_y;
}
}