Components and supplies
Stepper Motor, Mini Step
AC/DC Power Supply, 12 V
Breadboard (generic)
Grove - Electromagnet
Arduino UNO
Limit Switch, 5 A
Stepper motor driver board A4988
Project description
Code
Automated Chess piece mover code
arduino
Copy and paste to your Arduino IDE!
1const int stepsBetweenSquares = 500; 2const int stepPinX = 8; 3const int dirPinX = 9; 4const int stepPinY = 7; 5const int dirPinY = 6; 6const int limitSwitchX = 5; 7const int limitSwitchY = 4; 8const int magnetPin = 2; 9 10 11 12int x1 = 0; int x2 = 1; 13int y1 = 0; int y2 = 1; 14int xsteps; 15int ysteps; 16int x = 0; 17int y = 0; 18 19void setup() { 20 Serial.begin(9600); 21 pinMode(stepPinX,OUTPUT); 22 pinMode(dirPinX,OUTPUT); 23 pinMode(stepPinY,OUTPUT); 24 pinMode(dirPinY,OUTPUT); 25 pinMode(limitSwitchX, INPUT); 26 pinMode(limitSwitchY, INPUT); 27 pinMode(magnet, OUTPUT); 28 29 30 zeroDiagonal(); 31 digitalWrite(magnetPin, LOW); 32 33 34 35} 36 37 38void loop() { 39 40 x1 = 0; x2 = 1; 41 y1 = 0;y2 = 1; 42 43 44 Serial.print("Piece to be moved letter adress: "); 45 while (!Serial.available()); 46 x1 = readSerialNumber(); 47 48 Serial.print("Piece to be moved number adress: "); 49 while (!Serial.available()); 50 y1 = readSerialNumber(); 51 52 53 54 55 xsteps = stepsBetweenSquares*(x1-x2); 56 ysteps = stepsBetweenSquares*(y1-y2); 57 58 movePieceDiagonal(xsteps, ysteps); 59 60 digitalWrite(magnetPin, HIGH); 61 62//_______________________________________________________________________________________________ 63// Recieve position adress of piece new location from Serial Monitor (Letter, Number) 64//----------------------------------------------------------------------------------------------- 65 Serial.print("New location letter adress: "); 66 while (!Serial.available()); 67 x2 = readSerialNumber(); 68 69 Serial.print("New location number adress: "); 70 while (!Serial.available()); 71 y2 = readSerialNumber(); 72 Serial.println(); 73 74 75 76 77 78 79 xsteps = stepsBetweenSquares*(x2-x1); 80 ysteps = stepsBetweenSquares*(y2-y1); 81 82 digitalWrite(dirPinX, HIGH); digitalWrite(dirPinY, LOW); 83 offsetPiece(); 84 delay(50); 85 movePiece(xsteps, dirPinX, stepPinX); 86 movePiece(-ysteps, dirPinY, stepPinY); 87 delay(50); 88 digitalWrite(dirPinX, LOW); digitalWrite(dirPinY, HIGH); 89 offsetPiece(); 90 digitalWrite(magnetPin, LOW); 91 92while(true){} 93 94} 95 96 97 98 99 100 101////////////////////////////////////////////////////////////////////////////////////////////////////// 102 103int readSerialNumber(){ 104 int i; 105 while (i < 1 || i > 8){ 106 i = Serial.parseInt(); 107 } 108 Serial.println(i); 109 Serial.parseInt(); 110 return i; 111 } 112 113 114 115void zeroDiagonal(){ 116 digitalWrite(dirPinX, LOW); 117 digitalWrite(dirPinY, HIGH); 118 while (digitalRead(limitSwitchX) == HIGH && digitalRead(limitSwitchY) == HIGH){ 119 moveDiagonal(1.5); 120 } 121 while (digitalRead(limitSwitchX) == HIGH){ 122 moveLinear(stepPinX, 1.5); 123 } 124 while (digitalRead(limitSwitchY) == HIGH){ 125 moveLinear(stepPinY, 1.5); 126 } 127 while (digitalRead(limitSwitchX) == LOW){ 128 for (int i = 0; i<130; i++){ 129 digitalWrite(dirPinX, HIGH); 130 digitalWrite(stepPinX, HIGH); 131 delay(2); 132 digitalWrite(stepPinX, LOW); 133 delay(2); 134 } 135 136 } 137 138 139 while (digitalRead(limitSwitchY) == LOW){ 140 digitalWrite(dirPinY, LOW); 141 for (int i = 0; i<90; i++){ 142 digitalWrite(stepPinY, HIGH); 143 delay(2); 144 digitalWrite(stepPinY, LOW); 145 delay(2); 146 } 147} 148} 149/////////////////////////////////////////////////////////////////////////////////////////////////// 150 151void movePiece(int steps, int dirPin, int stepPin){ 152 if (steps > 0){ 153 digitalWrite(dirPin,HIGH); 154 } 155 else{ 156 steps = abs(steps); 157 digitalWrite(dirPin, LOW); 158 } 159 for(int i = 0; i < steps; i++) { 160 moveLinear(stepPin, 2); 161 } 162 } 163 164//////////////////////////////////////////////////////////////////////////////////////////////////////// 165 166void movePieceDiagonal(int xsteps, int ysteps){ 167 if (ysteps >= 0){ 168 digitalWrite(dirPinY, LOW); 169 } 170 else{ 171 ysteps = abs(ysteps); 172 digitalWrite(dirPinY, HIGH); 173 } 174 if (xsteps >= 0){ 175 digitalWrite(dirPinX, HIGH); 176 } 177 else{ 178 xsteps = abs(xsteps); 179 digitalWrite(dirPinX, LOW); 180 } 181 x = 0; y = 0; 182 while (x < xsteps && y < ysteps){ 183 moveDiagonal(2); 184 x++; y++; 185 } 186 for(x; x < xsteps; x++){ 187 moveLinear(stepPinX, 2); 188 } 189 for(y; y < ysteps; y++){ 190 moveLinear(stepPinY, 2); 191 } 192} 193 194///////////////////////////////////////////////////////////////////////////////////////////////////////// 195 196void offsetPiece(){ 197 for (int i=0; i < stepsBetweenSquares/2; i++){ 198 moveDiagonal(2); 199 } 200} 201 202/////////////////////////////////////////////////////////////////////////////////////////////////////////// 203 204void moveDiagonal(int delayTime){ 205 digitalWrite(stepPinX, HIGH); 206 digitalWrite(stepPinY, HIGH); 207 delay(delayTime); 208 digitalWrite(stepPinX, LOW); 209 digitalWrite(stepPinY, LOW); 210 delay(delayTime); 211} 212 213//////////////////////////////////////////////////////////////////////////////////////////////////////////////// 214 215void moveLinear(int stepPin, int delayTime){ 216 digitalWrite(stepPin, HIGH); 217 delay(delayTime); 218 digitalWrite(stepPin, LOW); 219 delay(delayTime); 220} 221
Comments
Only logged in users can leave comments
Moustafaidris97
0 Followers
•0 Projects
Table of contents
Intro
14
0