-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathworking_example.ino
377 lines (368 loc) · 10.5 KB
/
working_example.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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
// Example sketch to read the ID from an Addicore 13.56MHz RFID tag
// as found in the RFID AddiKit found at:
// http://www.addicore.com/RFID-AddiKit-with-RC522-MIFARE-Module-RFID-Cards-p/126.htm
#include <AddicoreRFID.h>
#include <SPI.h>
#define uchar unsigned char
#define uint unsigned int
uchar fifobytes;
uchar fifoValue;
AddicoreRFID myRFID; // create AddicoreRFID object to control the RFID module
/////////////////////////////////////////////////////////////////////
//set the pins
/////////////////////////////////////////////////////////////////////
const int chipSelectPin = 10;
const int NRSTPD = 9;
String card = "";
String ucard0 = "";
String ucard1 = "";
String ucard2 = "";
String ucard3 = "";
uint32_t myCard;
int currentCard;
String playerOneCard = "";
String playerTwoCard = "";
int playerOneScore = 0;
int playerTwoScore = 0;
String result = "";
bool gameOver = false;
//Maximum length of the array
#define MAX_LEN 16
int i;
void LightUpHigh(int port)
{
for(i = 0; i < 4; i++){
digitalWrite(port,HIGH);
delay(200);
digitalWrite(port,LOW);
delay(200);
digitalWrite(port,HIGH);
}
}
void LightUpScoreOne(int score)
{
for(i = 0; i < 4; i++){
digitalWrite(2,HIGH);
digitalWrite(3,HIGH);
digitalWrite(4,HIGH);
delay(200);
digitalWrite(2,LOW);
digitalWrite(3,LOW);
digitalWrite(4,LOW);
delay(200);
}
if(playerOneScore == 1)
digitalWrite(2,HIGH);
if(playerOneScore == 2){
digitalWrite(2,HIGH);
digitalWrite(3,HIGH);
}
if(playerOneScore >= 3){
digitalWrite(2,HIGH);
digitalWrite(3,HIGH);
digitalWrite(4,HIGH);
}
}
void LightUpScoreTwo(int score)
{
for(i = 0; i < 4; i++){
digitalWrite(5,HIGH);
digitalWrite(6,HIGH);
digitalWrite(7,HIGH);
delay(200);
digitalWrite(5,LOW);
digitalWrite(6,LOW);
digitalWrite(7,LOW);
delay(200);
}
if(playerTwoScore == 1)
digitalWrite(5,HIGH);
if(playerTwoScore == 2){
digitalWrite(5,HIGH);
digitalWrite(6,HIGH);
}
if(playerTwoScore >= 3){
digitalWrite(5,HIGH);
digitalWrite(6,HIGH);
digitalWrite(7,HIGH);
}
}
void LightUpTie()
{
for(i = 0; i < 4; i++){
digitalWrite(2,HIGH);
digitalWrite(3,HIGH);
digitalWrite(4,HIGH);
digitalWrite(5,HIGH);
digitalWrite(6,HIGH);
digitalWrite(7,HIGH);
delay(200);
digitalWrite(2,LOW);
digitalWrite(3,LOW);
digitalWrite(4,LOW);
digitalWrite(5,LOW);
digitalWrite(6,LOW);
digitalWrite(7,LOW);
delay(200);
}
if(playerOneScore == 1)
digitalWrite(2,HIGH);
if(playerOneScore == 2){
digitalWrite(2,HIGH);
digitalWrite(3,HIGH);
}
if(playerOneScore >= 3){
digitalWrite(2,HIGH);
digitalWrite(3,HIGH);
digitalWrite(4,HIGH);
}
if(playerTwoScore == 1)
digitalWrite(5,HIGH);
if(playerTwoScore == 2){
digitalWrite(5,HIGH);
digitalWrite(6,HIGH);
}
if(playerTwoScore >= 3){
digitalWrite(5,HIGH);
digitalWrite(6,HIGH);
digitalWrite(7,HIGH);
}
}
void setup() {
Serial.begin(9600); // RFID reader SOUT pin connected to Serial RX pin at 9600bps
// start the SPI library:
SPI.begin();
pinMode(chipSelectPin,OUTPUT); // Set digital pin 10 as OUTPUT to connect it to the RFID /ENABLE pin
digitalWrite(chipSelectPin, LOW); // Activate the RFID reader
pinMode(NRSTPD,OUTPUT); // Set digital pin 10 , Not Reset and Power-down
digitalWrite(NRSTPD, HIGH);
myRFID.AddicoreRFID_Init();
pinMode(7,OUTPUT);
//digitalWrite(7,HIGH);
pinMode(6,OUTPUT);
//digitalWrite(6,HIGH);
pinMode(5,OUTPUT);
digitalWrite(5,LOW);
pinMode(4,OUTPUT);
//digitalWrite(4,HIGH);
pinMode(3,OUTPUT);
//digitalWrite(3,HIGH);
pinMode(2,OUTPUT);
//digitalWrite(2,HIGH);
}
void loop()
{
uchar i, tmp, checksum1;
uchar status;
uchar str[MAX_LEN];
uchar RC_size;
uchar blockAddr; //Selection operation block address 0 to 63
String mynum = "";
//str[1] = 0x4400;
//Find tags, return tag type
status = myRFID.AddicoreRFID_Request(PICC_REQIDL, str);
if (status == MI_OK)
{
Serial.println("RFID tag detected");
/*Serial.print("Tag Type:\t\t");
uint tagType = str[0] << 8;
tagType = tagType + str[1];
switch (tagType) {
case 0x4400:
Serial.println("Mifare UltraLight");
break;
case 0x400:
Serial.println("Mifare One (S50)");
break;
case 0x200:
Serial.println("Mifare One (S70)");
break;
case 0x800:
Serial.println("Mifare Pro (X)");
break;
case 0x4403:
Serial.println("Mifare DESFire");
break;
default:
Serial.println("Unknown");
break;
} */
}
//Anti-collision, return tag serial number 4 bytes
status = myRFID.AddicoreRFID_Anticoll(str);
if (status == MI_OK)
{
card = str[0] + str[1] + str[2] + str[3];
checksum1 = str[0] ^ str[1] ^ str[2] ^ str[3];
myCard = (uint32_t)str[0] << 24 | (uint32_t)str[1] << 16 | (uint32_t)str[2] << 8 | (uint32_t)str[3];
currentCard = (int)myCard;
Serial.print("The tag's number is:\t");
Serial.print(str[0]);
Serial.print(" , ");
Serial.print(str[1]);
Serial.print(" , ");
Serial.print(str[2]);
Serial.print(" , ");
Serial.println(str[3]);
Serial.println(currentCard);
Serial.print("Read Checksum:\t\t");
Serial.println(str[4]);
Serial.print("Calculated Checksum:\t");
Serial.println(checksum1);
// Should really check all pairs, but for now we'll just use the first
if(str[0] == 197) //You can change this to the first byte of your tag by finding the card's ID through the Serial Monitor
{
Serial.println("\nHello Craig!\n");
} else if(str[0] == 244) { //You can change this to the first byte of your tag by finding the card's ID through the Serial Monitor
Serial.println("\nHello Erin!\n");
}
Serial.println();
delay(1000);
if (checksum1 == 209) { //reset //155 card, 54 fob
Serial.println("RESETTING");
// val = 0;
playerOneCard = "";
playerTwoCard = "";
playerOneScore = 0;
playerTwoScore = 0;
//winner = "";
result = "reset";
gameOver = false;
LightUpTie();
} else if (!gameOver){
if (checksum1 == 87){
Serial.println("Player one selects rock.");
LightUpScoreOne(playerOneScore);
playerOneCard = "rock";
} else if (checksum1 == 253){
Serial.println("Player one selects paper.");
LightUpScoreOne(playerOneScore);
playerOneCard = "paper";
} else if (checksum1 == 60){
Serial.println("Player one selects scissors.");
LightUpScoreOne(playerOneScore);
playerOneCard = "scissors";
} else if (checksum1 == 25){
Serial.println("Player two selects rock.");
LightUpScoreTwo(playerTwoScore);
playerTwoCard = "rock";
} else if (checksum1 == 250){
Serial.println("Player two selects paper.");
LightUpScoreTwo(playerTwoScore);
playerTwoCard = "paper";
} else if (checksum1 == 217){
Serial.println("Player two selects scissors.");
LightUpScoreTwo(playerTwoScore);
playerTwoCard = "scissors";
}
}
}
//val = 0;
if (playerOneCard != "" && playerTwoCard != ""){
startGame();
}
myRFID.AddicoreRFID_Halt(); //Command tag into hibernation
}
void startGame(){
if (playerOneCard == "rock" && playerTwoCard == "rock"){
Serial.println("tie");
LightUpTie();
result = "tie";
playerOneCard = "";
playerTwoCard = "";
} else if (playerOneCard == "rock" && playerTwoCard == "paper"){
Serial.println("Player Two Wins!");
playerTwoScore++;
LightUpScoreTwo(playerTwoScore);
result = "playerTwo";
playerOneCard = "";
playerTwoCard = "";
} else if (playerOneCard == "rock" && playerTwoCard == "scissors"){
Serial.println("Player One Wins!");
playerOneScore++;
LightUpScoreOne(playerOneScore);
result = "playerOne";
playerOneCard = "";
playerTwoCard = "";
} else if (playerOneCard == "paper" && playerTwoCard == "rock"){
Serial.println("Player One Wins!");
playerOneScore++;
LightUpScoreOne(playerOneScore);
result = "playerOne";
playerOneCard = "";
playerTwoCard = "";
} else if (playerOneCard == "paper" && playerTwoCard == "paper"){
Serial.println("tie");
LightUpTie();
result = "tie";
playerOneCard = "";
playerTwoCard = "";
} else if (playerOneCard == "paper" && playerTwoCard == "scissors"){
Serial.println("Player Two Wins!");
playerTwoScore++;
LightUpScoreTwo(playerTwoScore);
result = "playerTwo";
playerOneCard = "";
playerTwoCard = "";
} else if (playerOneCard == "scissors" && playerTwoCard == "rock"){
Serial.println("Player Two Wins!");
playerTwoScore++;
LightUpScoreTwo(playerTwoScore);
result = "playerTwo";
playerOneCard = "";
playerTwoCard = "";
} else if (playerOneCard == "scissors" && playerTwoCard == "paper"){
Serial.println("Player One Wins!");
playerOneScore++;
LightUpScoreOne(playerOneScore);
result = "playerOne";
playerOneCard = "";
playerTwoCard = "";
} else if (playerOneCard == "scissors" && playerTwoCard == "scissors"){
Serial.println("tie");
LightUpTie();
result = "tie";
playerOneCard = "";
playerTwoCard = "";
}
playerOneCard = "";
playerTwoCard = "";
//displayResults();
}
/*
void displayResults(){
if(result == "playerOne") {
playerOneScore++;
//light current Result LED
if (playerOneScore == 1){
//light LED1
} else if (playerOneScore == 2){
//light LED1
//light LED2
} else if (playerOneScore == 3){
//light LED1
//light LED2
//light LED3
gameOver = true;
}
} else if (result == "playerTwo"){
playerTwoScore++;
//light current Result LED
if (playerTwoScore == 1){
//light LED1
} else if (playerTwoScore == 2){
//light LED1
//light LED2
} else if (playerTwoScore == 3){
//light LED1
//light LED2
//light LED3
gameOver = true;
}
} else if (result == "tie"){
//light currentResult middle LED
} else if (result == "reset"){
//handle all resetting, all LEDS off
//game was reset
}
*/