Introduction: Robotic Arm Plays Tic Tac Toe
A few months ago I bought a robotic arm of 4dof made in acrylic and this week i decided to program it to play the ancient and famous game Tic Tac Toc against me, and also for participate in the robotic contest. So, if you like this project please voted for it. ;)
Materials:
- (1) Robotic arm with gripper.
- (1) Web cam.
- (1) Arduino One.
- (1) Board of 3 x 3 squares.
- Red and white pieces.
The web cam is for the image capture of the board. The image is analyzed by a visual basic program and using the minimax algorihm decides where the computer must place it´s piece. The program sends commands to the arduino one and this moves the robotic arm to the choosen position by the algorithm.
Step 1: The Robotic Arm
I bought the robotic arm here but you can build your own robot.
This robotic arm contains of 3 servos S3003, 1 for the base, 1 for the shoulder and 1 for the wrist. Also uses a servo sg90 for the gripper.
It is controlled by the Arduino One which is commanded by a visual basic program.
Step 2: The Diagram
As mentioned in the first step, 4 servos are connected to the pins 5,6,9 and 10 of the arduino one.
Step 3: The Image Capture
The web cam captures the image of the board and through a procedure in the visual basic program (you have to install the web cam library) , the image is divided into nine squares and each square is traveled for determining if contains red pixels. If so, the pixel is marked with red and later if the amount of points red of the square is greater than 60, then there is a red piece in that square and is interpreted with a "X", which is the movement of the human player.
Step 4: The Logic of the Game
The visual basic program determines the position of the computer thanks to an algorithm called miniMax, which is an algorithm used in games of two players and helps to decide the best movement for the computer turn.
Clic here for downloading the full code in visual basic of the project.
Note: the source code of the minimax algorithm used in the program was written by Ralph Main, you can see the article here.
Step 5: The Movement of the Arm
When the Artificial Intelligence of the minimax algorithm decides in which position the piece must be placed, it sends a string with the number of that position to the arduino through the serial port. The arduino receives the data and according to the position calls to the function that contains the movements in angles for moving the robotic arm.
Example of code for moving the arm to the second square:
if (dato=='2')
{
segundaPosicion()
}
......
void segundaPosicion()
{
//Move the base
for(i=180;i>=88;i--)
{ base.write(i);
delay(20);
}
delay(200);
//Moves the wrist to 90
for(i=114;i>=90;i--)
{
codo.write(i);
delay(20);
}
delay(200);
//Moves the shoulder to 160
for(i=140;i<=170;i++)
{ hombro.write(i);
delay(20);
}
delay(200);
//Moves the wrist
for(i=90;i<=102;i++)
{
codo.write(i);
delay(20);
}
delay(200);
//Open the gripper
for(i=110;i>=90;i--)
{
pinza.write(i);
delay(20); }
delay(200);
//Up the shoulder
for(i=170;i>=140;i--)
{
hombro.write(i);
delay(20);
}
delay(200);
posicionInicial(); //Moves to its initial position
}
Download the full code in arduino here
Thanks for watching, comments, sugestions, please let me know it.