Advertisement
safwan092

code-after-corrections

Dec 20th, 2017
218
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 7.35 KB | None | 0 0
  1. /*
  2.    --------------------------------------------------------------------------------------------------------------------
  3.    Example sketch/program showing how to read new NUID from a PICC to serial.
  4.    --------------------------------------------------------------------------------------------------------------------
  5.    This is a MFRC522 library example; for further details and other examples see: https://github.com/miguelbalboa/rfid
  6.  
  7.    Example sketch/program showing how to the read data from a PICC (that is: a RFID Tag or Card) using a MFRC522 based RFID
  8.    Reader on the Arduino SPI interface.
  9.  
  10.    When the Arduino and the MFRC522 module are connected (see the pin layout below), load this sketch into Arduino IDE
  11.    then verify/compile and upload it. To see the output: use Tools, Serial Monitor of the IDE (hit Ctrl+Shft+M). When
  12.    you present a PICC (that is: a RFID Tag or Card) at reading distance of the MFRC522 Reader/PCD, the serial output
  13.    will show the type, and the NUID if a new card has been detected. Note: you may see "Timeout in communication" messages
  14.    when removing the PICC from reading distance too early.
  15.  
  16.    @license Released into the public domain.
  17.  
  18.   The circuit for LCD SCREEN:
  19.   pin3 to grd with 220 ohm resister
  20.    LCD RS pin to digital pin A0
  21.    LCD Enable pin to digital pin A1
  22.    LCD D4 pin to digital pin 5
  23.    LCD D5 pin to digital pin 4
  24.    LCD D6 pin to digital pin 3
  25.    LCD D7 pin to digital pin 2
  26.    LCD R/W pin to ground
  27.    LCD VSS pin to ground
  28.    LCD VCC pin to 5V
  29.    10K resistor:
  30.    ends to +5V and ground
  31.    wiper to LCD VO pin (pin 3)
  32.  
  33.    RFID Circuit :
  34.    Typical pin layout used:
  35.    -----------------------------------------------------------------------------------------
  36.                MFRC522      Arduino       Arduino   Arduino    Arduino          Arduino
  37.                Reader/PCD   Uno/101       Mega      Nano v3    Leonardo/Micro   Pro Micro
  38.    Signal      Pin          Pin           Pin       Pin        Pin              Pin
  39.    -----------------------------------------------------------------------------------------
  40.    RST/Reset   RST          9             5         D9         RESET/ICSP-5     RST
  41.    SPI SS      SDA(SS)      10            53        D10        10               10
  42.    SPI MOSI    MOSI         11 / ICSP-4   51        D11        ICSP-4           16
  43.    SPI MISO    MISO         12 / ICSP-1   50        D12        ICSP-1           14
  44.    SPI SCK     SCK          13 / ICSP-3   52        D13        ICSP-3           15
  45. */
  46.  
  47. #include <SPI.h>
  48. #include <MFRC522.h>
  49. // include the library code:
  50. #include <LiquidCrystal.h>
  51.  
  52. // initialize the library by associating any needed LCD interface pin
  53. // with the arduino pin number it is connected to
  54. const int rs = A0, en = A1, d4 = 5, d5 = 4, d6 = 3, d7 = 2;
  55. LiquidCrystal lcd(rs, en, d4, d5, d6, d7);
  56. #define SS_PIN 10
  57. #define RST_PIN 9
  58. int writeVal;
  59. int greenLedPin = A2;
  60. int redLedPin = A3;
  61. int orangeLedPin = 6;
  62. int buzzerPin = A4;
  63.  
  64. boolean chkConn = false;
  65. MFRC522 rfid(SS_PIN, RST_PIN); // Instance of the class
  66.  
  67. MFRC522::MIFARE_Key key;
  68.  
  69. // Init array that will store new NUID
  70. byte nuidPICC[4];
  71.   boolean isValidating = false;
  72. void setup() {
  73.   Serial.begin(9600);
  74.  
  75.   SPI.begin(); // Init SPI bus
  76.   rfid.PCD_Init(); // Init MFRC522
  77.   pinMode(greenLedPin, OUTPUT);
  78.   pinMode(redLedPin, OUTPUT);
  79.   digitalWrite(buzzerPin, OUTPUT);
  80.   for (byte i = 0; i < 6; i++) {
  81.     key.keyByte[i] = 0xFF;
  82.   }
  83.   lcd.begin(16, 2);
  84.   lcd.setCursor(0, 0);
  85.  
  86. //  lcd.print("Welcome To GadgetProgrammer");
  87. //  for (int i = 0; i < 26; i++) {
  88. //    lcd.scrollDisplayRight();
  89. //    delay(100);
  90. //  }
  91.   lcd.begin(16, 2);
  92.   lcd.setCursor(0, 0);
  93.   lcd.print("GadgetProgrammer");
  94.   delay(100);
  95.  
  96.   lcd.setCursor(0, 1);
  97.   lcd.print(" Not Connected! ");
  98.   //establishConnection();
  99. }
  100.  
  101. void loop() {
  102.  
  103.   lcd.setCursor(0, 1);
  104.  
  105.   char retOutput;
  106.   if (Serial.available() > 0) {
  107.     retOutput = Serial.read();
  108.    
  109.     lcd.blink();
  110.     if (retOutput == 'A') {
  111.       chkConn = true;
  112.       analogWrite(orangeLedPin,0);
  113.       lcd.print("Swipe Your Card!");
  114.     }
  115.     if(retOutput == 'V'){
  116.       analogWrite(orangeLedPin,1000);
  117.       lcd.print("Validating Group");
  118.       isValidating = true;
  119.       delay(1000);
  120.     }
  121.    
  122.     if(retOutput == 'S'){
  123.       lcd.print("Validation Done!");
  124.       glowGreenLed(1000);
  125.       isValidating = false;
  126.       delay(1000);
  127.       }
  128.  
  129.   }
  130.   if (chkConn && !retOutput != "") {
  131.     if (retOutput == '1') {
  132.       lcd.print("Attendance Done!");
  133.       glowGreenLed(1000);
  134.       delay(4000);
  135.     }
  136.     else if (retOutput == '0') {
  137.       lcd.print("Network Error  !");
  138.       glowRedLed(1000);
  139.       delay(4000);
  140.     } else if (retOutput == '2') {
  141.       lcd.print("User Not Found!");
  142.       glowRedLed(1000);
  143.       delay(4000);
  144.     } else if (retOutput == '5') {
  145.       lcd.print("Network Error!");
  146.       glowRedLed(1000);
  147.       delay(4000);
  148.     }
  149.     //if not in validation process then swipe your card
  150.     if(isValidating == false){
  151.       lcd.setCursor(0, 1);
  152.       analogWrite(orangeLedPin,0);
  153.       lcd.print("Swipe Your Card!");
  154.     }
  155.     //lcd.clear();
  156.     // Look for new cards
  157.   }
  158.   if ( ! rfid.PICC_IsNewCardPresent())
  159.     return;
  160.  
  161.   // Verify if the NUID has been readed
  162.   if ( ! rfid.PICC_ReadCardSerial())
  163.     return;
  164.  
  165.   //Serial.print(F("PICC type: "));
  166.   MFRC522::PICC_Type piccType = rfid.PICC_GetType(rfid.uid.sak);
  167.   //Serial.println(rfid.PICC_GetTypeName(piccType));//Printing type of card
  168.  
  169.   // Check is the PICC of Classic MIFARE type
  170.   if (piccType != MFRC522::PICC_TYPE_MIFARE_MINI &&
  171.       piccType != MFRC522::PICC_TYPE_MIFARE_1K &&
  172.       piccType != MFRC522::PICC_TYPE_MIFARE_4K) {
  173.     //Serial.println(F("Your tag is not of type MIFARE Classic."));
  174.     return;
  175.   }
  176.   String s;
  177.   for (byte i = 0; i < 4; i++) {
  178.  
  179.     nuidPICC[i] = rfid.uid.uidByte[i];
  180.  
  181.   }
  182.  
  183.   //Serial.println(F("The NUID tag is:"));
  184.   //Serial.print(F("In hex: "));
  185.   printHex(rfid.uid.uidByte, rfid.uid.size);
  186.   Serial.println();
  187.   lcd.setCursor(0,1);
  188.   lcd.print("Reading Card...!");
  189.   // Halt PICC
  190.   rfid.PICC_HaltA();
  191.  
  192.   // Stop encryption on PCD
  193.   rfid.PCD_StopCrypto1();
  194.   delay(1000);
  195. }
  196.  
  197.  
  198. /**
  199.    Helper routine to dump a byte array as hex values to Serial.
  200. */
  201. void printHex(byte *buffer, byte bufferSize) {
  202.   for (byte i = 0; i < bufferSize; i++) {
  203.     Serial.print(buffer[i] < 0x10 ? "0" : "");
  204.     Serial.print(buffer[i], HEX);
  205.   }
  206. }
  207.  
  208. /**
  209.    Helper routine to dump a byte array as dec values to Serial.
  210. */
  211. void printDec(byte *buffer, byte bufferSize) {
  212.   for (byte i = 0; i < bufferSize; i++) {
  213.     Serial.print(buffer[i] < 0x10 ? "0" : "");
  214.     Serial.print(buffer[i], DEC);
  215.   }
  216.  
  217.  
  218.  
  219. }
  220. void glowGreenLed(int delaytime) {
  221.   digitalWrite(orangeLedPin,LOW);
  222.   digitalWrite(greenLedPin, HIGH);
  223.   digitalWrite(redLedPin, LOW);
  224.   tone(buzzerPin, 5000, 300);
  225.   if (delaytime > 0) {
  226.     delay(delaytime);
  227.     digitalWrite(greenLedPin, LOW);
  228.     noTone(buzzerPin);
  229.   }
  230. }
  231. void glowRedLed(int delaytime) {
  232.   digitalWrite(orangeLedPin,LOW);
  233.   digitalWrite(greenLedPin, LOW);
  234.   digitalWrite(redLedPin, HIGH);
  235.   digitalWrite(buzzerPin, HIGH);
  236.   delay(1000);
  237.   tone(buzzerPin, 5000);
  238.   //noTone(buzzerPin);
  239.   if (delaytime > 0) {
  240.     delay(delaytime);
  241.     digitalWrite(redLedPin, LOW);
  242.     noTone(buzzerPin);
  243.   }
  244. }
  245. void glowOrangeLed(){
  246.   analogWrite(orangeLedPin,100);
  247.   delay(100);}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement