Sunteți pe pagina 1din 8

/*

* ----------------------------------------------------------------------------
* This is NOT a MFRC522 library example; see https://github.com/miguelbalboa/rfid
* for (further?) details and other examples.
*
* NOTE: The library file MFRC522.h has a lot of useful info. Please read it.
*
* ----------------------------------------------------------------------------
*
* BEWARE: Data will be written to the PICC, in sector #1 (blocks #4 to #7). // number sector can be
changed from 1 to 15
* // WARNING: Sector #0 is
reserved for READ ONLY purposes
*
* Typical pin layout used:
* ---------------------------------------------------------------------------------------------------------
----------
* MFRC522 Arduino Arduino Arduino Arduino Arduino Arduino
* Reader/PCD Uno/101 Mega Nano v3 Leonardo/Micro Pro Micro Due
* Signal Pin Pin Pin Pin Pin Pin Pin
* ---------------------------------------------------------------------------------------------------------
----------
* RST/Reset RST 9 5 D9 RESET/ICSP-5 RST 12
* SPI SS SDA(SS) 10 53 D10 10 10 13
* SPI MOSI MOSI 11 / ICSP-4 51 D11 ICSP-4 16 MOSI (SPI)
* SPI MISO MISO 12 / ICSP-1 50 D12 ICSP-1 14 MISO (SPI)
* SPI SCK SCK 13 / ICSP-3 52 D13 ICSP-3 15 SCK (SPI)
*
* All modifications to example program (included in 'rfid-master' library)
* "C:\Program Files (x86)\Arduino\libraries\rfid-master\examples\ReadAndWrite\ReadAndWrite.ino"
* were done by Eduardo Alanis Vzquez (juecito_@hotmail.com)
* November 20, 2017
*
*/
#include <SPI.h> // Download from Github: https://github.com/PaulStoffregen/SPI/blob/master/SPI.h
#include <MFRC522.h> // Download from Github: https://github.com/miguelbalboa/rfid

constexpr uint8_t RST_PIN = 48; // Configurable, see typical pin layout above
constexpr uint8_t SS_PIN = 53; // Configurable, see typical pin layout above
MFRC522 mfrc522(SS_PIN, RST_PIN); // Create MFRC522 instance.

MFRC522::MIFARE_Key key;

// Para lectura y escritura se definen parmetros de direccin de datos

byte sector = 1;
byte blockAddr = 5;
byte writeBlock[16] = {0x00}; // aqu se lleva el control de reset de puntos

// Para la comunicacin con usuario se establecen perifricos de sealamiento e interaccin

byte N[10][7] = {
{1,1,1,1,1,1,0}, // Display 7 segmentos {a,b,c,d,e,f,g}
{0,1,1,0,0,0,0},
{1,1,0,1,1,0,1},
{1,1,1,1,0,0,1},
{0,1,1,0,0,1,1},
{1,0,1,1,0,1,1},
{0,0,1,1,1,1,1},
{1,1,1,0,0,0,0},
{1,1,1,1,1,1,1},
{1,1,1,0,0,1,1},
};
byte E[7]={0,0,0,0,0,0,1}; // guin central del display de 7 segmentos. nicamente para sealizacin. Puede
cambiarse

int valido=0;
int tiempo1=2;
int boton=0;
int puntos=0;
int b;
int i;
int j;
int d=0;
int k=0; //
int p=0; //
int q=0; //
int r=0; //
int s=0; //

void setup()
{
Serial.begin(9600);
SPI.begin(); // Init SPI bus
mfrc522.PCD_Init(); // Init MFRC522 card
delay(500);

// Prepare the key (used both as key A and as key B)


// using FFFFFFFFFFFFh which is the default at chip delivery from the factory
for (byte i = 0; i < 6; i++)
{
key.keyByte[i] = 0xFF;
}
pinMode(8,OUTPUT); // D1
pinMode(9,OUTPUT); // D2
pinMode(10,OUTPUT); // D3
pinMode(11,OUTPUT); // D4
pinMode(22,OUTPUT); // A
pinMode(24,OUTPUT); // B
pinMode(26,OUTPUT); // C
pinMode(28,OUTPUT); // D
pinMode(30,OUTPUT); // E
pinMode(32,OUTPUT); // F
pinMode(34,OUTPUT); // G
}

