Tahir Miriyev
Published © GPL3+

Electronic Chessboard (4x4 Demo Prototype)

Creative approach to building electronic chessboard upon RFID technology, Hall effect sensors, and Arduino Mega.

IntermediateFull instructions provided6,828
Electronic Chessboard (4x4 Demo Prototype)

Things used in this project

Hardware components

Hall Effect Sensor
Hall Effect Sensor
×16
Solderless Breadboard Full Size
Solderless Breadboard Full Size
×1
LED (generic)
LED (generic)
×16
RFID reader (generic)
125 kHz RFID Reader and Antenna
×1
Arduino Mega 2560
Arduino Mega 2560
×1
RFID Tag 3M
×2

Software apps and online services

Arduino IDE
Arduino IDE

Hand tools and fabrication machines

plexiglass
Glossy paper
wooden planks
Acrylic paint (dark green and creme) x2
10 mm round magnets
Pawn and Queen chess pieces
Soldering iron and soldering materials

Story

Read more

Schematics

4x4demo_q0nvG3T3nS.fzz

Schematics are a little bit complicated, I know, but the idea should be clear. It was the first time I used Fritzing (highly recommended by the way), probably connections could be drawn more accurately. Anyway, I noted down everything inside the schematics.

Note: I couldn't find the exact model of RDIF Reader among components in the database of Fritzing. The model I used is 125Khz RFID module - UART. You can find tutorials on Youtube about how to set this module with Arduino.

Code

thinK_code.ino

Arduino
I tried to leave as many comments as I could, in order to make the process of code analysis understandable. To be honest, the logic might seem a bit complex from the first sight, but if you dig deeper into the logic of the code, it will look more comprehensive.

Note: Similar to the real chessboard, I abstractly numerated squares as A1, A2, A3, A4, B1, ..., C1, ... ,D1,.., D4. However, in the code, it's not practical to use this notation. Therefore I used arrays and represented squares as 00, 01, 02, 03,10,11,12,13,..., 32,33 respectively.
#include <SoftwareSerial.h>
SoftwareSerial RFID(11, 12);
//----------------------------------------------------------- START ---------------------------------------------------------------------------------------------
int empty_pos[2] ;
int figure_pos[2][2]; //thinking of a figure position as a matrix of two vectors(queen and pawn) with three entries(x pos, y pos, and value 1(queen), 2(pawn)
int new_id[14] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
int id_type_matrix[4][4] = {{0, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}};
int temp = 0;
int queen_id[14] = {2, 53, 65, 48, 48, 56, 51, 49, 55, 70, 65, 51, 52, 3};
int pawn_id[14] = {2, 53, 65, 48, 48, 56, 51, 56, 67, 49, 56, 52, 68, 3};
int temp_vect[2];
int count = 0;
int ID = 0;
int yeni_id[14]; 

//initial array matrix for hall-effect sensors
int hes_bool_matrix[4][4] ;


// list of Hall-effect sensors pins on Arduino
const int hes_00 = 44;
const int hes_01 = 45;
const int hes_02 = 46;
const int hes_03 = 47;
const int hes_10 = 48;
const int hes_11 = 49;
const int hes_12 = 50;
const int hes_13 = 2;
const int hes_20 = 3;
const int hes_21 = 4;
const int hes_22 = 5;
const int hes_23 = 6;
const int hes_30 = 7;
const int hes_31 = 8;
const int hes_32 = 9;
const int hes_33 = 10;


int hes_pin_matrix[4][4] =

{ {hes_00, hes_01, hes_02, hes_03},

  {hes_10, hes_11, hes_12, hes_13},

  {hes_20, hes_21, hes_22, hes_23},

  {hes_30, hes_31, hes_32, hes_33}
};

//LED pins
const int led_00 = 22;
const int led_01 = 23;
const int led_02 = 24;
const int led_03 = 25;
const int led_10 = 26;
const int led_11 = 27;
const int led_12 = 28;
const int led_13 = 29;
const int led_20 = 30;
const int led_21 = 31;
const int led_22 = 32;
const int led_23 = 33;
const int led_30 = 34;
const int led_31 = 35;
const int led_32 = 36;
const int led_33 = 37;


int led_matrix[4][4] =
{ {led_00, led_01, led_02, led_03},

  {led_10, led_11, led_12, led_13},

  {led_20, led_21, led_22, led_23},

  {led_30, led_31, led_32, led_33}
};


