#include <LiquidCrystal.h>
#define ledAA 13
#define ledBA 12
#define ledCA 11
#define ledAB A0
#define ledBB A1
#define ledCB A2
#define ledAC A3
#define ledBC A4
#define ledCC A5
#define butC 8
#define butB 9
#define butA 10
LiquidCrystal lcd(7, 6, 5, 4, 3, 2);
int x;
int y;
bool turno = 0; //0 = o, 1 = x
int AA = 0; //0 = libero, 1 = O, 2 = X
int AB = 0; //indicano i posti
int AC = 0;
int BA = 0;
int BB = 0;
int BC = 0;
int CA = 0;
int CB = 0;
int CC = 0;
unsigned long oldtempo;
unsigned int del = 500; //delay per i led in millisecondi
int led[9];
int counter = 0;
/* int melody[] = {
440, 494, 523, 587,
659, 587, 523, 494,
440, 494, 523, 587,
659, 587, 523, 494
};
int melody2[] = {
330, 330, 349, 392, 392, 349,
330, 294, 294, 330, 330, 294,
330, 330, 349, 392, 392, 349,
330, 294, 294, 330, 294, 262
};
int length = sizeof(melody);
int length2 = 200; */
void setup(){
Serial.begin(9600);
lcd.begin(16, 2);
lcd.clear();
lcd.setCursor(0, 0);
pinMode(13, OUTPUT);
pinMode(12, OUTPUT);
pinMode(11, OUTPUT);
pinMode(A0, OUTPUT);
pinMode(A1, OUTPUT);
pinMode(A2, OUTPUT);
pinMode(A3, OUTPUT);
pinMode(A4, OUTPUT);
pinMode(A5, OUTPUT);
pinMode(9, INPUT);
pinMode(8, INPUT);
pinMode(1, INPUT);
lcd.print("Welcome to ");
lcd.setCursor(0,1);
lcd.print("TicTacToe!");
delay(500);
digitalWrite(0, LOW);
//musO();
}
//lcd.clear();
void setLED(){
if(turno == 1){
if(x == 0){
if(y == 0){
digitalWrite(ledAA, HIGH);
}else if(y == 1){
digitalWrite(ledAB, HIGH);
}else if(y == 2){
digitalWrite(ledAC, HIGH);
}
}else if(x == 1){
if(y == 0){
digitalWrite(ledBA, HIGH);
}else if(y == 1){
digitalWrite(ledBB, HIGH);
}else if(y == 2){
digitalWrite(ledBC, HIGH);
}
}else if(x == 2){
if(y == 0){
digitalWrite(ledCA, HIGH);
}else if(y == 1){
digitalWrite(ledCB, HIGH);
}else if(y == 2){
digitalWrite(ledCC, HIGH);
}
}
}else if(turno == 1){
if(x == 0){
if(y == 0){
led[counter] = ledAA;
}else if(y == 1){
led[counter] = ledAB;
}else if(y == 2){
led[counter] = ledAC;
}
}else if(x == 1){
if(y == 0){
led[counter] = ledBA;
}else if(y == 1){
led[counter] = ledBB;
}else if(y == 2){
led[counter] = ledBC;
}
}else if(x == 2){
if(y == 0){
led[counter] = ledCA;
}else if(y == 1){
led[counter] = ledCB;
}else if(y == 2){
led[counter] = ledCC;
}
}
counter++;
}
}
void changeTurn(){
lcd.clear();
if(turno){
lcd.print("O's turn");
turno = 0;
}else{
lcd.print("X's turn");
turno = 1;
}
delay(500);
}
/* void musO(){
for (int nota = 0; nota < length; nota++) {
tone(8, melody[nota], length2);
delay(150);
}
delay(2000);
} */
/* void musX(){
for (int nota = 0; nota < length; nota++) {
tone(8, melody2[nota], length2);
delay(150);
}
delay(1000);
} */
int winX(){
lcd.clear();
if(AA == 2 && AB == 2 && AC == 2){
lcd.print("X wins!");
lcd.setCursor(0,1);
lcd.print("Congrats!");
/* musX(); */
return true;
}else if(BA == 2 && BB == 2 && BC == 2){
lcd.print("X wins!");
lcd.setCursor(0,1);
lcd.print("Congrats!");
/* musX(); */
return true;
}else if(CA == 2 && CB == 2 && CC == 2){
lcd.print("X wins!");
lcd.setCursor(0,1);
lcd.print("Congrats!");
/* musX(); */
return true;
}else if(AA == 2 && BA == 2 && CA == 2){
lcd.print("X wins!");
lcd.setCursor(0,1);
lcd.print("Congrats!");
/* musX(); */
return true;
}else if(AB == 2 && BB == 2 && CB == 2){
lcd.print("X wins!");
lcd.setCursor(0,1);
lcd.print("Congrats!");
/* musX(); */
return true;
}else if(AC == 2 && BC == 2 && CC == 2){
lcd.print("X wins!");
lcd.setCursor(0,1);
lcd.print("Congrats!");
/* musX(); */
return true;
}else if(AA == 2 && BB == 2 && CC == 2){
lcd.print("X wins!");
lcd.setCursor(0,1);
lcd.print("Congrats!");
/* musX(); */
return true;
}else if(AC == 2 && BB == 2 && CA == 2){
lcd.print("X wins!");
lcd.setCursor(0,1);
lcd.print("Congrats!");
/* musX(); */
return true;
}
return false; //return bool,
}
int winO(){
lcd.clear();
if(AA == 1 && AB == 1 && AC == 1){
lcd.print("O wins!");
lcd.setCursor(0,1);
lcd.print("Congrats!");
/* musX(); */
return true;
}else if(BA == 1 && BB == 1 && BC == 1){
lcd.print("O wins!");
lcd.setCursor(0,1);
lcd.print("Congrats!");
/* musX(); */
return true;
}else if(CA == 1 && CB == 1 && CC == 1){
lcd.print("O wins!");
lcd.setCursor(0,1);
lcd.print("Congrats!");
/* musX(); */
return true;
}else if(AA == 1 && BA == 1 && CA == 1){
lcd.print("O wins!");
lcd.setCursor(0,1);
lcd.print("Congrats!");
/* musX(); */
return true;
}else if(AB == 1 && BB == 1 && CB == 1){
lcd.print("O wins!");
lcd.setCursor(0,1);
lcd.print("Congrats!");
/* musX(); */
return true;
}else if(AC == 1 && BC == 1 && CC == 1){
lcd.print("O wins!");
lcd.setCursor(0,1);
lcd.print("Congrats!");
/* musX(); */
return true;
}else if(AA == 1 && BB == 1 && CC == 1){
lcd.print("O wins!");
lcd.setCursor(0,1);
lcd.print("Congrats!");
/* musX(); */
return true;
}else if(AC == 1 && BB == 1 && CA == 1){
lcd.print("O wins!");
lcd.setCursor(0,1);
lcd.print("Congrats!");
/* musX(); */
return true;
}
return false; //return bool,
}
int endPos(){
if(AA != 0 && AB != 0 && AC != 0 && BA != 0 && BB != 0 && BC != 0 && CA != 0 && CB != 0 && CC != 0){
lcd.setCursor(0,0);
lcd.print("Pareggio!!");
return true;
}
return false;
}
void blink() {
if (oldtempo + del > millis()) {
for (int i = 0; i < sizeof(led); i++) {
if (digitalRead(led[i]) == LOW) {
digitalWrite(led[i], HIGH);
} else if (digitalRead(led[i]) == HIGH) {
digitalWrite(led[i], LOW);
}
oldtempo = millis();
}
}
}
int occPos(int x, int y){
if(x == 0){
if(y == 0 && AA != 0){
return true;
}else if(y == 1 && AB != 0){
return true;
}else if(y == 2 && AC != 0){
return true;
}
}else if(x == 1){
if(y == 0 && BA != 0){
return true;
}else if(y == 1 && BB != 0){
return true;
}else if(y == 2 && BC != 0){
return true;
}
}else if(x == 2){
if(y == 0 && CA != 0){
return true;
}else if(y == 1 && CB != 0){
return true;
}else if(y == 2 && CC != 0){
return true;
}
}
return false;
}
void reset(){
for(int i = 0; i < 9; i++){
led[i] = 0;
digitalWrite(ledAA, LOW);
digitalWrite(ledBA, LOW);
digitalWrite(ledCA, LOW);
digitalWrite(ledAB, LOW);
digitalWrite(ledBB, LOW);
digitalWrite(ledCB, LOW);
digitalWrite(ledAC, LOW);
digitalWrite(ledBC, LOW);
digitalWrite(ledCC, LOW);
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Welcome to ");
lcd.setCursor(0,1);
lcd.print("TicTacToe!");
delay(500);
}
}
void loop(){
while(!winO() || !winX() || !endPos()){
do{
delay(300);
changeTurn();
blink();
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Set");
lcd.setCursor(0, 1);
lcd.print("row");
Serial.println(!(digitalRead(butA) || digitalRead(butB) || digitalRead(butC)));
while(!(digitalRead(butA) || digitalRead(butB) || digitalRead(butC))){
delay(1);
}
if (digitalRead(butA)){
x = 0;
}
else if (digitalRead(butB)){
x = 1;
}
else if (digitalRead(butC)){
x = 2;
}
delay(300);
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Set");
lcd.setCursor(0, 1);
lcd.print("column");
while(!(digitalRead(butA) || digitalRead(butB) || digitalRead(butC))){
delay(1);
}
if (digitalRead(butA)){
y = 0;
}
else if (digitalRead(butB)){
y = 1;
}
else if (digitalRead(butC)){
y = 2;
}
}while(occPos(x, y));
delay(300);
setLED();
lcd.clear();
winO();
winX();
}
reset();
}
Welcome to the forum
What needs fixing ?
hey, thanks for reaching out.
when i upload the code to my arduino r3 none of the leds work and the check funcions aren't triggered.
Your circuit is not going to work. The top 2 rows of LEDs have their anodes connected together:

Hi @alepiga ,
Welcome to the forum..
The resistors for the leds look wrong too..
Do you have a simple sketch that tests the leds, buttons and screen??
Reminds me of when I did this..
Uno Connect 4
for some inspiration, ideas or just some fun..
good luck.. ~q
@qubits-us I tried your simulation. I dropped my counter, then waited over a minute for the arduino to drop it's counter. How long does it take to calculate its move?
it's multi player..
no ai here sorry..
~q
Oh, shame. I was looking forward to digging out my old connect-4 code for a head-to-head!
This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.
