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