#include "LedControl.h" // We need to include the library that allows us to use the LED array
//the inputs to LedControl are: DataIn pin, CLK pin, CS pin, number of arrays
LedControl lc=LedControl(12,10,11,1);
// Joystick pin numbers
int SW_pin = 2; // digital pin connected to switch output
int X_pin = 0; // analog pin connected to X output
int Y_pin = 1; // analog pin connected to Y output
int movePosition = 1;
int Yposition = 0;
int Xposition = 0;
int middleXvalue = 512;
int middleYvalue = 512;
int offset = 100;
byte ledRow = B00000001;
byte board[8] = {B00000000, B00000000, B00000000, B00000000,
B00000000, B00000000, B00000000, B00000000};
void setup() {
lc.shutdown(0,false); // wakeup call (default is power-saving mode)
lc.setIntensity(0,2); // Set the brightness to a medium values
lc.clearDisplay(0); // clear the display to start
pinMode(SW_pin, INPUT);
digitalWrite(SW_pin, HIGH); // sets value of pin to HIGH (or 1) when not clicked
Serial.begin(9600); //set up the serial monitor
lc.setRow(0,0,ledRow);
}
void loop() {
// checking if joystick is pushed to the left
if (analogRead(X_pin) > (middleXvalue + offset)) {
if (Xposition == 0) {
Serial.print("You are already in the leftmost column");
Serial.print("\n");
}
else {
Xposition = Xposition - 1;
ledRow = ledRow >> 1;
lc.setRow(0, Yposition, ledRow | board[Yposition]);
}
}
// checking if joystick is pushed to the right
if (analogRead(X_pin) < (middleXvalue - offset)) {
if (Xposition == 7) {
Serial.print("You are already in the righttmost column");
Serial.print("\n");
}
else {
Xposition = Xposition + 1;
ledRow = ledRow << 1;
lc.setRow(0, Yposition, ledRow | board[Yposition]);
}
}
// checking if joystick is pushed up
if (analogRead(Y_pin) > (middleYvalue + offset)) {
if (Yposition == 0) {
Serial.print("You are already in the top row");
Serial.print("\n");
}
else {
Yposition = Yposition - 1;
lc.setRow(0, Yposition+1, 0 | board[Yposition+1]);
lc.setRow(0, Yposition, ledRow | board[Yposition]);
}
}
// checking if joystick is pushed down
if (analogRead(Y_pin) < (middleYvalue - offset)) {
if (Yposition == 7) {
Serial.print("You are already in the bottom row");
Serial.print("\n");
}
else {
Yposition = Yposition + 1;
lc.setRow(0, Yposition-1, 0 | board[Yposition-1]);
lc.setRow(0, Yposition, ledRow | board[Yposition]);
}
}
// Update board
if (digitalRead(SW_pin) == 0){
board[Yposition] = board[Yposition] | ledRow;
lc.setRow(0,Yposition,board[Yposition]);
ledRow = B00000001;
lc.setRow(0,0,ledRow | board[0]);
Xposition = 0;
Yposition = 0;
}
delay(200);
}