//----------------------------------------------------- SETUP AREA ---------------------------------------------------------------------------------

void setup() {

  RFID.begin(9600);
  Serial.begin(9600);


  //reading(input) modes for hall-effect sensors
  pinMode(hes_01, INPUT);
  pinMode(hes_02, INPUT);
  pinMode(hes_03, INPUT);
  pinMode(hes_00, INPUT);
  pinMode(hes_10, INPUT);
  pinMode(hes_11, INPUT);
  pinMode(hes_12, INPUT);
  pinMode(hes_13, INPUT);
  pinMode(hes_20, INPUT);
  pinMode(hes_21, INPUT);
  pinMode(hes_22, INPUT);
  pinMode(hes_23, INPUT);
  pinMode(hes_30, INPUT);
  pinMode(hes_31, INPUT);
  pinMode(hes_32, INPUT);
  pinMode(hes_33, INPUT);

  //writing(output) modes for LED pins
  pinMode(led_00, OUTPUT);
  pinMode(led_01, OUTPUT);
  pinMode(led_02, OUTPUT);
  pinMode(led_03, OUTPUT);
  pinMode(led_10, OUTPUT);
  pinMode(led_11, OUTPUT);
  pinMode(led_12, OUTPUT);
  pinMode(led_13, OUTPUT);
  pinMode(led_20, OUTPUT);
  pinMode(led_21, OUTPUT);
  pinMode(led_22, OUTPUT);
  pinMode(led_23, OUTPUT);
  pinMode(led_30, OUTPUT);
  pinMode(led_31, OUTPUT);
  pinMode(led_32, OUTPUT);
  pinMode(led_33, OUTPUT);
  
  read_tags(); delay(1000); 
  first_figure(); delay(1000);
  Serial.end(); 
  RFID.end(); delay(5000);
  Serial.begin(9600); delay(1000);
  RFID.begin(9600); delay(1000);  
  read_tags(); delay(5000);
  //RFID.flush(); // stops multiple reads
  second_figure(); delay(1000);
//  id_type_matrix[0][2] = 2;
//  id_type_matrix[2][1] = 1;
}
//---------------------------------------------------FUNCTIONS AREA------------------------------------------------------------
//----------------------------------------RFID ID Tracking and Figure Recognition-----------------------------------------------


boolean compare_ID(int aa[14], int bb[14])
{
  boolean ff = false;
  int fg = 0;
  for (int cc = 0 ; cc < 14 ; cc++)
  { if (aa[cc] == bb[cc])
    {
      fg++;
    }
  } if (fg == 14) {
    ff = true;
  }

  return ff;
}

int read_tags()
{
  Serial.println("Place the figure near to RFID reader");
  delay(5000);

  RFID.flush(); // stops multiple reads
  if (RFID.available() > 0) 
  {
    delay(300);
    for (int z = 0 ; z < 14 ; z++)
    {
      ID = RFID.read();
      new_id[z] = ID;
      Serial.println(new_id[z], DEC);
      delay(500);

    }  
  }

  Serial.println("ID reading is done ");
  
  ++count;
  delay(5000); //time to place the figure onto a board and the second one in front of a RFID reader

return new_id;
}

void first_figure() {

  if (compare_ID(new_id, queen_id) == true) {
    Serial.println("QUEEN IS DETECTED");
    for (int s = 0; s <= 3; s++) {
      for (int t = 0; t <= 3; t++) {
       
        if (digitalRead(hes_pin_matrix[s][t]) == 0) {
          id_type_matrix[s][t] = 1;
          temp_vect[0] = s;
          temp_vect[1] = t;
          Serial.print("Queen is placed onto:");
          Serial.print(s);
          Serial.println(t);
          digitalWrite(led_matrix[s][t],HIGH); delay(1000);
          digitalWrite(led_matrix[s][t],LOW);
          
        }
      }
    }
  }

  else if (compare_ID(new_id, pawn_id) == true) {
    Serial.println("PAWN IS DETECTED");
    for (int s = 0; s <= 3; s++) {
      for (int t = 0; t <= 3; t++) {
        if (digitalRead(hes_pin_matrix[s][t]) == 0) {
          id_type_matrix[s][t] = 2;
          temp_vect[0] = s;
          temp_vect[1] = t;
          Serial.print("Pawn is placed onto:");
          Serial.print(s);
          Serial.println(t);
          digitalWrite(led_matrix[s][t],HIGH); delay(1000);
          digitalWrite(led_matrix[s][t],LOW);
        }
      }
    }

  }
  else {
    Serial.println("Undefined figure ");
  }
}

