+ All Categories
Home > Documents > CheckMate … · CheckMate Remote Arduino powered chess JOAKIM ERICSON ADAM WESTERMARK Bachelor’s...

CheckMate … · CheckMate Remote Arduino powered chess JOAKIM ERICSON ADAM WESTERMARK Bachelor’s...

Date post: 22-Oct-2020
Category:
Upload: others
View: 1 times
Download: 0 times
Share this document with a friend
70
IN DEGREE PROJECT TECHNOLOGY, FIRST CYCLE, 15 CREDITS , STOCKHOLM SWEDEN 2020 CheckMate Remote Arduino powered chess JOAKIM ERICSON ADAM WESTERMARK KTH ROYAL INSTITUTE OF TECHNOLOGY SCHOOL OF INDUSTRIAL ENGINEERING AND MANAGEMENT
Transcript
  • IN DEGREE PROJECT TECHNOLOGY,FIRST CYCLE, 15 CREDITS

    , STOCKHOLM SWEDEN 2020

    CheckMateRemote Arduino powered chess

    JOAKIM ERICSON

    ADAM WESTERMARK

    KTH ROYAL INSTITUTE OF TECHNOLOGYSCHOOL OF INDUSTRIAL ENGINEERING AND MANAGEMENT

  • CheckMate

    Remote Arduino powered chess

    JOAKIM ERICSONADAM WESTERMARK

    Bachelor’s Thesis at ITM

    Supervisor: Nihad Subasic

    Examiner: Nihad Subasic

    TRITA-ITM-EX 2020:38

  • Abstract

    Board games are on the rise and chess is no exception.However, in this increasingly digitalized world these boardgames lack something in comparrison to digitalized games,being able to play with anyone anywhere. This is a projectthat aimed to combine these two worlds by being a physi-cal game of chess where one could play against an opponentfrom far away.

    CheckMate is a robot consisting of laser cut acrylic framethat utilizing two stepper motors, by an pulley system in x-and y-direction, to move an electromagnet and a Hall e�ectsensor. The electromagnet was used for moving magneticpieces and one Hall e�ect sensor was used to determine if apiece occupied a square, and it that case if it was a black ora white piece. The corresponding output provided by theHall e�ect sensor was stored to its corresponding position.The board then communicated the location of all the pieceson the board, using wifi, to an website that also kept trackon whose turn it was.

    The result of this project was an robot that was able toperform all the moves necessary on the chessboard as wellas communicating to the website. When moving a piecefrom one location to another the piece repelled other pieceson its way. However, this was deemed to be acceptablesince the pieces were not moved too large of a distancefor the electromagnet to attract them when moved into itssquare. One move that the robot was not able to performwas castling. The results can therefore bee seen as a start-ing point toward further developments.

  • Referat

    Title in Swedish goes here

    Brädspels popularitet stiger och schack är inget undantag.Dock saknar brädspel de digitala spelens möjlighet att spelamed vem som helst varsomhelst. Detta projekt ville byggaöver klyftorna mellan dessa två världar genom att vara ettfysiskt schackspel med de digitala spelens möjligheter.

    ChecMate är en robot byggd av laserskuren akrylplast somanvänder två stegmotorer för att positionera en elektromag-net och en halle�ektsensor. Elektromagneten användes föratt flytta pjäserna på brädet medan helle�ektsensorn an-vändes vid kartläggning av pjäsernas position på brädet.Brädet kommmunicerade, via trådlöst nätverk, till en hem-sida som användes för att spara och överföra pjäsernas po-sitioner.

    Projektet resulterade i en robot som kunde göra alla mot-svarande drag hämtat från hemsidan. Dock så repelleradeen flyttande pjäs på de stationära pjäserna när den passera-de. Detta ansågs dock vara acceptabelt då elektromagnetenkunde föra tillbaka pjäserna när den befann sig i dess ruta.Något som projektet ej lyckades utföra var draget rockad.Resultatet av detta projekt kan ses som en startpunkt ien vidareutveckling eller en färdig produkt vid obesväradspelning.

  • Acknowledgements

    In the duration of this project we have received loads of help from people whomwithout their help this project would not have been possible.

    We would like to thank Nihad Subasic for providing us with feedback through-out this project and the support given to us during these strange COVID-19 times,Sta�an Qvarnström and Thomas Östberg for helping provide and giving guidancewith the components for the construction. We would like to thank SeshagopalanThorapalli Muralidharan for all the help given to us. Finally we would like to thankall of our course mates for the feedback and discussion.

  • Contents

    1 Introduction 11.1 Background . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11.2 Purpose . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11.3 Goals and scope . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2

    2 Theory 32.1 Microcontroller unit . . . . . . . . . . . . . . . . . . . . . . . . . . . 32.2 Internet . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3

    2.2.1 Wifi module . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32.3 Magnetic sensors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4

    2.3.1 Hall e�ect sensors . . . . . . . . . . . . . . . . . . . . . . . . 42.3.2 Reed switches . . . . . . . . . . . . . . . . . . . . . . . . . . . 52.3.3 Comparison . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5

    2.4 Motors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52.4.1 Stepper motor . . . . . . . . . . . . . . . . . . . . . . . . . . 6

    2.5 Stepper motor driver . . . . . . . . . . . . . . . . . . . . . . . . . . . 62.5.1 H-Bridge . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7

    2.6 Electromagnets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7

    3 Implementation 93.1 Construction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9

    3.1.1 Frame . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 93.1.2 Pulley system . . . . . . . . . . . . . . . . . . . . . . . . . . . 103.1.3 Pieces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10

    3.2 Electronics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 113.2.1 Hall-e�ect sensor . . . . . . . . . . . . . . . . . . . . . . . . . 123.2.2 Push button . . . . . . . . . . . . . . . . . . . . . . . . . . . 13

    3.3 Arduino code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 133.3.1 Movement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 143.3.2 Mapping pieces . . . . . . . . . . . . . . . . . . . . . . . . . . 15

    3.4 Webserver . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15

    4 Result 17

  • 4.1 Physical model . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 174.2 Testing the physical implementation . . . . . . . . . . . . . . . . . . 17

    4.2.1 Magnet proximity . . . . . . . . . . . . . . . . . . . . . . . . 184.2.2 Magnet strength . . . . . . . . . . . . . . . . . . . . . . . . . 184.2.3 Electromagnet attracting a piece . . . . . . . . . . . . . . . . 194.2.4 Positioning . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19

    4.3 Performance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 194.4 Communication . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20

    5 Discussion and conclussions 215.1 Discussion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21

    5.1.1 Reducing friction . . . . . . . . . . . . . . . . . . . . . . . . . 225.2 Conclussions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23

    6 Recommendations for future work 256.1 Recommendations . . . . . . . . . . . . . . . . . . . . . . . . . . . . 256.2 Future work . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25

    Bibliography 27

    Appendices 28

    A Website 29

    B Arduino code 31

    C Website code 45C.1 index.html . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45C.2 send.html . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46C.3 update.php . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46C.4 showboard.php . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48C.5 reset.php . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49C.6 ardCheck.php . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 50

    D Components 53D.1 Product sheet . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53

  • List of Figures

    2.1 Hall-e�ect in a conductive sheet . . . . . . . . . . . . . . . . . . . . . . . 42.2 Orientation of magnets and sesnors . . . . . . . . . . . . . . . . . . . . . 52.3 A depiction of the wiring in uni- and bipolar stepper motors . . . . . . . 6

    3.1 Assembly of the construction . . . . . . . . . . . . . . . . . . . . . . . . 103.2 Connection diagram between the electronic components . . . . . . . . . 123.3 A flowchart depicting the program . . . . . . . . . . . . . . . . . . . . . 133.4 Schematic chess board, and directions used in the code . . . . . . . . . . 143.5 The structure of the datatable pos . . . . . . . . . . . . . . . . . . . . . 163.6 The structure of the datatable last move . . . . . . . . . . . . . . . . . . 16

    4.1 The two types of magnets used, inserted into the pieces . . . . . . . . . 184.2 The webpage showboardstate.php showing the board state to a web

    browser . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20

  • List of Tables

    4.1 The result of the magnet proximity test . . . . . . . . . . . . . . . . . . 184.2 The result of the electromagnet test . . . . . . . . . . . . . . . . . . . . 19

  • List of Abbreviations

    HES Hall-e�ect sensor

    PWM Pulse width modulation

    USB Universal Serial Bus

    ICSP In-circuit serial programming

    IP adress Internet Protocol address

    DC motor Direct Current motor

    CAD Computer Aided Design

    wifi Wireless Fidelity

  • Chapter 1

    Introduction

    This chapter introduces the background, purpose, goals and scope of the project.It aims to provide the context in which this project was constructed as well as theprojects intentions and limitations.

    1.1 BackgroundIn an increasingly digitalized world some people still value playing games withphysical pieces on a physical board. According to a 2019 study [1] the global playingcards and board games are on the rise. The ancient game of chess - the roots ofthe game can be traced as far back as before the 6th century [2] - is no exception.Even with the rise of incorporated technologies in chess the longing for the analoggame is still there. These physical games does however lack some conveniences oftheir digital brethren, such as being able to play with anyone, anywhere. There hasbeen previous attempts at creating physical board games of chess with convenienceof digitalized games where players can play together from di�erent locations [3] [4].However, these project have had rather large material costs. In order to appeal toa larger audience the cost needs to be reduced.

    1.2 PurposeThe purpose of this project was to combine the feeling of playing a physical boardgame with the convenience of digitalized games where players can play together fromdi�erent locations. To make the boards convenient for home use it was decided thatthe boards should not use any crane to move the pieces and instead the movementshould stem from under the board. The project aspired to answer the followingresearch questions:

    • How can the boards be connected to each other?

    • How could the board register and keep track of the di�erent pieces?

    1

  • CHAPTER 1. INTRODUCTION

    • How can all this be at the lowest possible cost?

    1.3 Goals and scopeThe ultimate goal of this project was to create two physical boards of chess thatcould record and send moves between each other, as well as being able to executemoves being made on the opposite board. Due to time constraint of five monthspart time work, this was done in the following steps:

    • Create a board that can move a piece in both x- and y- direction

    • Implement registration and mapping of the pieces

    • Connect the board to the internet and use the internet to make moves on theboard

    • If possible, make another board and connect the two

    2

  • Chapter 2

    Theory

    This chapter provides the fundamental theoretical background of this project andits components.

    2.1 Microcontroller unitA microcontroller is a small circuit board that can be programmed to control a greatnumber of things,such as sensors and motors. The Arduino Uno is one of the mostpopular microcontrollers on the market, much due to how easy it is to program andthe simple design of it. It can interpret both analog and digital signals, it also hasa USB connection and a reset button.[5]

    2.2 InternetCommunication between devices at great distance from each other can be easilyachieved using the internet. Devices on the internet communicate using web servers.These servers store data which can be provided to other devices (called clients) whenrequested. To locate servers and clients on the web IP addresses are used. An IPaddress specifies the location of a specific device on the internet [6].

    2.2.1 Wifi moduleConnecting to the internet can be done through connecting a wifi module to theArduino. A cheap option is the ESP8266. The module allows connection to theinternet by sending and receiving radiowaves with information that can be caughtby nearby internet routers [7]. Through the wifi module a server can be setup whichallows the Arduino to connect to a computer or another Arduino [8]. An alternativeto this is to set up a server with a separate computer, this would however meanthat the dedicated computer would have to be on for the chessboard to work [9].

    3

  • CHAPTER 2. THEORY

    2.3 Magnetic sensorsAn easy way to detect where the chess pieces are on the board is to integrate magnetsinto their design,which can be sensed by magnetic sensors under the board. Aprevious project have used this to great e�ect [10]. That project used reed switchesas the sensors, however, Hall e�ect sensors are also an option [11], [4].

    2.3.1 Hall e�ect sensorsHall e�ect sensors (HES) are sensors that are built around Hall elements. Theseelements are activated by an external magnetic field and produces an output sig-nal that grows in strength as a function of the magnetic-field around the sensor.How this signal gets treated by the HES depends on its type. There are two typesHES, digital and analogue. Analogue versions takes the signal and amplifies it to aoutput voltage.This means that the output voltage of the sensor, often called Hallvoltage VH is proportional to the strength of the magnetic field. Digital versions ofthe sensors on the other hand, only has an ”ON” and an ”OFF” state. The sensorenters the ”ON” state whenever the magnetic flux around it is big enough. Thismeans that the sensor cant distinguish between two magnetic fields with di�erentflux, as long as the flux of both fields exceeds the threshold.[12]

    The Hall-e�ect is illustrated in Figure 2.1.

    (a) (b)

    Figure 2.1: Hall-e�ect in a conductive sheet [13]

    In Figure 2.1a a current is running through a sheet of conductive material, oftensemi-conductive,with a pair of probes on opposite sides connected to a voltmeter,measuring the voltage to zero. In Figure2.1b a magnetic field is applied to the plateat an orthogonal angle and the voltmeter measures as mall voltage.[13]

    Moving a magnet and a sensor in a sideway motion relative each other, with a

    4

  • 2.4. MOTORS

    fixed distance, is called sideway detection.Sideway detection is useful for detectingthe presence of a magnetic field.[12]

    2.3.2 Reed switchesReed switches consists of two magnetic contacts enveloped by glass. When sub-jected to a magnetic field,these contact draws closer to eachother and completes acircuit.[14]

    2.3.3 ComparisonA big di�erence between Reed switches and HES are that certain HES can give ananalogue signal while Reed switches only gives digital signals. The orientation ofthe magnets to be detected also di�ers.The axis of the magnet should be parallel toReed switches but perpendicular to HES , see Figure 2.2 [15].

    Figure 2.2: Orientation of magnets and sensors [15]

    Since analog HES are able to register di�erent level of magnetic flux, they could beused in the project to register either black or white pieces by having magnets withdi�erent magnetic flux in them.

    2.4 MotorsA simple DC motor works by using a looped wire connected to a rotor in a magneticfield. The wire is given current by a commutator which means that the magnitudeof the magnetic torque, · , equals to

    · = IBA sin(„). (2.1)

    5

  • CHAPTER 2. THEORY

    In (2.1) I is the current provided to the motor, B is the strength of the magneticfield inside the motor, A is the area of the loop and „ is the angle between themagnetic field and the loop plane. The torque acts on the wire and causes it tospin. Due to how sin(„) becomes negative if „ is between the values 180° and 360°,the current I needs to shift way between these values and become negative in ordertoo maintain the direction of the torque. To increase the torque and to avoid makingit uneven DC-motors often uses multiple wires turned into coils. The coils increasethe torque and by having multiple of them set at di�erent angles, the torque canremain relatively constant.[16]

    2.4.1 Stepper motorOne type of DC-motor is the stepper motor. These are able to be accurately con-trolled by turning in discrete steps. It does this by organizing the coils in phasesand energizing them in a sequence. They are most e�cient at low speeds. Steppercan be either bipolar or unipolar. The di�erence is that unipolar stepper motors hasone lead that always is negative and one that is positive. Because of this only halfthe coils can be used at a time. Bipolar steppers on the other hand can have alter-nating current through the phases which means that the polarity can be switchedand allows all the coils working at the same time. The implications of this is thatbipolar steppers generally have better e�ciency and produces a higher torque, how-ever it also needs a more complicated control system with H-bridges since they needalternating current.[17] Figure 2.3 illustrates the wiring in uni- and bipolar steppermotors.

    Figure 2.3: A depiction of the wiring in uni- and bipolar stepper motors [17]

    2.5 Stepper motor driverThe purpose of a stepper motor driver is to convert step commands coming fromthe machine controller to correct on-o� patterns required to drive the stepper mo-

    6

  • 2.6. ELECTROMAGNETS

    tor. Therefore the driver’s two primary functions are to sequencing the phases andcontrolling the phase current.[18]

    The type of driver used needs to be compatible to the stepper motor. A unipo-lar motor needs a unipolar driver and bipolar motors requires bipolar drivers.. Thealternating current in the phases in a bipolar motor means that the driver needs tohave two H-bridges to allow for this.[17]

    2.5.1 H-BridgeIn order to control the rotating direction of a DC motor one can simply reverse thedirection of the current flow through the motor. An H-bridge allows DC motors torun both backward and forward by activating two, of four, particular switches atthe time, thus changing the rotation direction of the motor.[19]

    2.6 ElectromagnetsAn electromagnet is a device consisting of a core of magnetic material surroundedby a coil through which an electric current is passed to magnetize the core. Elec-tromagnets are used when controllable magnets are required, for example when themagnetic flux is to be varied or be turned on respectively o�.[20]

    7

  • Chapter 3

    Implementation

    The implementation of the physical construction and digital code was done simul-taneously. A CAD model was created to test di�erent design solutions while firstdrafts of the code was written. When the CAD model was finished, the acrylicframe was cut and the custom pieces 3D-printed.

    3.1 Construction

    The first thing to be decided was the size of the chess pieces and the squares ofthe chessboard. Based on the size of the cheap magnets available, the pieces wasgiven the dimension to 18 mm in diameter and the squares the size 4◊4 cm. Basedon this, the maximum size of the available laser cutter and the philosophy thatsmaller was better,a model was created in the CAD (Computer-aided design) pro-gram Solid Edge. Metal rods were implemented in the design in order to supportthe xy-coordinate system under the board. The final dimensions of the chessboardbecame 55 ◊ 48 ◊ 12, 8 cm.

    When all the parts had been assembled, the physical construction could begin.The parts were mounted according to the Solid Edge model.

    3.1.1 Frame

    The frame was made out of 3mm thick acrylic which was laser-cut and engravedin the style of a traditional chessboard. Two holes were cut out of each long sidein order to fit the supporting metal rods. Underneath the board the componentsnecessary for the movement of the pieces was placed, Figure 3.1 shows an assemblyof the construction.

    9

  • CHAPTER 3. IMPLEMENTATION

    Figure 3.1: Assembly of the construction, made in Solid Edge 2019

    3.1.2 Pulley systemOne of the primary challenges of the construction, moving the electromagnet (andsubsequently the pieces) was solved using a pulley system. The system transfers thetorque provided by the stepper motors to belts which gets pulled back and forth.One belt is static and stands at the bottom of the frame while the other moves withthe rotation of the first belt. The electromagnet is connected to the rotation of thesecond belt. In other words, belt one controls the X-axis of the electromagnet whilebelt two controls its Y-axis. The whole system is supported by metal rods whichstabilizes everything.

    3.1.3 PiecesMagnets were integrated into the design of the pieces in order for magnetic sensorsbelow the board to detect the pieces. This was done by 3D printing hollow piecesto allow for the magnets to be put inside.

    When choosing what magnets to use for the chess pieces, three factors were con-sidered. Their size, cost and relative strength. The relative strength between the

    10

  • 3.2. ELECTRONICS

    magnets to be incorporated in the white and black pieces was important in orderfor the HES to give di�erent signals depending on the color of the piece. The cho-sen magnets were N35 Neodymium and Y28 Ferrite magnets. The product sheetsfor both magnets can be found in the Appendix D. Generally, magnets made ofNeodymium is much more magnetic then ones made from Ferrite. [21]

    3.2 ElectronicsThe electrical components used were the following:

    • One Arduino uno

    • Two breadboards

    • Two stepper motors

    • Two stepper motor drivers

    • One wifi-module, ESP8266

    • One Hall e�ect sensor

    • One electromagnet

    • One capacitor

    • One push button

    • One resistor 10k ohm

    • One resistor 1k ohm

    • One PVC pipe, 1m

    • One power supply

    The components were connected accordingly to Figure 3.2.

    11

  • CHAPTER 3. IMPLEMENTATION

    Figure 3.2: Connection diagram between the electronic components, made in Fritz-ing

    3.2.1 Hall-e�ect sensor

    As previously discussed, in section 1.1 Background, there have been similar projectsto this before. However, many of them have used HES under each of the boardssquares. To meet this projects goal on making a cheaper option and expanding onother projects it was decided to only use one HES. The HES was placed with theelectromagnet, and the two, more or less, took turns being active, to enable it tomove around and thereby knowing where the pieces were located. As presented insection 2.3.1 Hall e�ect sensors, having an analog Hall-e�ect sensor enabled to di�erbetween white and black pieces by having magnets of di�erent magnetic flux andtherefore producing di�erent VH .

    12

  • 3.3. ARDUINO CODE

    3.2.2 Push buttonTo ease the playing mechanics a push button was installed. The button was usedto send and retrieve information from the server.

    3.3 Arduino codeThe Arduino code provided the necessary functions, such as moving the pieces,mapping the pieces on the board, and allowing for communication to the server,needed to operate two communicating boards. The program used follows the logicpresented in Figure 3.3.

    Figure 3.3: A flowchart depicting the program, drawn using draw.io

    13

  • CHAPTER 3. IMPLEMENTATION

    The code itself can be found in Appendix B.

    In the code, the board was seen as by Figure 3.4, with the edge of the playingarea in blue and edge of the construction in red.

    y

    x

    (0, 0)

    (7, 7)

    Up

    DownLeft Right

    Figure 3.4: Schematic chess board, and directions used in the code, drawn inTeXShop using tikzpicture

    Setting the outer edges of the board helped control the positioning by setting a limiton how far the movement could be made, when for example scanning the board.

    3.3.1 MovementThe movement of the pieces on the board was achieved using a electromagnet underthe board, which was moved using a pulley system. When a piece was to be moved,the electromagnet was positioned under the specified piece, turned on and moved tothe right position. In order to ease the positioning, the function to move a squarein each position was programmed and used throughout the code. Moving half asquare in each direction was also programmed and used when removing pieces fromthe board as well as moving pieces from one square to another.

    How a piece was moved depended on the desired outcome, for example if the otherplayer captured a piece that to-be-removed piece would move di�erently on theboard compared to a piece that has to be moved between to squares. Below aretherefore a two explanations of functions that moves a piece on the board, di�er-ently from each other, that were used to control the movement of pieces.

    Removal of a pieceIf the other player captured a piece on their turn, that piece would first have to beremoved from the board before that move could be made. The board was designedwith extra space in x-direction to allow for removed pieces to be stored there. Thecode was written so that the to-be-removed piece was before the removal read bythe HES and depending on it it was a black or white piece it was stored on di�erentsides. The removal was done by moving a half square towards the middle of the

    14

  • 3.4. WEBSERVER

    board, to avoid contact with other pieces on the way out, and thereafter moveda square beyond the playing area in x-direction. It was necessary to move a halfsquare towards the middle of the board since there was no extra space in y-directionbeyond the edges of the board, and if the to-be-removed pieces was for example lo-cated at (3, 0) or (6, 7) it would not be possible to move a half square towards theedge.

    Moving a piece on the boardAs previously described a piece that moves on the board would have to be movedbetween squares, on the edges, in order to avoid contact with other pieces. Thiswas implemented by first moving a half square in x-direction, then moving half asquare too far in y-direction, thereafter moving to the correct x-location and finallyto the correct y-location.

    3.3.2 Mapping piecesFor one board to make the move that has been played on the other the board, theboards need to know what piece has been moved and to what location. This wasdone by saving values, depending on the VH for each square, into a two dimensionalarray. Depending on the VH one of three di�erent values corresponding to eitherwhite piece, black piece or empty square was stored in the array.

    As Figure 3.3 shows there is both a need to save a piece‘s new location and aneed to scan the board and to send that array to the other board. To save a piecenew location was done in the code by copying the opponents array as your own,after making the corresponding moves.

    Scanning the boardSince only one HES was used, the board had to be scanned in order for the boardto know what move has been made. The scanning of the board worked by firstreturning the HES to the starting position (7, 0) and thereafter moving it row byrow, both left and right, to the end position (7, 7), as seen in Figure 3.4. For eachsquare it passes it reads and stores the value corresponding to the VH .

    3.4 WebserverIn order for di�erent devices to communicate through the internet, a server wasneeded. It was set up on a PC running the operating system Windows 10 using theWeb development platform WampServer, which can create websites with HTML(HyperText Markup Language), PHP and the database platform MariaDB. Theprocess of setting up the server included opening a network port, an address fora specific process,on a local internet router. When the server was set up any webbrowser connected to the internet could easily access the sever,provided the correctIP address and port number.

    15

  • CHAPTER 3. IMPLEMENTATION

    The website was given was given capability to handle requests from both Arduinosand web browsers.To achieve this, the Arduino was programmed to access a webpage di�erent from the default one. All the code files in Appendix C was placedin the the www directory created by WampServer. The database structures can beseen in Figures 3.5, 3.6.

    Figure 3.5: The structure of the datatable pos

    Figure 3.6: The structure of the datatable last move

    16

  • Chapter 4

    Result

    This chapter presents the result of the project and its performance. Only one boardwas able to be constructed.

    4.1 Physical modelThe physical model was constructed in accordance to section 3.1 Construction.However, due to poor tolerance in the 3D printing of the customary parts, some ofthe holes had to be drilled afterwards in order to save time. This was done with apower drill, which resulted in some of the holes being angled.

    The goal of the design of the construction was to allow for the full range of necessarymovement while still being as small as possible. This was achieved by allowing theelectromagnet and HES to move as much as necessary in both x-and y-direction andsubsequently the board itself was only supported by its side frame. This combinedwith the flexible nature of the acrylic made the board sag, and consequently thedistance between the electromagnet and the piece di�ered depending on the locationon the board

    In order to save time, 16 identical cylinder pieces, in both colors, were 3D-printed.This still allowed for the board to distinguish black- and white pieces. However, forthe player this created a problem since a number of turns into a game it could bea problem to remember what cylinder represented what piece. This was solved bywriting the first letter of the respectively piece on top of it, for the king a crownsymbol was used.

    4.2 Testing the physical implementationIt was crucial to the performance that the code could be followed through withoutthe physical implementation preventing this. Hence, a few tests were constructed.

    17

  • CHAPTER 4. RESULT

    It was necessary to test how close to each other two magnets could be withoutthe magnet being repelling each other. In order to know how accurate the position-ing in x-and y-direction needed to be, it was necessary to test how far from a piecethe electromagnet and HES could be and still function as planned. Thereafter atest to see how accurate the positioning itself was carried out.

    4.2.1 Magnet proximityTwo pieces were placed on a fix distance from each other‘s center. One piece wasslowly pushed toward the other until the stationary piece moved. Then the dis-tance from the center of the pushed piece to the original positioning of the station-ary piece’s center was measured. The test was performed multiple times and thearithmetic mean of the results is shown in Table 4.1.

    Table 4.1: The result of the magnet proximity test in [cm]

    Pushed piece / stationary piece White BlackWhite 2.8 2.7Black 2.6 2.2

    4.2.2 Magnet strengthThe signals from two di�erent HES, one with the sensitivity 2,5 mV/G and theother with 5,0 mV/G was tested in position directly under the magnets on theboard (approximately 5mm below them). None of the sensors could di�erentiatebetween the N35 and Y28 magnets with a margin greater than 5%. This is likelybecause the N35 magnets were much smaller than the Y28 magnets see Figure 4.1.

    Figure 4.1: The two types of magnets used, inserted into the pieces

    18

  • 4.3. PERFORMANCE

    4.2.3 Electromagnet attracting a pieceTo see how close to the center of a piece the electromagnet needed to be in order tobe able to attract and move a piece the following test was executed.

    The electromagnet was placed in a fix position underneath the acrylic top sheet.Thereafter a piece was placed on a fix distance away from the center of the elec-tromagnet and the electromagnet was activated with a push button. If the electro-magnet attracted the piece the distance between the two centers was increased andthe test followed through again. The test was performed on two separate locations,in the middle on the board and on the bottom right corner of the board, since thedistance between the electromagnet and the piece was at largest on the bottomright corner and closest in the middle. The test was performed multiple times oneach location and the arithmetic mean of the test is presented Table 4.2.

    Table 4.2: The result of the electromagnet test in [cm]

    White BlackIn the corner 1.7 1.7In the middle 1.8 1.7

    4.2.4 PositioningAn essential part of this project was that the positioning was working as intendedand without any large errors. The previous test gave a result in how close to acenter of a piece the position of the electromagnet needed to be. These restrictionsresulted in that the margin of error when positioning the electromagnet was: 1.7cm. To ensure that a cultivated error when moving multiple squares did not occurthe margin of error was set to 1.78 cm when moving a square.

    To test that the movement did not exceed this margin of error the following testwas implemented. The centers of two squares, n squares apart, were marked. Thestarting position of the board was set to one of the squares before it was movedn squares away toward the other center. This test was conducted for a number ofdi�erent n values in di�erent directions. The end position was always within 1.78 ncm from the center the square and therefore the movement of the position was seenas acceptable.

    4.3 PerformanceOverall the performance of this model was deemed to be acceptable.

    The result presented in Table 4.1 with the magnet proximity test showed that apiece might a�ect other pieces when moved. Since moving a piece along the edge of

    19

  • CHAPTER 4. RESULT

    a square only provides 2 cm distance to that square‘s center it ensures that a piecethat might be located in that square is a�ected by the moving piece and moved.This however, was not an issue since a comparison with the result rendered in Table4.2 entails that the piece is able to move back to its center with the activation ofthe electromagnet.

    The decision to only use one HES induced the need to scan the board. This inturn resulted in a slower possible game, since the players needed to wait for thescanning process. To see how much extra time the decision to only use one HESculminated in the board was scanned multiple times with various delays, to allowfor the HES to give an output, on each square.

    When moving a half square in any direction, the piece lingered and was not movedas much as the electromagnet. Di�erent speeds were tested for moving a half squareand did not provide any notable di�erent outcome.

    4.4 CommunicationThe board was not able to connect with the standard 5,0 and 2,4 MHz wifi signalfrom a local router. It could however connect with the internet hotspot providedby an Android phone. It could with a 100% replication rate both send and receivedata to/from the website. Figure 4.2 shows how the positioning of the pieces couldbe viewed from the web server.

    Figure 4.2: The webpage showboardstate.php showing the board state to a webbrowser

    More pictures of the website can be found in Appendix A.

    20

  • Chapter 5

    Discussion and conclussions

    5.1 Discussion

    During this project KTH made the decision to shutdown its campus. This a�ectedthis project by not having access the usual equipment one might expect. Thereforemany solutions to eventual problems in the construction of this project had to beimprovised and may have a�ected the end result.

    One of the biggest challenges of this project was making the belt and pulley systemrun smoothly. The system had to be rebuilt several times in order for them to workconsistently. The PVC pipes between the two belts that run in the x-direction wasadded after the first version couldn’t move at all. Another thing that helped thesystem was to polish the metal rods with very fine sandpaper. Further, some of theholes being angled may have resulted in the construction not being as accurate andstable as intended. Additionally the tools available when working from home wasnot optimal and may have resulted in some of the parts being angled and having arough surface. Combining this with the fact that the assembly of the constructionneeded to be very exact in order to ensure that the parts lined up perfectly mayresulted in the pulley system being very sensitive. As previous said in section 4.2,when moving a piece along the edge of a square, pieces in the adjacent squares wasa�ected by the moving piece and repelled. Since a square was 4 ◊ 4 cm and there-fore the distance of the edge to the middle of a square 2 cm the stationary piecemoved at most 0.8 cm based on Table 4.1. Considering Table 4.2, the electromagnetcould easily within that distance attract that piece back into its square‘s center, andhence, this was not seen as a problem.

    One decision that was taken pursuing the goal of reducing the cost, as well asmaking something di�erent, was only using one HES. This, as said in section 4.2resulted in a slower game since the board needed to be scanned. This however,was not seen as an obstacle. The goal of this project was not to create the fastestpossible long distance physical chess game. Instead it aimed to explore di�erent

    21

  • CHAPTER 5. DISCUSSION AND CONCLUSSIONS

    options when constructing a long distance physical chess game. It is also to be saidthat if one would like to play with competitively rules - such as the so called touchmove rule, 4.3 in [22], or with a chess clock, article 6 in [22] - this project wouldnot anyway be able to meet those requirements and hence the need for speed wasobsolete. It is also possible for a chess game to last a very long time [23], and sincethis project was intended... where players could engage in other activities betweenmoves, it might not matter that much that the scanning of the board takes sometime. The implementation of the push button also makes it easier to go long timebetween moves since the opponents move on ones board only is made after the pressof the button. This entails that the player is ready and allow the player to see theopponent‘s move being made.

    As given by Table 4.1 magnets were a�ected if they were located in a square amoving magnet was passing by. This in turn meant that the HES might give adi�erent VH for a piece not located in the center of a square compared to if it wasin the center. Ergo the HES might misread the white for a black piece, and mightnot register a black piece at all. This was partially solved in the Arduino codeby activating the electromagnet to attract the piece and thereafter deactivating theelectromagnet before reading the HES. This ensured that the piece was in a positionalmost straight above the HES before it was read. This reduced the error of theHES, but since the margins in the signal value were so small (see chapter 4.2.2) theresults were still inconsistent.

    The fact that a piece lingered when moving half a square in one direction, wasnot seen as a large defect. When moving in any direction it is more favorable forthe positioning of the electromagnet and HES to be correct rather than a piece.This is because the piece can with the activation of the electromagnet be broughtback into the right position.

    As mentioned in section 3.1.3 identical pieces, in both colors, were 3D-printed.The original plan to create specific pieces to each position was however, closelyfollowed. This was since the original plan for the pieces was to create them in twosteps, first the identical cylinders and subsequently create an upper part to allowfor distinction. The result was considered good enough since it served the samepurpose as the original intended pieces in all ways but esthetic.

    5.1.1 Reducing friction

    The holes in the 3D printed parts which would be connected to the metal rods wereoriginally a bit to small to be able to slide. This was fixed by drilling them with a10mm drill.

    22

  • 5.2. CONCLUSSIONS

    5.2 ConclussionsIn conclusion, the result and answer to the research questions were the following.

    How can the boards be connected to each other?

    Connecting two, communicating, separate boards could be done Using the internetto connect two boards to each other was seen as a good solution. There wereoriginally some struggle setting up the WiFi module ESP8266 but after that itworked fine.

    How could the board register and keep track of the di�erent pieces?

    One possible way this could be done was to have magnetic sensors below the board.Specifically one could for example use Reed switches or HES. One way to utilizeHES is to have one under each square of the board, another to just have one thatis able to move. If only one HES is used it is necessary to scan the whole board inorder to know what move the player made during their turn.

    The implementation of only one HES resulted in the need to scan the board andconsequently the potential time between moves was increased.

    Using an online database to store the positions of every piece worked great. Theresponse time was quick and everything was easily accessible.

    How can all this be done at the lowest possible cost?

    One way to reduce the cost was the decision to use only one HES. Another approachto this was to 3D print all the components, instead of buying them. In addition tothis the chess board itself was made of acrylic and laser cut and engraved in orderto reduce the cost.

    23

  • Chapter 6

    Recommendations for future work

    This chapter aims to provide recommendations for improvements for the existingproject as well as suggestions for future work.

    6.1 RecommendationsWhen assembling the construction it was of the utter most necessity to be preciseto avoid misalignment and consequently avert any part of the pulley system to bemisaligned. The pulley system was also one part of this project that required muchtime. To make this part of the project easier, it might be beneficial to use bearings.

    6.2 Future workAs mentioned in section 4.2 the magnets, when passing, a�ected each other. Infuture work it is worth investigating how this can be done. One way this could bedone is by using magnets with a lesser field strength compared to the ones usedin this project. The magnets might not then a�ect each other and remain in thecenter of the square. Another solution is to simply make the board larger.

    Another way to improve upon this project would be to use magnets of more vary-ing strengths, since the HES could not always distinguish between black and whitepieces.

    25

  • Bibliography

    [1] (2018, Sep) Visited: 2020-01-31. [On-line]. Available: https://www.grandviewresearch.com/industry-analysis/playing-cards-board-games-market?fbclid=IwAR19aUx92YuV2iouMPUdmou5oR4dDEBjYYcy9D6-NnICX8W8RLnfvwjZ6IA

    [2] A. E. Soltis. (2019, Dec) Chess. Visited: 2020-01-31. [Online]. Available:https://www.britannica.com/topic/chess/Development-of-theory

    [3] M. A. Chhangani, “Arduino based wireless powered chess,” International Jour-nal of Innovative Research in Computer and Communication Engineering,vol. 3, no. 4, 2015, doi: 10.15680/ijircce.2015.0304139.

    [4] F. Sušac, I. Aleksi, and Z. Hocenski, “Digital chess board based on arrayof hall-e�ect sensors,” in 2017 40th International Convention on Informationand Communication Technology, Electronics and Microelectronics (MIPRO).IEEE, 2017, pp. 1011–1014, doi: 10.23919/MIPRO.2017.7973572.

    [5] What is an arduino? Visited: 2020-05-11. [Online]. Available: https://learn.sparkfun.com/tutorials/what-is-an-arduino

    [6] D. Gourley, B. Totty, M. Sayer, S. Reddy, and A. Aggarwal, HTTP: The Defi-nite Guide, 1st ed., L. Mui, Ed. 1005 Gravenstein Highway North, Sebastopol,1005 Gravenstein Highway North, Sebastopol, CA 95472.: O’Reilly Media, Inc.,Sep 2002.

    [7] H. Johansson, Elektroteknik, 2013.

    [8] Wifi library. Visited: 2020-02-16. [Online]. Available: https://www.arduino.cc/en/Reference/WiFi?fbclid=IwAR3fa7DrBv5OITts7kVQwo1-iuiMyXj0VoBIHH0I9tLngZQHXOpaTip4eM

    [9] (2016, 10) What is a web server? and which web server software is there?Visited: 2020-02-16. [Online]. Available: https://www.ionos.com/digitalguide/server/know-how/web-server-definition-background-software-tips/?fbclid=IwAR0OGUNGL3jbvsIB1dR3FAZOJDZRqbU5rRhf24kTg37Sd6VWIPjTsNyQnpE

    [10] R. Seyed Farshchi, “The chess robot,” Master’s thesis, KTH, 2015, diva id:2:916283.

    27

    https://www.grandviewresearch.com/industry-analysis/playing-cards-board-games-market?fbclid=IwAR19aUx92YuV2iouMPUdmou5oR4dDEBjYYcy9D6-NnICX8W8RLnfvwjZ6IAhttps://www.grandviewresearch.com/industry-analysis/playing-cards-board-games-market?fbclid=IwAR19aUx92YuV2iouMPUdmou5oR4dDEBjYYcy9D6-NnICX8W8RLnfvwjZ6IAhttps://www.grandviewresearch.com/industry-analysis/playing-cards-board-games-market?fbclid=IwAR19aUx92YuV2iouMPUdmou5oR4dDEBjYYcy9D6-NnICX8W8RLnfvwjZ6IAhttps://www.britannica.com/topic/chess/Development-of-theory10.15680/ijircce.2015.030413910.23919/MIPRO.2017.7973572https://learn.sparkfun.com/tutorials/what-is-an-arduinohttps://learn.sparkfun.com/tutorials/what-is-an-arduinohttps://www.arduino.cc/en/Reference/WiFi?fbclid=IwAR3fa7DrBv5OITts7kVQw_o1-iuiMyXj0VoBIHH0I9tLngZQHXOpaTip4eMhttps://www.arduino.cc/en/Reference/WiFi?fbclid=IwAR3fa7DrBv5OITts7kVQw_o1-iuiMyXj0VoBIHH0I9tLngZQHXOpaTip4eMhttps://www.arduino.cc/en/Reference/WiFi?fbclid=IwAR3fa7DrBv5OITts7kVQw_o1-iuiMyXj0VoBIHH0I9tLngZQHXOpaTip4eMhttps://www.ionos.com/digitalguide/server/know-how/web-server-definition-background-software-tips/?fbclid=IwAR0OGUNGL3jbvsIB1dR3FAZOJDZRqbU5rRhf24kTg37Sd6VWIPjTsNyQnpEhttps://www.ionos.com/digitalguide/server/know-how/web-server-definition-background-software-tips/?fbclid=IwAR0OGUNGL3jbvsIB1dR3FAZOJDZRqbU5rRhf24kTg37Sd6VWIPjTsNyQnpEhttps://www.ionos.com/digitalguide/server/know-how/web-server-definition-background-software-tips/?fbclid=IwAR0OGUNGL3jbvsIB1dR3FAZOJDZRqbU5rRhf24kTg37Sd6VWIPjTsNyQnpE

  • BIBLIOGRAPHY

    [11] D. Bhutani, Y. Ali, and P. Gupta, “Design and implementation of a wire-less remote chess playing physical platform,” International Journal of Engi-neering Research and Technology, vol. V6, no. 09, Sep 2017, doi: 10.17577/IJERTV6IS090020.

    [12] (2018, Feb) Hall e�ect sensor. Visited: 2020-02-07. [Online]. Available: https://www.electronics-tutorials.ws/electromagnetism/hall-e�ect.html?fbclid=IwAR2q8ZlindSqy-fSlfHdLI93j2fyWat8gqLqgn2UBb19uOe-uM-jYSa8-Ho

    [13] E. Ramsden, Hall-E�ect Sensors: Theory and Application, 2nd ed. ElsevierInc, 2006.

    [14] Z. Talib, “Investigation of fast high voltage pdc measurement based on a vac-uum reed-switch,” Master’s thesis, KTH, EES, 01 2011, diva id: diva2:511557.

    [15] I. K & J Magnetics. Reed switches and hall e�ect sen-sors. [Online]. Available: https://www.kjmagnetics.com/blog.asp?p=reed-switches-and-hall-e�ect-sensors

    [16] H. D. Young and R. A. Freedman, Univerisity Physics with Modern Physics,14th ed. Pearson Education Limited, 2016.

    [17] B. Earl. (2019, 01) All about stepper motors.

    [18] B. Porter. (2019, Apr) What’s a stepper motor driver and whydo i need it? Visited: 2020-12-02. [Online]. Available: https://all3dp.com/2/what-s-a-stepper-motor-driver-why-do-i-need-it/

    [19] Arduino dc motor control tutorial - l298n — pwm — h-bridge. Visited:2020-02-12. [Online]. Available: https://howtomechatronics.com/tutorials/arduino/arduino-dc-motor-control-tutorial-l298n-pwm-h-bridge/

    [20] (2014, Jan) Visited: 2020-02-07. [Online]. Available: https://www.britannica.com/science/electromagnet

    [21] Permanent magnet standard items. Visited: 2020-05-11. [Online]. Available:https://www.magfine.co.jp/eng/magnet/comparison.html

    [22] I. C. Federation. Fide laws of chess.

    [23] G. W. Records. Most moves in a chess game. Visited: 2020-05-11.[Online]. Available: https://www.guinnessworldrecords.com/world-records/68297-most-moves-in-a-chess-game

    28

    10.17577/IJERTV6IS09002010.17577/IJERTV6IS090020https://www.electronics-tutorials.ws/electromagnetism/hall-effect.html?fbclid=IwAR2q8ZlindSqy-fSlfHdLI93j2fyWat8gqLqgn2UBb19uOe-uM-jYSa8-Hohttps://www.electronics-tutorials.ws/electromagnetism/hall-effect.html?fbclid=IwAR2q8ZlindSqy-fSlfHdLI93j2fyWat8gqLqgn2UBb19uOe-uM-jYSa8-Hohttps://www.electronics-tutorials.ws/electromagnetism/hall-effect.html?fbclid=IwAR2q8ZlindSqy-fSlfHdLI93j2fyWat8gqLqgn2UBb19uOe-uM-jYSa8-Hodiva2:511557https://www.kjmagnetics.com/blog.asp?p=reed-switches-and-hall-effect-sensorshttps://www.kjmagnetics.com/blog.asp?p=reed-switches-and-hall-effect-sensorshttps://all3dp.com/2/what-s-a-stepper-motor-driver-why-do-i-need-it/https://all3dp.com/2/what-s-a-stepper-motor-driver-why-do-i-need-it/https://howtomechatronics.com/tutorials/arduino/arduino-dc-motor-control-tutorial-l298n-pwm-h-bridge/https://howtomechatronics.com/tutorials/arduino/arduino-dc-motor-control-tutorial-l298n-pwm-h-bridge/https://www.britannica.com/science/electromagnethttps://www.britannica.com/science/electromagnethttps://www.magfine.co.jp/eng/magnet/comparison.htmlhttps://www.guinnessworldrecords.com/world-records/68297-most-moves-in-a-chess-gamehttps://www.guinnessworldrecords.com/world-records/68297-most-moves-in-a-chess-game

  • Appendix A

    Website

    Various pictures from the di�erent web pages.

    29

  • APPENDIX A. WEBSITE

    30

  • Appendix B

    Arduino code

    This code was loaded to the Arduino.

    // Pro j e c t name : CheckMate// Date : 13/05≠2020// Desc r ip t i on : Turns the Arduino in to a ches s robot that can// send and r e c e i v e moves from a webs i te// Written by : Joakim Er icson & Adam Westermark

    // L i b r a r i e s

    // Used by m i c r o c o n t r o l l e r s for communication#inc l ude // Used by the esp8266 for communication with the i n t e r n e t#inc l ude

    //Motors// Pins for motors#d e f i n e dirPinY 2#d e f i n e stepPinY 3#d e f i n e dirPinX 4#d e f i n e stepPinX 5

    //Used for movement#d e f i n e s t epsPerRevo lu t ion 200

    // Electromagnet#d e f i n e e lec tromagnetPin 7

    // Hall≠e f f e c t s enso r#d e f i n e ha l lSensorPin A0f l o a t ha l lVa lue ;

    31

  • APPENDIX B. ARDUINO CODE

    //Button#d e f i n e buttonPin 9i n t buttonState = 0 ; // v a r i a b l e w i l l change

    // Wifi≠module#d e f i n e RX 10#d e f i n e TX 11

    //These S t r i n g s depend on what w i f i network//you are us ing and what s e r v e r you wish to connect toSt r ing AP = ”your AP” ; // Access po int ( the name o f the w i f i )S t r ing PASS = ” your Password ” ; // Password ( for the w i f i )S t r ing HOST = ” your Server ” ; // Server nameSt r ing PORT = ” your ServerPort ” ; // Port name

    S o f t w a r e S e r i a l esp8266 (RX,TX) ; / / Makes the esp8266 a S o f t w a r e S e r i a l ob j e c t

    // Locat ion o f the p i e c e s on each board r e s p e c t i v e l y ,//0≠>empty square , 1≠>white p iece , 2≠>black p i e c e//Board 1 i s the p h y s i c a l board ,// board 2 i s the l a s t board gotten from the webs i tei n t board1 [ 8 ] [ 8 ] = {{2 ,2 , 2 , 2 , 2 , 2 , 2 , 2 } ,

    {2 ,2 , 2 , 2 , 2 , 2 , 2 , 2 } ,{0 ,0 , 0 , 0 , 0 , 0 , 0 , 0 } ,{0 ,0 , 0 , 0 , 0 , 0 , 0 , 0 } ,{0 ,0 , 0 , 0 , 0 , 0 , 0 , 0 } ,{0 ,0 , 0 , 0 , 0 , 0 , 0 , 0 } ,{1 ,1 , 1 , 1 , 1 , 1 , 1 , 1 } ,{1 ,1 , 1 , 1 , 1 , 1 , 1 , 1 }} ;

    i n t board2 [ 8 ] [ 8 ] = {{2 ,2 , 2 , 2 , 2 , 2 , 2 , 2 } ,{2 ,2 , 2 , 2 , 2 , 2 , 2 , 2 } ,{0 ,0 , 0 , 0 , 0 , 0 , 0 , 0 } ,{0 ,0 , 0 , 0 , 0 , 0 , 0 , 0 } ,{0 ,0 , 0 , 0 , 0 , 0 , 0 , 0 } ,{0 ,0 , 0 , 0 , 0 , 0 , 0 , 0 } ,{1 ,1 , 1 , 1 , 1 , 1 , 1 , 1 } ,{1 ,1 , 1 , 1 , 1 , 1 , 1 , 1 }} ;

    // Var iab l e s used to keep track o f where the board the e lect romagnet i s//Home p o s i t i o n i s ( 7 , 0 )f l o a t x=7;f l o a t y=0;

    32

  • // Stor ing

    i n t cArray [ 5 ] ; // an array used to compare the two boardsi n t i c ; // used as counter s for i t e r a t i n g through the two≠dimens iona l arrayi n t j c ;

    //Movement , used when moving to a new l o c a t i o ni n t xTarget ;i n t yTarget ;

    void setup ( ) {pinMode ( stepPinY , OUTPUT) ;pinMode ( dirPinY , OUTPUT) ;pinMode ( stepPinX , OUTPUT) ;pinMode ( dirPinX , OUTPUT) ;pinMode ( e lectromagnetPin , OUTPUT) ;pinMode ( ha l lSensorPin , INPUT) ;pinMode ( buttonPin , INPUT) ;S e r i a l . begin ( 9 6 0 0 ) ;esp8266 . begin ( 9 6 0 0 ) ;

    }

    // Functions to change p o s i t i o n//k i s the amount o f s t ep s to be taken in a d i r e c t i o nvoid squareUp ( i n t k ){

    d i g i t a l W r i t e ( dirPinY , LOW) ;for ( f l o a t i = 0 ; i < k �0 .8� s tepsPerRevo lut ion ; i++) {

    // These four l i n e s r e s u l t in 1 step :d i g i t a l W r i t e ( stepPinY , HIGH) ;de layMicroseconds ( 2 0 0 0 ) ;d i g i t a l W r i t e ( stepPinY , LOW) ;de layMicroseconds ( 2 0 0 0 ) ;

    }delay ( 1 5 0 0 ) ;y≠=k ;

    }

    void squareDown ( i n t k ){d i g i t a l W r i t e ( dirPinY , HIGH) ;for ( f l o a t i = 0 ; i < k �0 .8� s tepsPerRevo lut ion ; i++) {

    // These four l i n e s r e s u l t in 1 step :d i g i t a l W r i t e ( stepPinY , HIGH) ;

    33

  • APPENDIX B. ARDUINO CODE

    delayMicroseconds ( 2 0 0 0 ) ;d i g i t a l W r i t e ( stepPinY , LOW) ;de layMicroseconds ( 2 0 0 0 ) ;

    }delay ( 1 5 0 0 ) ;y+=k ;

    }

    void squareRight ( i n t k ){d i g i t a l W r i t e ( dirPinX , HIGH) ;for ( f l o a t i = 0 ; i < k �0 .8� s tepsPerRevo lut ion ; i++) {

    // These four l i n e s r e s u l t in 1 step :d i g i t a l W r i t e ( stepPinX , HIGH) ;de layMicroseconds ( 2 0 0 0 ) ;d i g i t a l W r i t e ( stepPinX , LOW) ;de layMicroseconds ( 2 0 0 0 ) ;

    }delay ( 1 5 0 0 ) ;x+=k ;

    }

    void squareLe f t ( i n t k ){d i g i t a l W r i t e ( dirPinX , LOW) ;for ( f l o a t i = 0 ; i < k �0 .8� s tepsPerRevo lut ion ; i++) {

    // These four l i n e s r e s u l t in 1 step :d i g i t a l W r i t e ( stepPinX , HIGH) ;de layMicroseconds ( 2 0 0 0 ) ;d i g i t a l W r i t e ( stepPinX , LOW) ;de layMicroseconds ( 2 0 0 0 ) ;

    }delay ( 1 5 0 0 ) ;x≠=k ;

    }

    void halfSquareUp (){d i g i t a l W r i t e ( dirPinY , LOW) ;for ( f l o a t i = 0 ; i < 0 .4� s tepsPerRevo lut ion ; i++) {

    // These four l i n e s r e s u l t in 1 step :d i g i t a l W r i t e ( stepPinY , HIGH) ;de layMicroseconds ( 3 0 0 0 ) ;d i g i t a l W r i t e ( stepPinY , LOW) ;de layMicroseconds ( 3 0 0 0 ) ;

    }delay ( 1 5 0 0 ) ;

    34

  • y≠=0.5;}

    void halfSquareDown (){d i g i t a l W r i t e ( dirPinY , HIGH) ;for ( f l o a t i = 0 ; i < 0 .4� s tepsPerRevo lut ion ; i++) {

    // These four l i n e s r e s u l t in 1 step :d i g i t a l W r i t e ( stepPinY , HIGH) ;de layMicroseconds ( 3 0 0 0 ) ;d i g i t a l W r i t e ( stepPinY , LOW) ;de layMicroseconds ( 3 0 0 0 ) ;

    }delay ( 1 5 0 0 ) ;y+=0.5;

    }

    void ha l fSquareRight ( ){d i g i t a l W r i t e ( dirPinX , HIGH) ;for ( f l o a t i = 0 ; i < 0 .4� s tepsPerRevo lut ion ; i++) {

    // These four l i n e s r e s u l t in 1 step :d i g i t a l W r i t e ( stepPinX , HIGH) ;de layMicroseconds ( 3 0 0 0 ) ;d i g i t a l W r i t e ( stepPinX , LOW) ;de layMicroseconds ( 3 0 0 0 ) ;

    }delay ( 1 5 0 0 ) ;x+=0.5;

    }

    void ha l fSquareLe f t ( ){d i g i t a l W r i t e ( dirPinX , LOW) ;for ( f l o a t i = 0 ; i < 0 .4� s tepsPerRevo lut ion ; i++) {

    // These four l i n e s r e s u l t in 1 step :d i g i t a l W r i t e ( stepPinX , HIGH) ;de layMicroseconds ( 3 0 0 0 ) ;d i g i t a l W r i t e ( stepPinX , LOW) ;de layMicroseconds ( 3 0 0 0 ) ;

    }delay ( 1 5 0 0 ) ;x≠=0.5;

    }

    void returnToStart ( ){squareUp ( y ) ;

    35

  • APPENDIX B. ARDUINO CODE

    squareRight (7≠x ) ;}

    // Functions for the Hall≠e f f e c t s enso r// Function that read and re tu rn s the value g iven by the Hall≠e f f e c t s enso rf l o a t readHal l ( ){

    d i g i t a l W r i t e ( e lectromagnetPin , HIGH) ;de lay ( 2 0 0 ) ;d i g i t a l W r i t e ( e lectromagnetPin , LOW) ;de lay ( 3 0 0 ) ;ha l lVa lue=analogRead ( ha l lSenso rP in ) ;

    }// Function that s t o r e s va lue s g iven by the Hall≠e f f e c t s enso r// input : the counter for the Hall≠sensor , and h a l l v a l u ei n t s t o r e H a l l ( ){

    i n t row = x ;i n t c o l = y ;i f ( ha l lValue >=818){ // white p i e c e

    board1 [ row ] [ c o l ]=1;}else i f ( ha l lVa lue =800){// black p i e c e

    board1 [ row ] [ c o l ]=2;}else {//no detec ted p i e c e

    board1 [ row ] [ c o l ]=0;}

    }

    // Functions used for the communication between the boards// Function that scans the board and maps the p i e c e si n t scanBoard ( ){

    returnToStart ( ) ;while (y0){squareLe f t ( 1 ) ;r eadHal l ( ) ;s t o r e H a l l ( ) ;

    }}else {

    36

  • while (x

  • APPENDIX B. ARDUINO CODE

    else i f ( board2 [ i c ] [ j c ]≠board1 [ i c ] [ j c ] != 0){cArray [ 3 ] = j c ; //xTocArray [ 4 ] = i c ; //yTo

    }}

    }}

    }// Function that counts and compares the number o f p i e c e s on each board ,// r e tu rn s 1 i f the same number , else 0boolean sameNOP(){

    // Counters used to s t o r e the number o f p i e c e s on each r e p s e c t i v e boardi n t nop1=0; // Number o f p i e c e s , board1i n t nop2=0;i n t comp ;for ( i c =0; i c

  • else {squareUp (y≠yTarget ) ;

    }i f (x3){

    halfSquareUp ( ) ;}else i f (y512 && hal lValue 545){

    squareRight (7≠x ) ;ha l fSquareRight ( ) ;d i g i t a l W r i t e ( e lectromagnetPin , LOW) ;ha l fSquar eLe f t ( ) ;

    }halfSquareUp ( ) ;

    }

    // Function that changes the p o s i t i o n o f a p iece , help≠functionvoid movePiece ( ){

    d i g i t a l W r i t e ( e lectromagnetPin , HIGH) ;

    i f ( xTarget>=x){

    39

  • APPENDIX B. ARDUINO CODE

    hal fSquareRight ( ) ;}else {

    ha l fSquar eLe f t ( ) ;}

    i f (y>=yTarget ){squareUp (y≠yTarget ) ;halfSquareUp ( ) ;

    }else {

    squareDown ( yTarget≠y ) ;halfSquareDown ( ) ;

    }i f ( xTarget>=x){

    hal fSquareRight ( ) ;}else {

    ha l fSquar eLe f t ( ) ;}i f (x>=xTarget ){

    squareLe f t (x≠xTarget ) ;}else {

    squareRight ( xTarget≠x ) ;}i f (y>yTarget ){

    halfSquareUp ( ) ;}else {

    halfSquareDown ( ) ;}d i g i t a l W r i t e ( e lectromagnetPin , LOW) ;

    }

    // Function that move p i e c e to i t s new l o c a t i o nvoid moveToNew(){

    yTarget = cArray [ 2 ] ; // yFromxTarget = cArray [ 1 ] ; // xFrom

    moveToTarget ( ) ;yTarget = cArray [ 4 ] ; // yTo

    40

  • xTarget = cArray [ 3 ] ; // xTo

    movePiece ( ) ;returnToStart ( ) ;memcpy( board1 , board2 , s i z e o f ( board2 ) ) ;

    }

    // Function that move the p i e c e d i f f e r e n t l y depending on i f sameNOPvoid opponentsMove ( ){

    boolean same = sameNOP ( ) ;i f ( same==0){ // The opponent captured a p i e c e

    removePiece ( ) ;}moveToNew ( ) ;

    }

    // Gets the boards tate and i f i t i s the Arduino ’ s turn from the webs i te// Returns true i f i t ’ s the Arduinos turnboolean getData ( ){

    getConnect ion ( ) ;S t r ing getData = ”GET /ardCheck . php” ; // The webpage conta in ing the data

    // Prepare the s e r v e r for the amount o f bytes sent ;sendCommand( ”AT+CIPSEND=0,”

    +St r ing ( getData . l ength ()+2) ,4 , ”>” ) ;

    // This command can not be sent with sendCommand as// i t w i l l not save the re sponseesp8266 . p r i n t l n ( getData ) ;i n t i = 0 ;S t r ing temp ;

    // Allow the esp8266 some time to send and record responsewhile ( i 0){temp=esp8266 . r eadSt r ing ( ) ;

    }else {

    i +=1;}

    }

    //The va lue s from the s e r v e r s t a r t s at index 49 ,// i t conta in s one value for p laye r and 64 for p i e c e s

    41

  • APPENDIX B. ARDUINO CODE

    Str ing data = temp . sub s t r i ng (49 , 49+65);i n t count = 0 ; // Used to parse through the data

    //The f i r s t r e tu rn ing value i s what p laye r made the l a s t move ,// 1 for Arduino , 2 for web browser , 0 for new gamei n t p laye r = data . sub s t r i ng ( count , count +1). t o In t ( ) ;count++;

    //Goes through the r e s t o f the data and saves the boards tate i t conta in sfor ( i n t i c =0; i c

  • sendCommand( ”AT+CIPCLOSE=0” ,5 , ”OK” ) ;sendCommand( ”AT+CWQAP” ,5 , ”OK” ) ;

    }

    // E s t a b l i s h e s a connect ion between the s e r v e r and Arduinovoid getConnect ion ( ){

    // Sets the esp8266 in c l i e n t modesendCommand( ”AT+CWMODE=1” ,5 , ”OK” ) ;// Get a connect ion with the w i f isendCommand( ”AT+CWJAP=\””+AP+”\” ,\ ””+PASS+”\”” ,20 , ”OK” ) ;// Ready the esp8266 to accept connec t i onssendCommand( ”AT+CIPMUX=1” ,5 , ”OK” ) ;// Get a connect ion with the s e r v e rsendCommand( ”AT+CIPSTART=0,\”TCP\” ,\ ””+HOST+”\” , ”+PORT,1 5 , ”OK” ) ;

    }

    // Sends AT commands through the esp8266//command i s the AT command to be executed ,// maxTime i s the maximum time i t i s a l lowed be f o r e the// command f a i l s , r e sponse i s the expected response// from the esp8266 i f the command succeedsvoid sendCommand( St r ing command, i n t maxTime , char re sponse [ ] ) {

    i n t counter = 0 ;//some commands take some time be f o r e a re sponse i s sentwhile ( counter < (maxTime�1)){

    esp8266 . p r i n t l n (command) ;

    // i f the s u c c e s s f u l l message i s read , returni f ( esp8266 . f i n d ( re sponse ) ){

    break ;}counter++;

    }}

    // Converts a coord inate in to a ASCII charachte r// A 0 get converted to ’ a ’ , 1 to ’b ’ e t c .S t r ing convertASCII ( i n t coo ){

    char ASCII = coo + 97 ;return St r ing ( ASCII ) ;}

    // The game loop o f the system

    43

  • APPENDIX B. ARDUINO CODE

    void loop ( ) {buttonState=d ig i ta lRead ( buttonPin ) ;i f ( buttonState==HIGH){

    i f ( getData ( ) ) {d i sc repancy ( 2 ) ;i f ( cArray [0]==1){ // The opponent has made a move

    opponentsMove ( ) ;}else { // I t i s your move

    scanBoard ( ) ;d i s c repancy ( 1 ) ;i f ( cArray [0]==1){ // You have made a move

    sendData ( cArray [ 1 ] , cArray [ 2 ] , cArray [ 3 ] , cArray [ 4 ] ) ;}

    }}

    }}

    44

  • Appendix C

    Website code

    These codes were used to create the website that could communicate with theArduino. they were all placed in the www directory created by Wampserver.

    C.1 index.html

    Welcome!

    Make move

    Show boardstate

    Reset board

    45

  • APPENDIX C. WEBSITE CODE

    C.2 send.html

    Move from:X value :
    Y value :
    Move to :X value :
    Y value :

    Home

    C.3 update.php

  • C.3. UPDATE.PHP

    // Create connect ion to the database$dbconnect=mysq l i connect ( $hostname , $username , $password , $db ) ;

    // Get the va lue s sent$xFrom = $ GET [ ’ xfrom ’ ] ;$yFrom = $ GET [ ’ yfrom ’ ] ;$xTo = $ GET [ ’ xto ’ ] ;$yTo = $ GET [ ’ yto ’ ] ;

    // I f the Arduino makes a move , $player ge t s set to 1 ,// otherwi s e i t s the webs i te and $player ge t s set to 2i f ( ! i s s e t ($ GET [ ’ p layer ’ ] ) ) {

    $player =2;}else {

    $player=$ GET [ ’ player ’ ] ;}

    // Gets the type o f p i e c e that ge t s moved and s t o r e s i t in $obj$getP iece = ”SELECT p i e c e from $ t a b l e p o s i t i o n sWHERE x=$xFrom AND y=$yFrom” ;i f ( $ r e s u l t = mysql i query ( $dbconnect , $getP iece ) ) {

    $obj = m y s q l i f e t c h o b j e c t ( $ r e s u l t ) ;}

    // Updates the database with the new move$from = ”UPDATE $ t a b l e p o s i t i o n s SET p i e c e=0WHERE x=$xFrom AND y=$yFrom” ;

    $to = ”UPDATE $ t a b l e p o s i t i o n s SET p i e c e=$obj≠>p i e c eWHERE x=$xTo AND y=$yTo” ;

    mysq l i query ( $dbconnect , $from ) ;mysq l i query ( $dbconnect , $to ) ;// This s e t s the new move as the l a s t move made$ s e tLas t = ”UPDATE $what move SET xfrom = $xFrom , yfrom = $yFrom ,

    xto = $xTo , yTo = $yTo , atTime = CURRENT TIMESTAMP, p laye r= $player ” ;mysq l i query ( $dbconnect , $ s e tLas t ) ;

    // Disconnect from the databasem y s q l i c l o s e ( $dbconnect ) ;?>

    47

  • APPENDIX C. WEBSITE CODE

    Show boardstate

    C.4 showboard.php

    should be viewed and s t a r t s c r e a t i n g the tab l e ≠≠>1 = White , 2 = Black , 0 = No p i e c e

    boardabcdef gh

  • C.5. RESET.PHP

    while ( $row number >0){echo

    ”{$row number}” ;$query = mysql i query ( $dbconnect ,

    ”SELECT p i e c e FROM $ t a b l e p o s i t i o n s WHERE y=$row number ORDER BY x” ) ;while ( $row = m y s q l i f e t c h a r r a y ( $query ) ) {

    echo ”{$row [0]} ” ;}echo ”” ;$row number ≠= 1 ;

    }

    // Gets i n f o about the l a s t move from the database// and d i s p l a y s the in fo rmat ion$que = mysql i query ( $dbconnect , ”SELECT � FROM $what move” ) ;$ i n f o = m y s q l i f e t c h a r r a y ( $que ) ;i f ( $ i n f o [ ’ xfrom ’ ] ) {

    echo ”The l a s t move was { $ i n f o [ ’ xfrom ’ ] }{ $ i n f o [ ’ yfrom ’ ] } to { $ i n f o [ ’ xto ’ ] } { $ i n f o [ ’ yto ’ ] }at { $ i n f o [ ’ atTime ’ ] } . Made from playe r { $ i n f o [ ’ p layer ’ ] } ” ;

    }else {

    echo”New game” ;}// Disconnectm y s q l i c l o s e ( $dbconnect ) ;?>

    Home

    C.5 reset.php

  • APPENDIX C. WEBSITE CODE

    $password = ” your password ” ;$db = ” p o s i t i o n s ” ; // The name o f the database// The name o f the t ab l e conta in ing the p o s i t i o n o f a l l p i e c e s$ t a b l e p o s i t i o n s =” pos ” ;// The name o f the t ab l e conta in ing in fo rmat ion on the l a s t move made$what move =” last move ” ;// Create connect ion to the database$dbconnect=mysq l i connect ( $hostname , $username , $password , $db ) ;

    // Sets every p o s i t i o n to 0$ r s t= ”UPDATE $ t a b l e p o s i t i o n s SET p i e c e = 0” ;// Sets p i e c e s on row 1 and 2 to 1$ r s t w h i t e =”UPDATE $ t a b l e p o s i t i o n s SET p i e c e = 1 WHERE y = 1 OR y = 2” ;// Sets p i e c e s on row 7 and 8 to 2$ r s t b l a c k =”UPDATE $ t a b l e p o s i t i o n s SET p i e c e = 2 WHERE y = 7 OR y = 8” ;// Resets who made the l a s t move$ r s t l a s t m o v e = ”UPDATE $what move SET xfrom = NULL, p laye r = 0” ;

    mysq l i query ( $dbconnect , $ r s t ) ;mysq l i query ( $dbconnect , $ r s t w h i t e ) ;mysq l i query ( $dbconnect , $ r s t b l a c k ) ;mysq l i query ( $dbconnect , $ r s t l a s t m o v e ) ;// Close connect ionm y s q l i c l o s e ( $dbconnect ) ;?>

    Home

    C.6 ardCheck.php

  • C.6. ARDCHECK.PHP

    $db = ” p o s i t i o n s ” ; // The name o f the database// The name o f the t ab l e conta in ing the p o s i t i o n o f a l l p i e c e s$ t a b l e p o s i t i o n s =” pos ” ;// The name o f the t ab l e conta in ing in fo rmat ion on the l a s t move made$what move =” last move ” ;// Create connect ion to the database$dbconnect=mysq l i connect ( $hostname , $username , $password , $db ) ;$query = mysql i query ( $dbconnect , ”SELECT player FROM $what move” ) ;$obj = m y s q l i f e t c h o b j e c t ( $query ) ;// Sends what p laye r made the l a s t move to the Arduinoecho”{ $obj≠>p laye r }” ;// Gets the boards tate from the database and sends i t to the Arduino$row number = 8 ;while ( $row number >0){

    $query = mysql i query ( $dbconnect ,”SELECT p i e c e FROM $ t a b l e p o s i t i o n s WHERE y=$row number ORDER BY x” ) ;

    while ( $row = m y s q l i f e t c h a r r a y ( $query ) ) {echo ”{$row [ ’ p iece ’ ] } ” ;

    }$row number ≠= 1 ;

    }// Close connect ionm y s q l i c l o s e ( $dbconnect ) ;?>

    51

  • Appendix D

    Components

    D.1 Product sheet

    53

  • Sura Magnets AB Telefon +46 (0) 121 353 10Hamragatan 2 Fax +46 (0) 121 353 15S-614 31 Söderköping E-mail [email protected]/Sweden Internet www.suramagnets.se

    Partnumber: NA35-2-005-3Material : Neodym 35

    Dimensions D H5 mm 5 mm

    Magnetisation: Axially magnetized

    Grade Max Worktemp

    Min Typ Min Typ Min TypN35 1,17 1,21 860 915,4 262,7 278,6 =955

    Energi product( T ) ( kA/m ) ( kA/m ) BH max ( kJ/m3 )

    Remanence Coercivity BHc Coercivity Ihc

  • Sura Magnets AB Telefon +46 (0) 121 353 10Hamragatan 2 Fax +46 (0) 121 353 15S-614 31 Söderköping E-mail [email protected]/Sweden Internet www.suramagnets.se

    Partnumber: FA-2-012-1Material : Ferrite Y28

    Dimensions D H12 mm 6 mm

    Magnetisation: Axially magnetized

    Grade

    Min Typ Min Typ Min Typ Min TypY 28 370 400 175 210 180 220 26 30

    Energi product( mT ) ( kA/m ) ( kA/m ) BH max ( kJ/m3 )

    Remanence Coercivity BHc Coercivity Ihc

  • TRITA ITM-EX 2020:38

    www.kth.se


Recommended