Hello! So this project uses the Arduino UNO/RedBoard from Sparksfun, 8x8 Bicolor LED Matrix from Adafruit, and a 2-Axis Thumb Joystick.
I'm almost done making the game but the issue I am facing right is whenever I hover over a taken spot on the matrix with the joystick, the LED turns off. So I want add a condition that will make the LED skip over already taken spots. I have an idea of maybe using an array but I have no clue how to actually do it. Adding the win condition of the game after figuring that out should be the same idea.
Shown below is the program, libraries, and the parts that I used. I also have some comments that will help.
http://www.kr4.us/sparkfun-redboard-programmed-with-arduino.html
#include <Wire.h>
#include <Adafruit_GFX.h>
#include "Adafruit_LEDBackpack.h"
#define ROWS 8
#define COLS 8
#define PLAYER_1 LED_GREEN
#define PLAYER_2 LED_RED
Adafruit_BicolorMatrix matrix = Adafruit_BicolorMatrix();
/************* Variables ************/
//Binary map for board
static const uint8_t PROGMEM
board_bmp[] = {
B00100100,
B00100100,
B11111111,
B00100100,
B00100100,
B11111111,
B00100100,
B00100100 };
int buttonState = HIGH;
int X=6;
int Y=6;
bool player1 = true;
bool victory = false;
bool filled[8][8] = {false};
//Joystick Pins
const int SW_pin = 2;
const int X_pin = 0;
const int Y_pin = 1;
/********* Setup **********/
void setup()
{
Serial.begin(9600);
matrix.begin(0x70);
pinMode(SW_pin, INPUT);
digitalWrite(SW_pin, HIGH);
//Make Game board
matrix.setBrightness(5);
matrix.drawBitmap(0, 0, board_bmp, ROWS, COLS, LED_YELLOW);
matrix.writeDisplay();
}
/******** Loop *******/
void loop() {
matrix.setRotation(0); //Set rotation to normal
while(!victory) {
/********* Player 1's turn ********/
while(player1) {
Serial.print("X Axis:");
Serial.print(X);
Serial.print("\n");
Serial.print("Y Axis:");
Serial.print(Y);
Serial.print("\n\n");
matrix.drawRect(X,Y, 2,2, PLAYER_1);
matrix.writeDisplay();
delay(300);
//Go Left
if((analogRead(X_pin) < 500) && (X < 6))
{
matrix.drawRect(X,Y, 2,2, LED_OFF);
matrix.writeDisplay();
X=X+3;
delay(300);
}
//Go Right
else if((analogRead(X_pin) > 600) && (X > 0))
{
matrix.drawRect(X,Y, 2,2, LED_OFF);
matrix.writeDisplay();
X=X-3;
delay(300);
}
//Go Up
else if((analogRead(Y_pin) < 500) && (Y < 6))
{
matrix.drawRect(X,Y, 2,2, LED_OFF);
matrix.writeDisplay();
Y=Y+3;
delay(300);
}
//Go Down
else if((analogRead(Y_pin) > 600) && (Y > 0))
{
matrix.drawRect(X,Y, 2,2, LED_OFF);
matrix.writeDisplay();
Y=Y-3;
delay(300);
}
//Click button for player 1
buttonState = digitalRead(SW_pin);
if(buttonState==LOW)
{
Serial.print("Button Click \n");
Serial.print("X Axis:");
Serial.print(X);
Serial.print("\n");
Serial.print("Y Axis:");
Serial.print(Y);
Serial.print("\n\n");
matrix.drawRect(X,Y, 2,2, PLAYER_1);
matrix.writeDisplay();
delay(300);
X=6;
Y=6;
player1 = false;
}
}
/*********** Player 2's turn *********/
while(!player1) {
matrix.drawRect(X,Y, 2,2, PLAYER_2);
matrix.writeDisplay();
delay(300);
//Go Left
if((analogRead(X_pin) < 500) && (X < 6))
{
matrix.drawRect(X,Y, 2,2, LED_OFF);
matrix.writeDisplay();
X=X+3;
delay(300);
}
//Go Right
else if((analogRead(X_pin) > 600) && (X > 0))
{
matrix.drawRect(X,Y, 2,2, LED_OFF);
matrix.writeDisplay();
X=X-3;
delay(300);
}
//Go Up
else if((analogRead(Y_pin) < 500) && (Y < 6))
{
matrix.drawRect(X,Y, 2,2, LED_OFF);
matrix.writeDisplay();
Y=Y+3;
delay(300);
}
//Go Down
else if((analogRead(Y_pin) > 600) && (Y > 0))
{
matrix.drawRect(X,Y, 2,2, LED_OFF);
matrix.writeDisplay();
Y=Y-3;
delay(300);
}
//Click button for player 2
buttonState = digitalRead(SW_pin);
if(buttonState==LOW)
{
matrix.drawRect(X,Y, 2,2, PLAYER_2);
matrix.writeDisplay();
delay(300);
X=6;
Y=6;
player1 = true;
}
}
}
}