/*
Project: Tic-Tac-Toe
Description: A tic tak toe game
Date: 11/30/24
Author: AnonEngineering
License: Beerware
NOTE: You need 330 ohm resistors in series with each LED!
*/
const int NUM_BTNS = 9;
const int BTN_PINS[] = {10, 7, 4, 11, 8, 5, 12, 9, 6};
const int X_LED_PINS[] = {43, 47, 51, 31, 35, 39, 3, 23, 27};
const int O_LED_PINS[] = {45, 49, 53, 33, 37, 41, 2, 25, 29};
// initialize global variables
int btnState[NUM_BTNS];
int oldBtnState[NUM_BTNS];
// function returns which button was pressed, or 0 if none
int checkButtons() {
int btnPressed = 0;
for (int i = 0; i < NUM_BTNS; i++) {
// check each button
btnState[i] = digitalRead(BTN_PINS[i]);
if (btnState[i] != oldBtnState[i]) { // if it changed
oldBtnState[i] = btnState[i]; // remember state for next time
if (btnState[i] == LOW) { // was just pressed
btnPressed = i + 1;
Serial.print("Button ");
Serial.print(btnPressed);
Serial.println(" pressed");
//digitalWrite(O_LED_PINS[i], HIGH); // or X
//} else { // was just released
//digitalWrite(O_LED_PINS[i], LOW);
}
delay(20); // debounce
}
}
return btnPressed;
}
void setup() {
Serial.begin(115200);
for (int i = 0; i < 9; i++) {
pinMode(BTN_PINS[i], INPUT_PULLUP);
pinMode(X_LED_PINS[i], OUTPUT);
pinMode(O_LED_PINS[i], OUTPUT);
}
}
void loop() {
int current_button = checkButtons();
//Serial.print("Button: ");
//Serial.println(current_button);
if (current_button != 0) { // if a button was pressed
digitalWrite(X_LED_PINS[current_button - 1], HIGH);
}
}