void loop()
{
boton=digitalRead(40);

while(boton==1)
{
for(i=8; i<=11; i++)
{
digitalWrite(i,HIGH);
}
for(i=0; i<=6; i++)
{
j=(2*i)+22;
digitalWrite(j,E[i]);
}
delay(4000);
for(i=8; i<=11; i++)
{
digitalWrite(i,LOW);
}

// Look for new cards


if ( ! mfrc522.PICC_IsNewCardPresent())
return;

// Select one of the cards


if ( ! mfrc522.PICC_ReadCardSerial())
return;

byte trailerBlock = 7;
MFRC522::StatusCode status;

status = (MFRC522::StatusCode) mfrc522.PCD_Authenticate(MFRC522::PICC_CMD_MF_AUTH_KEY_A, trailerBlock,


&key, &(mfrc522.uid));

if (status != MFRC522::STATUS_OK)
return;
status = (MFRC522::StatusCode) mfrc522.MIFARE_Write(blockAddr, writeBlock, 16); // Escritura de
datos
if (status != MFRC522::STATUS_OK)
{
// Halt PICC
mfrc522.PICC_HaltA();
// Stop encryption on PCD
mfrc522.PCD_StopCrypto1();
return;
}

for (b=8; b<=11; b++)


{
digitalWrite(b,HIGH);
}
for(i=0; i<=6; i++)
{
j=(2*i)+22;
digitalWrite(j,E[i]);
}
delay(1000);
for (b=8; b<=11; b++)
{
digitalWrite(b,LOW);
}
digitalWrite(8,HIGH);
for(i=0; i<=6; i++)
{
j=(2*i)+22;
digitalWrite(j,N[0][i]);
}
delay(1000);
digitalWrite(8,LOW);

// Halt PICC
mfrc522.PICC_HaltA();
// Stop encryption on PCD
mfrc522.PCD_StopCrypto1();
boton=0;
}
// Look for new cards
if ( ! mfrc522.PICC_IsNewCardPresent())
return;

// Select one of the cards


if ( ! mfrc522.PICC_ReadCardSerial())
return;

byte trailerBlock = 7;
MFRC522::StatusCode status;
byte buffer[18];
byte size = sizeof(buffer);

status = (MFRC522::StatusCode) mfrc522.PCD_Authenticate(MFRC522::PICC_CMD_MF_AUTH_KEY_A, trailerBlock,


&key, &(mfrc522.uid));

if (status != MFRC522::STATUS_OK)
return;

status = (MFRC522::StatusCode) mfrc522.MIFARE_Read(blockAddr, buffer, &size);

Serial.println("Buffer Read: ");


for (k=0; k<=15; k++) // recorre los 16 bytes del bloque para establecer el byte de escritura de
puntos
{
Serial.print(buffer[k] < 0x10 ? " 0" : " ");
Serial.print(buffer[k],HEX);
Serial.print(" ");
puntos+=buffer[k]; // se lleva un contador de puntos acumulados en hexadecimal
}
Serial.println();
Serial.println(puntos,DEC);

while(puntos>=1000)
{
p++;
puntos-=1000;
Serial.println("Miles: ");
Serial.println(p,DEC);
}
while(puntos>=100)
{
q++;
puntos-=100;
Serial.println("Centenas: ");
Serial.println(q,DEC);
}
while(puntos>=10)
{
r++;
puntos-=10;
Serial.println("Decenas: ");
Serial.println(r,DEC);
}
s=puntos;
Serial.println("Unidades: ");
Serial.println(s,DEC);

puntos=0;
Serial.println();
Serial.println(puntos,DEC);

for (d=0; d<=500; d++)


{
digitalWrite(8,HIGH);
for(i=0; i<=6; i++)
{
j=(2*i)+22;
digitalWrite(j,N[s][i]);
}
delay(tiempo1);
digitalWrite(8,LOW);
digitalWrite(9,HIGH);
for(i=0; i<=6; i++)
{
j=(2*i)+22;
digitalWrite(j,N[r][i]);
}
delay(tiempo1);
digitalWrite(9,LOW);
digitalWrite(10,HIGH);
for(i=0; i<=6; i++)
{
j=(2*i)+22;
digitalWrite(j,N[q][i]);
}
delay(tiempo1);
digitalWrite(10,LOW);
digitalWrite(11,HIGH);
for(i=0; i<=6; i++)
{
j=(2*i)+22;
digitalWrite(j,N[p][i]);
}
delay(tiempo1);
digitalWrite(11,LOW);
}

p=0;
q=0;
r=0;
s=0;

// Halt PICC
mfrc522.PICC_HaltA();
// Stop encryption on PCD
mfrc522.PCD_StopCrypto1();
}

S-ar putea să vă placă și