-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTicTacToe.ino
287 lines (257 loc) · 5.42 KB
/
TicTacToe.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
/*need to figure out where to bring drawing back to
*can figure out how many steps to travel whole board
*calculate how many steps from origin
*/
#include <SPI.h>
#include <Pixy.h>
#include <Servo.h>
//each move corresponds to robot's moves, not total
boolean move1 = false;
boolean move2 = false;
boolean rest = false;
boolean over = false;
int taken[3][3] = { //0 is empty, 1 is robot, 2 is human
{0, 0, 0},
{0, 0, 0},
{0, 0, 0}};
int x = 0;
int y = 0;
static int i = 0;
Pixy pixy;
uint16_t blocks;
Servo marker;
void setup() {
Serial.begin(9600);
pinMode(2, OUTPUT); //left direction
pinMode(3, OUTPUT); //left step
pinMode(4, OUTPUT); //right direction
pinMode(5, OUTPUT); //right step
pinMode(8, OUTPUT); //C direction
pinMode(9, OUTPUT); //C step
pinMode(6, OUTPUT); //servo
pinMode(12, INPUT); //turn button
marker.attach(6);
marker.write(180);
delay(3000);
// toOrigin();
pixy.init();
//First move
goTo(0, 0);
drawX();
toOrigin(0,0);
move1 = true;
taken[0][0] = 1;
Serial.println("did it again");
}
void loop() {
blocks = pixy.getBlocks();
//pixy range = 2.816
//Row steps: 2100
//Column steps: 1700
/*boolean waiting = true.;
while(waiting)
{
if(digitalRead(12) == 1)
waiting = false;
}
Serial.println(waiting);*/
/* for(int r = 0; r < 3; r++)
{
for(int c = 0; c < 3; c++)
{
goTo(r, c);
drawX();
toOrigin(r, c);
}
}*/
/* int humanR = 0;
int humanC = 0;
humanR = map(pixy.blocks[0].y, 18, 163, 0, 3);
humanC = map(pixy.blocks[0].x, 97, 245, 0, 3);
Serial.print(humanR);
Serial.print(", ");
Serial.println(humanC);*/
if(move1)
{
int humanR = -1;
int humanC = -1;
boolean waiting = true;
Serial.println("waiting");
while(waiting)
{
if(digitalRead(12) == 1)
waiting = false;
}
/* while(!blocks)
{
Serial.println("Blocks");
blocks = pixy.getBlocks();
}*/
Serial.println(blocks);
//figure out which square the O is here vv
delay(2000);
uint16_t blocks;
int x = 0;
int y = 0;
blocks = pixy.getBlocks();
if (blocks)
{
i++;
if (i%50==0)
{
x = pixy.blocks[0].x + pixy.blocks[0].width/2;
y = pixy.blocks[0].y + pixy.blocks[0].height/2;
}
}
// Serial.println(pixy.blocks[0].y);
// blocks = pixy.getBlocks();
humanR = map(y, 18, 163, 0, 300);
humanC = map(x, 97, 245, 0, 300);
//taken[humanR][humanC] = 2;
Serial.print(humanR);
Serial.print(" ");
Serial.println(humanC);
// delay(2000);
if(humanR == 1 && humanC == 1)
{
goTo(1, 2);
drawX();
toOrigin(1, 2);
taken[1][2] = 1;
}
else if(humanR == 0 && humanC == 2)
{
goTo(1, 0);
drawX();
toOrigin(1, 0);
taken[1][0] = 1;
}
else
{
goTo(0, 2);
drawX();
toOrigin(0, 2);
taken[0][2] = 1;
}
move1 = false;
move2 = true;
}
if(move2)
{
boolean humanMoved = false;
int humanR = -1;
int humanC = -1;
boolean waiting = true;
while(waiting)
{
Serial.println("waiting");
if(digitalRead(12) == 1)
waiting = false;
}
// Serial.println(blocks);
//figure out which square the O is here vv
delay(2000);
uint16_t blocks;
while(blocks == 0){
blocks = pixy.getBlocks();
}
delay(3000);
/* if (blocks)
{
i++;
if (i%50==0)
{
}
} */
// boolean newMove = false;
// while (!newMove)
// {
humanR = map(pixy.blocks[0].y, 18, 163, 0, 300);
humanC = map(pixy.blocks[0].x, 97, 245, 0, 300);
// taken[humanR][humanC] = 2;
/*if(taken[humanR][humanC]==2)
{
humanR = map(pixy.blocks[1].y, 18, 163, 0, 2);
humanC = map(pixy.blocks[1].x, 97, 245, 0, 2);
newMove = true;
}
taken[humanR][humanC] = 2;
}*/
Serial.print(humanR);
Serial.print(", ");
Serial.println(humanC);
/* Serial.print(map(pixy.blocks[0].y, 18, 163, 0, 200));
Serial.print(", ");
Serial.println(map(pixy.blocks[0].x, 97, 245, 0, 200));*/
if(taken[1][1]) //if human has gone in center
{
if(humanR == 0 && humanC == 1)
{
goTo(2, 1);
drawX();
toOrigin(2, 1);
taken[2][1] = 1;
}
else if(humanR == 0 && humanC == 2)
{
goTo(2, 0);
drawX();
toOrigin(2, 0);
taken[2][0] = 1;
}
else if(humanR == 2 && humanC == 1)
{
goTo(0, 1);
drawX();
toOrigin(0, 1);
taken[0][1] = 1;
}
else
{
goTo(0, 2);
drawX();
toOrigin(0, 2);
taken[0][2] = 1;
}
}
else
{
if(taken[2][2])
{
goTo(2, 0);
drawX();
toOrigin(2, 0);
taken[2][0] = 1;
}
else
{
goTo(1, 1);
drawX();
toOrigin(1,1);
taken[1][1] = 1;
}
}
move2 = false;
rest = true;
}
/* if(rest)
{
boolean humanMoved = false;
int humanR = -1;
int humanC = -1;
if(blocks > 2)
{
//figure out which square the O is here vv
humanR = pixy.blocks[2].x; //needs rescaled & floored/ceilinged
humanC = pixy.blocks[2].y; //needs rescaled & floored/ceilinged
taken[humanR][humanC] = 2;
humanMoved = true;
}
if(humanMoved)
{
win();
if(!over)
block();
}
}*/
}