Advertisement
Guest User

Untitled

a guest
Feb 25th, 2020
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <Wire.h>
  2. #include <SPI.h>
  3. #include <Adafruit_PN532.h>
  4.  
  5. #define PN532_IRQ   (3)
  6. #define PN532_RESET (23)
  7.  
  8. Adafruit_PN532 nfc(PN532_IRQ,PN532_RESET);
  9. //Adafruit_PN532 nfc(PN532_SCK, PN532_MISO, PN532_MOSI, PN532_SS);
  10.  
  11. #if defined(ARDUINO_ARCH_SAMD)
  12. // for Zero, output on USB Serial console, remove line below if using programming port to program the Zero!
  13. // also change #define in Adafruit_PN532.cpp library file
  14.    #define Serial SerialUSB
  15. #endif
  16.  
  17. // constants won't change. They're used here to set pin numbers:
  18. const int buttonPin_MTP = 4;     // the number of the pushbutton pin
  19. const int buttonPin_Atk = 6;     // the number of the pushbutton pin
  20. const int buttonPin_Def = 5;     // the number of the pushbutton pin
  21.  
  22. int Prev_Status_MTP = 1;
  23. int Prev_Status_Atk = 1;
  24. int Prev_Status_Def = 1;
  25. int Aux_Status_MTP = 1;
  26. int Aux_Status_Atk = 1;
  27. int Aux_Status_Def = 1;
  28. int buttonState_MTP ;
  29. int buttonState_Atk;
  30. int buttonState_Def;
  31.  
  32. int NextState = 0;
  33. int State = 0;
  34.  
  35.  
  36.  
  37.  
  38. void Process_Read_Card(String CardID){
  39. //  Serial.println(CardID);
  40.   delay(2000);
  41.   // read the state of the pushbutton value:
  42.   buttonState_MTP = Check_Position(buttonPin_MTP);
  43.   buttonState_Atk = Check_Position(buttonPin_Atk);
  44.   buttonState_Def = Check_Position(buttonPin_Def);
  45.  
  46. //  Serial.println(buttonState_MTP);
  47. //  Serial.println(buttonState_Atk);
  48. //  Serial.println(buttonState_Atk);
  49.  
  50.   if( buttonState_MTP!=Prev_Status_MTP || buttonState_Atk!=Prev_Status_Atk || buttonState_Def!=Prev_Status_Def){
  51. //    Serial.println("New Change in field");
  52.     delay(100);
  53.     Aux_Status_MTP = buttonState_MTP;
  54.     Aux_Status_Atk = buttonState_Atk;
  55.     Aux_Status_Def = buttonState_Def;
  56.     //Estos son nuestros estados nuevos
  57.     buttonState_MTP = Check_Position(buttonPin_MTP);
  58.     buttonState_Atk = Check_Position(buttonPin_Atk);
  59.     buttonState_Def = Check_Position(buttonPin_Def);
  60.  
  61. //    Serial.println(buttonState_MTP);
  62. //    Serial.println(buttonState_Atk);
  63. //    Serial.println(buttonState_Atk);
  64.  
  65.     if(Aux_Status_MTP == buttonState_MTP && Aux_Status_Atk == buttonState_Atk && Aux_Status_Def == buttonState_Def){
  66. //      Serial.println("Seleccionando estado");
  67.       if( buttonState_Atk==1 && buttonState_Def==1 && buttonState_MTP==1){
  68.         NextState = 0;
  69. //        Serial.println("Campo Libre");
  70.         }
  71.       if( buttonState_Atk==0 && buttonState_Def==0 && buttonState_MTP==0){
  72.         NextState = 5;
  73. //        Serial.println("Ataque y MTP");
  74.         }
  75.       if( buttonState_Atk==1 && buttonState_Def==0 && buttonState_MTP==0){
  76.         NextState = 4;
  77. //        Serial.println("Defensa y MTP");
  78.         }
  79.       if( buttonState_Atk==1 && buttonState_Def==0 && buttonState_MTP==1){
  80.         NextState = 2;
  81. //        Serial.println("Defensa");
  82.         }
  83.       if( buttonState_Atk==0 && buttonState_Def==0 && buttonState_MTP==1){
  84.         NextState = 1;
  85. //        Serial.println("Ataque");
  86.         }
  87.       if( buttonState_Atk==1 && buttonState_Def==1 && buttonState_MTP==0){
  88.         NextState = 3;
  89. //        Serial.println("MTP");
  90.         }
  91.      State = MEF(State,NextState,CardID);
  92.      
  93.       Prev_Status_MTP = buttonState_MTP;
  94.       Prev_Status_Atk = buttonState_Atk;
  95.       Prev_Status_Def = buttonState_Def;
  96.      
  97.       }
  98.  
  99.   }
  100. }
  101.  
  102. int Check_Position(int buttonPin){
  103.   int sensorState = digitalRead(buttonPin);
  104.  
  105.   delay(10);
  106.   sensorState = sensorState + digitalRead(buttonPin);
  107.   delay(10);
  108.   sensorState = sensorState + digitalRead(buttonPin);
  109.   delay(10);
  110.   sensorState = sensorState + digitalRead(buttonPin);
  111.   delay(10);
  112.   sensorState = sensorState + digitalRead(buttonPin);
  113.   delay(10);
  114.   sensorState = sensorState + digitalRead(buttonPin);
  115.   delay(10);
  116.   sensorState = sensorState + digitalRead(buttonPin);
  117.   delay(10);
  118.   sensorState = sensorState + digitalRead(buttonPin);
  119.   delay(10);
  120.   sensorState = sensorState + digitalRead(buttonPin);
  121.   delay(10);
  122.   sensorState = sensorState + digitalRead(buttonPin);
  123.  
  124.   if(sensorState>8){
  125.     return 1;
  126.     }
  127.   else{
  128.     return 0;
  129.     }
  130.   }
  131.  
  132.  
  133. int MEF(int State,int NextState, String CardID){
  134.  
  135.   switch(State){
  136.     case 0:
  137.       if(NextState == 1){
  138.         State = NextState;
  139.         Serial.println("001"+CardID);
  140. //        Serial.println(CardID);
  141.         }
  142.       if(NextState == 2){
  143.         State = NextState;
  144.         Serial.print("010");
  145.         Serial.println(CardID);
  146.         }
  147.       if(NextState == 3){
  148.         State = NextState;
  149.         Serial.print("011");
  150.         Serial.println(CardID);
  151.         }
  152.       break;
  153.     case 1:
  154.       if(NextState == 2){
  155.         State = NextState;
  156.         Serial.print("010");
  157.         Serial.println(CardID);
  158.         }
  159.       if(NextState == 5){
  160.         State = NextState;
  161.         Serial.print("011");
  162.         Serial.println(CardID);
  163.         }
  164.       if(NextState == 0){
  165.         State = NextState;
  166.         Serial.print("101");
  167.         Serial.println(CardID);
  168.         }
  169.       break;
  170.     case 2:
  171.       if(NextState == 1){
  172.         State = NextState;
  173.         Serial.print("001");
  174.         Serial.println(CardID);
  175.         }
  176.       if(NextState == 4){
  177.         State = NextState;
  178.         Serial.print("011");
  179.         Serial.println(CardID);
  180.         }
  181.       if(NextState == 0){
  182.         State = NextState;
  183.         Serial.print("110");
  184.         Serial.println(CardID);
  185.         }
  186.       break;
  187.     case 3:
  188.       if(NextState == 4){
  189.         State = NextState;
  190.         Serial.print("010");
  191.         Serial.println(CardID);
  192.         }
  193.       if(NextState == 5){
  194.         State = NextState;
  195.         Serial.print("001");
  196.         Serial.println(CardID);
  197.         }
  198.       if(NextState == 0){
  199.         State = NextState;
  200.         Serial.print("111");
  201.         Serial.println(CardID);
  202.         }
  203.       break;
  204.     case 4:
  205.       if(NextState == 2){
  206.         State = NextState;
  207.         Serial.print("111");
  208.         Serial.println(CardID);
  209.         }
  210.       if(NextState == 5){
  211.         State = NextState;
  212.         Serial.print("001");
  213.         Serial.println(CardID);
  214.         }
  215.       if(NextState == 3){
  216.         State = NextState;
  217.         Serial.print("110");
  218.         Serial.println(CardID);
  219.         }
  220.       break;
  221.     case 5:
  222.       if(NextState == 1){
  223.         State = NextState;
  224.         Serial.print("111");
  225.         Serial.println(CardID);
  226.         }
  227.       if(NextState == 4){
  228.         State = NextState;
  229.         Serial.print("010");
  230.         Serial.println(CardID);
  231.         }
  232.       if(NextState == 3){
  233.         State = NextState;
  234.         Serial.print("101");
  235.         Serial.println(CardID);
  236.         }
  237.       break;
  238.    
  239.     }
  240.     return State;
  241.  
  242.  
  243.   }
  244.  
  245. void setup() {
  246.   #ifndef ESP8266
  247.     while (!Serial); // for Leonardo/Micro/Zero
  248.   #endif
  249.   Serial.begin(9600);
  250.   nfc.begin();
  251.   uint32_t versiondata = nfc.getFirmwareVersion();
  252.   if (! versiondata) {
  253.     Serial.print("Didn't find PN53x board");
  254.     while (1); // halt
  255.   }  
  256.   nfc.SAMConfig();
  257.   pinMode(buttonPin_MTP, INPUT);
  258.   pinMode(buttonPin_Atk, INPUT);
  259.   pinMode(buttonPin_Def, INPUT);
  260.   pinMode(7, OUTPUT);
  261.  
  262. }
  263.  
  264. void loop() {
  265.   uint8_t success;
  266.   uint8_t uid[] = { 0, 0, 0, 0, 0, 0, 0 };
  267.   uint8_t uidLength;
  268.   success = nfc.readPassiveTargetID(PN532_MIFARE_ISO14443A, uid, &uidLength);
  269.   if (success) {  
  270.     if (uidLength == 4){
  271.       uint8_t keya[6] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
  272.       success = nfc.mifareclassic_AuthenticateBlock(uid, uidLength, 4, 0, keya);
  273.       if (success){  
  274.         uint8_t data[16];          
  275.         // Para escribir una carta en el Bloque 4
  276.         //memcpy(data, (const uint8_t[]){ '3', '9', '2', '7', '1', '5', '5', '3', 0, 0, 0, 0, 0, 0, 0, 0 }, sizeof data);
  277.         //success = nfc.mifareclassic_WriteDataBlock (4, data);
  278.         success = nfc.mifareclassic_ReadDataBlock(4, data);
  279.         if (success){
  280.           String ReadBlock;
  281.           String ReadCard = "11111111";
  282.           for (char c : data) ReadBlock += c;
  283.           ReadCard[0] = ReadBlock[0];
  284.           ReadCard[1] = ReadBlock[1];
  285.           ReadCard[2] = ReadBlock[2];
  286.           ReadCard[3] = ReadBlock[3];
  287.           ReadCard[4] = ReadBlock[4];
  288.           ReadCard[5] = ReadBlock[5];
  289.           ReadCard[6] = ReadBlock[6];
  290.           ReadCard[7] = ReadBlock[7];
  291. //          Serial.println(ReadCard);
  292.           digitalWrite(7, HIGH);   // turn the LED on (HIGH is the voltage level)
  293.           delay(200);                       // wait for a second
  294.           digitalWrite(7, LOW);
  295.           Process_Read_Card(ReadCard);
  296.           delay(100);
  297.         }
  298.         else{
  299.           Serial.println("Ooops ... unable to read the requested block.  Try another key?");
  300.         }
  301.       }
  302.       else{
  303.         Serial.println("Ooops ... authentication failed: Try another key?");
  304.       }
  305.     }
  306.   }
  307. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement