so i'm trying to make a tic tac toe game using a 3x3 keypad and a 8x8 dot matrix display, and an lcd screen that i'm still working in. one problem i'm consistently having is that the input seems to be inconsistent. The way the code currently works, the arduino treats the keypad as two keypads, one with the x coordinates, and one with the y coordinates, and from there it places an "x" (or a / due to size constraints) or an "o" (a ), then flips the turn. unfortunately, though the arduino detects an input from the keypad, it flips the turn without displaying an x or o. what would be the best way to solve this issue?
here's my code (ignore the win detection, it's incomplete, though has no effect on the issue)
#include <LedControl.h>
#include <Keypad.h>
#include <LiquidCrystal.h>
//lcd init
const int rs = 2, en = 3, d4 = 4, d5 = 5, d6 = 6, d7 = 7;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);
//keypad init
const byte ROWS = 3;
const byte COLS = 3;
const byte yValues[ROWS][COLS] =
{
{1,1,1},
{2,2,2},
{3,3,3},
};
const byte xValues[ROWS][COLS] =
{
{3,2,1},
{3,2,1},
{3,2,1},
};
const byte colPins[COLS] = {11,12,13};
const byte rowPins[ROWS] = {8,9,10};
Keypad keypadX = Keypad( makeKeymap(xValues), rowPins, colPins , ROWS, COLS);
Keypad keypadY = Keypad( makeKeymap(yValues), rowPins, colPins , ROWS, COLS);
//dot matrix init
int DIN = A0;
int CS = A1;
int CLK = A2;
LedControl lc = LedControl(DIN, CLK, CS, 0);
//game init
int turn = 1;
char* gameTable[3][3] = {
" ", " ", " ",
" ", " ", " ",
" ", " ", " ",
};
//setup loop
void setup() {
lcd.begin(16, 2);
lc.shutdown(0, false);
lc.setIntensity (0, 1);
lc.clearDisplay(0);
byte Table [8] = {B00100100, B00100100, B11111111, B00100100, B00100100, B11111111, B00100100, B00100100};
for (int i = 0; i < 8; i += 1) {
lc.setRow(0, i, Table[i]);
}}
//void loop
void loop() {
char keyX = keypadX.getKey();
char keyY = keypadY.getKey();
//gets coordinates from keypad input and places accordingly.
if (keyX or keyY) {
if (turn == 0) {
placeX(keyX, keyY);
turn = 1;
delay(500);
} else if (turn == 1) {
placeO(keyX, keyY);
turn = 0;
delay(500);
}
if (turn == 0) {
lcd.setCursor(0,0);
lcd.print("Player 1's turn!");
} else if (turn == 1) {
lcd.setCursor(0,0);
lcd.print("Player 2's turn!");
}}}
//place cross funcion
void placeX(int x, int y) {
int xProjected = (x - 1) * 3;
int yProjected = (y - 1) * 3;
lc.setLed(0, xProjected, yProjected, true);
lc.setLed(0, xProjected + 1, yProjected + 1, true);
gameTable[x -1][y -1] = "x";
}
//place circle funcion
void placeO(int x, int y) {
int xProjected = (x - 1) * 3;
int yProjected = (y - 1) * 3;
lc.setLed(0, xProjected + 1, yProjected, true);
lc.setLed(0, xProjected, yProjected + 1, true);
gameTable[x -1][y -1] = "o";
}
//win detection for rows
void checkRows() {
for (int y = 0; y < 3; y += 1) {
if (gameTable[1][y] == gameTable[2][y] && gameTable[1][y] == gameTable[3][y]) {
if (gameTable[1][y] = "o") {
//player 1 wins
} else if (gameTable[1][y] = "x"){
//player 2 wins
}}}}
//win detection for columns
void checkCols() {
for (int x = 0; x < 3; x += 1) {
if (gameTable[x][1] == gameTable[x][2] && gameTable[x][1] == gameTable[x][3]) {
if (gameTable[x][1] = "o") {
//player 1 wins
} else if (gameTable[x][1] = "x"){
//player 2 wins
}}}}
//win detection for diagonals
void checkDiag() {
if (gameTable[1][1] == gameTable[2][2] && gameTable[1][1] == gameTable[3][3]) {
if (gameTable[1][1] = "o") {
//player 1 wins
} else if (gameTable[1][1] = "x"){
//player 2 wins
}
} else if (gameTable[3][1] == gameTable[2][2] && gameTable[1][1] == gameTable[1][3]) {
if (gameTable[3][1] = "o") {
//player 1 wins
} else if (gameTable[3][1] = "x"){
//player 2 wins
}}}pe or paste code here



