Skip to content
This repository was archived by the owner on Dec 20, 2024. It is now read-only.

Commit 73e326f

Browse files
Adicionar sketch para configurar cartões dos jogadores
1 parent a9baa37 commit 73e326f

1 file changed

Lines changed: 147 additions & 0 deletions

File tree

Lines changed: 147 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,147 @@
1+
#include <SPI.h>
2+
#include <MFRC522.h>
3+
4+
#define RST_PIN 9
5+
#define SS_PIN 10
6+
7+
MFRC522 mfrc522(SS_PIN, RST_PIN);
8+
9+
MFRC522::MIFARE_Key key = {
10+
.keyByte = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF }
11+
};
12+
13+
void setup()
14+
{
15+
Serial.begin(9600);
16+
SPI.begin();
17+
mfrc522.PCD_Init();
18+
}
19+
20+
void loop()
21+
{
22+
char playerName[16];
23+
bool cardAvailable = false;
24+
unsigned long cardTimeout;
25+
26+
Serial.println(F("Cadastrando novo cartão de jogador"));
27+
28+
if (!readPlayerName(playerName, sizeof(playerName))) {
29+
Serial.println(F("Nenhum dado recebido em 30 segundos, cancelando..."));
30+
}
31+
else {
32+
Serial.print(F("Nome do jogador: "));
33+
Serial.println(playerName);
34+
35+
Serial.println("Aproxime o cartão do leitor");
36+
37+
cardTimeout = millis() + 30000L;
38+
while (!cardAvailable && (millis() < cardTimeout)) {
39+
cardAvailable = isCardAvailable();
40+
}
41+
42+
if (cardAvailable && writeOnCard(playerName, sizeof(playerName))) {
43+
Serial.println("Nome do jogador foi salvo com sucesso!");
44+
}
45+
else {
46+
Serial.println("ERRO: Não foi possível salvar o nome do jogador!");
47+
}
48+
}
49+
50+
Serial.println("Finalizado!");
51+
Serial.println("---------------------------------");
52+
}
53+
54+
bool readPlayerName(char *outputBuffer, size_t bufferSize)
55+
{
56+
byte length;
57+
58+
// Limpa o buffer setando todos os bytes para 0
59+
memset(outputBuffer, 0, bufferSize);
60+
61+
Serial.println(F("Digite o nome do jogador e adicione # no final: "));
62+
63+
// Aguarda até 30 segundos para o nome ser digitado
64+
Serial.setTimeout(30000L);
65+
length = Serial.readBytesUntil('#', outputBuffer, bufferSize);
66+
67+
return (length > 0);
68+
}
69+
70+
// Funções utilitárias para acessar leitor RFID
71+
72+
bool writeOnCard(char *playerName, size_t bufferSize)
73+
{
74+
byte block = 1;
75+
bool success = false;
76+
77+
if (!authenticate(block)) {
78+
return false;
79+
}
80+
81+
if (writeBlock(block, (byte*) playerName, (byte) bufferSize)) {
82+
success = true;
83+
}
84+
85+
deauthenticate();
86+
87+
return success;
88+
}
89+
90+
bool isCardAvailable()
91+
{
92+
// Verifica se existe um cartão na frente do leitor
93+
if (!mfrc522.PICC_IsNewCardPresent()) {
94+
return false;
95+
}
96+
97+
// Verifica se é possível ler o UID do cartão
98+
return mfrc522.PICC_ReadCardSerial();
99+
}
100+
101+
bool writeBlock(byte blockAddr, byte *buffer, byte bufferSize)
102+
{
103+
MFRC522::StatusCode status;
104+
105+
status = mfrc522.MIFARE_Write(blockAddr, buffer, bufferSize);
106+
107+
if (status != MFRC522::STATUS_OK) {
108+
Serial.print(F("MIFARE_Write failed: "));
109+
Serial.println(mfrc522.GetStatusCodeName(status));
110+
111+
return false;
112+
}
113+
114+
return true;
115+
}
116+
117+
bool authenticate(byte blockAddr)
118+
{
119+
MFRC522::StatusCode status;
120+
121+
status = mfrc522.PCD_Authenticate(MFRC522::PICC_CMD_MF_AUTH_KEY_A, blockAddr, &key, &(mfrc522.uid));
122+
123+
if (status != MFRC522::STATUS_OK) {
124+
Serial.print(F("PCD_Authenticate failed: "));
125+
Serial.println(mfrc522.GetStatusCodeName(status));
126+
127+
return false;
128+
}
129+
130+
return true;
131+
}
132+
133+
void deauthenticate()
134+
{
135+
MFRC522::StatusCode status;
136+
137+
status = mfrc522.PICC_HaltA();
138+
139+
if (status != MFRC522::STATUS_OK) {
140+
Serial.print(F("PICC_HaltA failed: "));
141+
Serial.println(mfrc522.GetStatusCodeName(status));
142+
}
143+
144+
mfrc522.PCD_StopCrypto1();
145+
}
146+
147+

0 commit comments

Comments
 (0)