Moustafaidris97
Published © GPL3+

Automated Chess Piece Mover

The aim of the project is to play chess in a completely automated environment.

AdvancedShowcase (no instructions)14,914
Automated Chess Piece Mover

Things used in this project

Story

Read more

Code

Automated Chess piece mover code

Arduino
Copy and paste to your Arduino IDE!
const int stepsBetweenSquares = 500;
const int stepPinX = 8; 
const int dirPinX = 9; 
const int stepPinY = 7;
const int dirPinY = 6;
const int limitSwitchX = 5;
const int limitSwitchY = 4;
const int magnetPin = 2;



int x1 = 0; int x2 = 1;
int y1 = 0; int y2 = 1;
int xsteps;
int ysteps;
int x = 0;
int y = 0;

void setup() {
  Serial.begin(9600);
  pinMode(stepPinX,OUTPUT); 
  pinMode(dirPinX,OUTPUT);
  pinMode(stepPinY,OUTPUT); 
  pinMode(dirPinY,OUTPUT);
  pinMode(limitSwitchX, INPUT);
  pinMode(limitSwitchY, INPUT);
  pinMode(magnet, OUTPUT);
  
  
  zeroDiagonal();
  digitalWrite(magnetPin, LOW);



}


void loop() {

  x1 = 0; x2 = 1;
 y1 = 0;y2 = 1;


  Serial.print("Piece to be moved letter adress:  ");
  while (!Serial.available());
    x1 = readSerialNumber(); 
      
  Serial.print("Piece to be moved number adress:  ");
  while (!Serial.available());
    y1 = readSerialNumber();      




  xsteps = stepsBetweenSquares*(x1-x2);
  ysteps = stepsBetweenSquares*(y1-y2);
  
  movePieceDiagonal(xsteps, ysteps);
 
  digitalWrite(magnetPin, HIGH);

//_______________________________________________________________________________________________
// Recieve position adress of piece new location from Serial Monitor (Letter, Number)
//-----------------------------------------------------------------------------------------------
  Serial.print("New location letter adress:  ");
  while (!Serial.available());
    x2 = readSerialNumber(); 

  Serial.print("New location number adress:  ");
  while (!Serial.available());
    y2 = readSerialNumber();
  Serial.println();

  


  

  xsteps = stepsBetweenSquares*(x2-x1);
  ysteps = stepsBetweenSquares*(y2-y1);
  
  digitalWrite(dirPinX, HIGH); digitalWrite(dirPinY, LOW);
  offsetPiece();
  delay(50);
  movePiece(xsteps, dirPinX, stepPinX);
  movePiece(-ysteps, dirPinY, stepPinY);
  delay(50);
  digitalWrite(dirPinX, LOW); digitalWrite(dirPinY, HIGH);
  offsetPiece();
  digitalWrite(magnetPin, LOW);

while(true){}

}






//////////////////////////////////////////////////////////////////////////////////////////////////////

int readSerialNumber(){
  int i;
  while (i < 1 || i > 8){
     i = Serial.parseInt();
    }
  Serial.println(i);
  Serial.parseInt();
  return i;
  }



void zeroDiagonal(){
  digitalWrite(dirPinX, LOW);
  digitalWrite(dirPinY, HIGH);
  while (digitalRead(limitSwitchX) == HIGH && digitalRead(limitSwitchY) == HIGH){
    moveDiagonal(1.5);
  }
  while (digitalRead(limitSwitchX) == HIGH){
    moveLinear(stepPinX, 1.5);
  }
  while (digitalRead(limitSwitchY) == HIGH){
    moveLinear(stepPinY, 1.5);
  }
  while (digitalRead(limitSwitchX) == LOW){         
   for (int i = 0; i<130; i++){
     digitalWrite(dirPinX, HIGH);
     digitalWrite(stepPinX, HIGH);
  delay(2);
  digitalWrite(stepPinX, LOW);
  delay(2);
  }
  
  }

  
  while (digitalRead(limitSwitchY) == LOW){         
    digitalWrite(dirPinY, LOW);
    for (int i = 0; i<90; i++){
     digitalWrite(stepPinY, HIGH);
  delay(2);
  digitalWrite(stepPinY, LOW);
  delay(2);
  }
}
}
///////////////////////////////////////////////////////////////////////////////////////////////////

void movePiece(int steps, int dirPin, int stepPin){
  if (steps > 0){
    digitalWrite(dirPin,HIGH);
  }
  else{
    steps = abs(steps);
    digitalWrite(dirPin, LOW);
  }
  for(int i = 0; i < steps; i++) {
    moveLinear(stepPin, 2);
  }
  }

////////////////////////////////////////////////////////////////////////////////////////////////////////

void movePieceDiagonal(int xsteps, int ysteps){
  if (ysteps >= 0){
    digitalWrite(dirPinY, LOW);
  }
  else{
    ysteps = abs(ysteps);
    digitalWrite(dirPinY, HIGH);
  }
  if (xsteps >= 0){
    digitalWrite(dirPinX, HIGH);
  } 
  else{
    xsteps = abs(xsteps);
    digitalWrite(dirPinX, LOW);
  }
  x = 0; y = 0; 
  while (x < xsteps && y < ysteps){
    moveDiagonal(2);
    x++; y++;
  }
  for(x; x < xsteps; x++){
    moveLinear(stepPinX, 2);
  }
  for(y; y < ysteps; y++){
    moveLinear(stepPinY, 2);
  }
}

/////////////////////////////////////////////////////////////////////////////////////////////////////////

void offsetPiece(){
  for (int i=0; i < stepsBetweenSquares/2; i++){
    moveDiagonal(2);
  }
}

///////////////////////////////////////////////////////////////////////////////////////////////////////////

void moveDiagonal(int delayTime){
    digitalWrite(stepPinX, HIGH);
    digitalWrite(stepPinY, HIGH);
    delay(delayTime);
    digitalWrite(stepPinX, LOW);
    digitalWrite(stepPinY, LOW);
    delay(delayTime);
}

////////////////////////////////////////////////////////////////////////////////////////////////////////////////

void moveLinear(int stepPin, int delayTime){
  digitalWrite(stepPin, HIGH);
  delay(delayTime);
  digitalWrite(stepPin, LOW);
  delay(delayTime);
}

Credits

Moustafaidris97

Moustafaidris97

1 project • 4 followers

Comments