void second_figure() {

  if (compare_ID(new_id, queen_id) == true) {
     Serial.println("QUEEN IS DETECTED");
    for (int s = 0; s <= 3; s++) {
      for (int t = 0; t <= 3; t++) {
        if (digitalRead(hes_pin_matrix[s][t]) == 0 && (s != temp_vect[0] || t != temp_vect[1])) {
          id_type_matrix[s][t] = 1;
          Serial.print("Queen is placed onto:");
          Serial.print(s);
          Serial.println(t);
          digitalWrite(led_matrix[s][t],HIGH); delay(1000);
          digitalWrite(led_matrix[s][t],LOW);
        }
      }
    }
  }

  else if (compare_ID(new_id, pawn_id) == true) {
    Serial.println("PAWN IS DETECTED");
    for (int s = 0; s <= 3; s++) {
      for (int t = 0; t <= 3; t++) {
        if (digitalRead(hes_pin_matrix[s][t]) == 0 && (s != temp_vect[0] || t != temp_vect[1])) {
          id_type_matrix[s][t] = 2;
          Serial.print("Pawn is placed onto:");
          Serial.print(s);
          Serial.println(t);
          digitalWrite(led_matrix[s][t],HIGH); delay(1000);
          digitalWrite(led_matrix[s][t],LOW);
        }
      }
    }
  }
}


//----------------------------------------------- FINDING FIGURES-------------------------------------------------------------------------

//extra function for turning leds off only when you place back the figure onto the board
void leds_off() {
  int i, j;
  for (i = 0; i <= 3; i++) {
    for (j = 0; j <= 3; j++) {
      digitalWrite(led_matrix[i][j], LOW);
    }
  }
}

//-------------------------------------------------MOVING QUEEN------------------------------------------------------------------------------------------------
void move_queen() {

  int i, j ;

  for (i = empty_pos[0]; i < 3;) {
    digitalWrite(led_matrix[++i][empty_pos[1]], HIGH); //light along a vertical line
  }   for (i = empty_pos[0]; i > 0;) {
    digitalWrite(led_matrix[--i][empty_pos[1]], HIGH);
  } for (i = empty_pos[1]; i < 3;) {
    digitalWrite(led_matrix[empty_pos[0]][++i], HIGH); //light along a horizontal line
  }
  for (i = empty_pos[1]; i > 0;) {
    digitalWrite(led_matrix[empty_pos[0]][--i], HIGH);
  }

  i = empty_pos[0];
  j = empty_pos[1];
  for (i = i - 3, j = j - 3; i <= 3, j <= 3; i++, j++) {
    if (i >= 0 && j >= 0 && i != empty_pos[0]) {
      Serial.print(i);
      Serial.println(j);
      digitalWrite(led_matrix[i][j], HIGH);
    }
  }
  i = empty_pos[0];
  j = empty_pos[1];
  for (i = i + 3, j = j - 3; i >= 0, j <= 3; i--, j++)
  {
    if (i >= 0 && i <= 3 && j >= 0 && j <= 3 && i != empty_pos[0]) {
      Serial.print(i);
      Serial.println(j);
      digitalWrite(led_matrix[i][j], HIGH);
    }
  }
 
}

//-------------------------------------start reading number of figures and saving positions for each of them--------------------------

void figure_reading() {
  //read all positions in a loop to detect the position of a pawn
  int i, j, found_figure = 0;
start: found_figure = 0 ;

  //read all occupied(0) and empty(1) positions on the board
  //assign 0(=empty square), 1(=occupied square) to a variable

  hes_bool_matrix[0][0] = digitalRead(hes_00);
  hes_bool_matrix[0][1] = digitalRead(hes_01);
  hes_bool_matrix[0][2] = digitalRead(hes_02);
  hes_bool_matrix[0][3] = digitalRead(hes_03);
  hes_bool_matrix[1][0] = digitalRead(hes_10);
  hes_bool_matrix[1][1] = digitalRead(hes_11);
  hes_bool_matrix[1][2] = digitalRead(hes_12);
  hes_bool_matrix[1][3] = digitalRead(hes_13);
  hes_bool_matrix[2][0] = digitalRead(hes_20);
  hes_bool_matrix[2][1] = digitalRead(hes_21);
  hes_bool_matrix[2][2] = digitalRead(hes_22);
  hes_bool_matrix[2][3] = digitalRead(hes_23);
  hes_bool_matrix[3][0] = digitalRead(hes_30);
  hes_bool_matrix[3][1] = digitalRead(hes_31);
  hes_bool_matrix[3][2] = digitalRead(hes_32);
  hes_bool_matrix[3][3] = digitalRead(hes_33);


  for (i = 0; i <= 3; i++) {
    for (j = 0; j <= 3; j++) {
      if (hes_bool_matrix[i][j] == 0) {
        found_figure++ ;

        if (found_figure == 1) {
          if (id_type_matrix[i][j] == 0) {
            id_type_matrix[i][j] = temp;
            temp = 0; }
         
          if(id_type_matrix[i][j]==1){
          Serial.print("Queen is standing on:");
          Serial.print(i);
          Serial.println(j);
          figure_pos[0][0] = i;
          figure_pos[0][1] = j;

         
          
          } else if(id_type_matrix[i][j]==2){
          Serial.print("Pawn is standing on:");
          Serial.print(i);
          Serial.println(j);
          figure_pos[0][0] = i;
          figure_pos[0][1] = j;
          
          //if (id_type_matrix[i][j] == 0) {
          //id_type_matrix[i][j] = temp;
          //temp = 0; }}
        } }
        
        else if (found_figure == 2)
        { if (id_type_matrix[i][j] == 0) {
            id_type_matrix[i][j] = temp;
            temp = 0; }
          
          if(id_type_matrix[i][j]==1){ Serial.print("Queen is standing on:");
          Serial.print(i);
          Serial.println(j);
          figure_pos[1][0] = i;
          figure_pos[1][1] = j;

         //if (id_type_matrix[i][j] == 0) {
         //id_type_matrix[i][j] = temp;
         //temp = 0; }
        
        } else if(id_type_matrix[i][j]==2){Serial.print("Pawn is standing on:");
          Serial.print(i);
          Serial.println(j);
          figure_pos[1][0] = i;
          figure_pos[1][1] = j;

//          if (id_type_matrix[i][j] == 0) {
//          id_type_matrix[i][j] = temp;
//          temp = 0; }
          
          //goto out;
        }
        }
      }
    }
} out:
  if (found_figure == 0 || found_figure ==1) {
    goto start;
  }
  else if (found_figure == 2) {
    leds_off();
  }

  //---------------------------------------------- we choose which figure to pick -----------------------------------------------------------------

  empty_pos[0] = -1;

  delay(2000);

  if (digitalRead(hes_pin_matrix[figure_pos[0][0]][figure_pos[0][1]]) == 1) {
    empty_pos[0] = figure_pos[0][0];
    empty_pos[1] = figure_pos[0][1];
    temp = id_type_matrix[empty_pos[0]][empty_pos[1]];
    id_type_matrix[empty_pos[0]][empty_pos[1]] = 0;
  }


  else if (digitalRead(hes_pin_matrix[figure_pos[1][0]][figure_pos[1][1]]) == 1) {
    empty_pos[0] = figure_pos[1][0];
    empty_pos[1] = figure_pos[1][1];
    temp = id_type_matrix[empty_pos[0]][empty_pos[1]];
    id_type_matrix[empty_pos[0]][empty_pos[1]] = 0;
  }

  //------------------------------------------- MOVING PAWN or QUEEN----------------------------------------------------------------------------------------------------

if(temp==1){
  if ( empty_pos[0] != -1) 
  {
    move_queen();
    goto start;
    }
    
  } else if(temp==2){if ( empty_pos[0] != -1) {
    if (empty_pos[0] < 2) {
      digitalWrite(led_matrix[empty_pos[0] + 1][empty_pos[1]], HIGH);
      digitalWrite(led_matrix[empty_pos[0] + 2][empty_pos[1]], HIGH);
    } else if (empty_pos[0] == 2) {
      digitalWrite(led_matrix[empty_pos[0] + 1][empty_pos[1]], HIGH);
    }
    else;
  } delay(100);}
  
 figure_reading();
}

//--------------------------------------------------------------------- loop starts ------------------------------------------------------------------------------------

void loop() {

  figure_reading();
}

Credits

Tahir Miriyev

Tahir Miriyev

1 project • 5 followers
Enthusiastic about product design, electronics, programming and project management, motivated to work hard, think deep and build things.

Comments