陸、全體程式列表


  1. #include <LiquidCrystal.h>
  2. #include <EEPROM.h>
  3. #include <SPI.h>
  4. #include <MFRC522.h>
  5. /* * Signal     Pin              Pin               Pin
  6.  *            Arduino Uno      Arduino Mega      MFRC522 board
  7.  * ------------------------------------------------------------
  8.  * Reset      9                5                 RST
  9.  * SPI SS     10               53                SDA
  10.  * SPI MOSI   11               52                MOSI
  11.  * SPI MISO   12               51                MISO
  12.  * SPI SCK    13               50                SCK
  13.  *
  14.  * Hardware required:
  15.  * Arduino
  16.  * PCD (Proximity Coupling Device): NXP MFRC522 Contactless Reader IC
  17.  * PICC (Proximity Integrated Circuit Card): A card or tag using the ISO 14443A interface, eg Mifare or NTAG203.
  18.  * The reader can be found on eBay for around 5 dollars. Search for "mf-rc522" on ebay.com. 
  19.  */

  20. #define SS_PIN 10    //Arduino Uno
  21. #define RST_PIN 9
  22. MFRC522 mfrc522(SS_PIN, RST_PIN);        // Create MFRC522 instance.
  23. const  byte  Buzzer=15;
  24. const  byte  InitialPin=17;
  25. const  byte  LockOut=16;
  26. const  byte  LCD_LT=8;
  27. byte  In_Data,Receive_Data[4],EEPROM_data[8],RFID_SN[4],RFID_PW[4];
  28. //
  29. int  KeyIn,CardCount,CardNumber;
  30. byte  KeyNum,PW_Error;
  31. unsigned long Wait_Time;
  32. const  int  keyValue[]={1000,800,670,500,440,390,325,305,280,245,230,215,190,160,135,110};
  33. const  byte  keyNumbers[]={1,14,7,2,0,8,3,15,9,10,13,12,4,5,6,11};
  34. const  char keyChar[]={'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};
  35. byte  keyNumber;
  36. //     LCD Pins : RS EN D4 D5 D6 D7
  37. LiquidCrystal  lcd(3,2,7,6,5,4);
  38. const  int  numRows=2;
  39. const  int  numCols=20;
  40. String inputString = "";         // a string to hold incoming data
  41. boolean stringComplete = false,equal,flag;  // whether the string is complete
  42. //
  43. void  setup()
  44. {
  45.     pinMode(LockOut,OUTPUT);
  46.     pinMode(LCD_LT,OUTPUT);
  47.     pinMode(Buzzer,OUTPUT);
  48.     digitalWrite(Buzzer,1);
  49.     digitalWrite(LCD_LT,0);
  50.     lcd.begin(numCols,numRows);
  51.     LCD_Clear_Line(0);
  52.     lcd.setCursor(4,0);
  53.     lcd.print("Power Test..");
  54.     BuzzerSound(100);
  55. //    lcd.setCursor(0,1);
  56.     lcd.blink();
  57.      LCD_Clear_Line(1);
  58.   for (int i=0;i<10;i++)
  59.   {
  60.      lcd.setCursor(i,1);
  61.      lcd.print(i);
  62.      delay(100);
  63.   }
  64.   for (int i=0;i<10;i++)
  65.   {
  66.      lcd.setCursor(i+10,1);
  67.      lcd.print(i);
  68.      delay(100);
  69.   }
  70.   for (int i=0;i<10;i++)
  71.   {
  72.      lcd.setCursor(i,0);
  73.      lcd.print(i);
  74.      delay(100);
  75.   }
  76.   for (int i=0;i<10;i++)
  77.   {
  78.      lcd.setCursor(i+10,0);
  79.      lcd.print(i);
  80.      delay(100);
  81.   }
  82.         SPI.begin();                // Init SPI bus
  83.         mfrc522.PCD_Init();        // Init MFRC522 card
  84.     Serial.begin(9600);
  85.     Serial.println("Start...");
  86. //  inputString.reserve(50);
  87.   delay(1000);
  88.     BuzzerSound(500);
  89.       LCD_Clear_Line(0);
  90.       lcd.setCursor(1,0);
  91.       lcd.print("Module TP: RC522");
  92.     delay(1000);
  93.     if(digitalRead(InitialPin)==0)
  94.       Initial_System();
  95. }
  96. //
  97. void  loop()
  98. {
  99.   Start:
  100.     BeeBee();
  101.       LCD_Clear_Line(0);
  102.       lcd.setCursor(1,0);
  103.       lcd.print("RFID Security Sys.");
  104.       LCD_Clear_Line(1);
  105.       lcd.setCursor(4,1);
  106.       lcd.print("MUST-2014A");
  107.     delay(1000);
  108.     digitalWrite(LCD_LT,1);
  109. //      
  110.         // Look for new cards
  111.       while( ! mfrc522.PICC_IsNewCardPresent())
  112.       {};
  113.     digitalWrite(LCD_LT,0);
  114.       if(Read_RFID_SN()==true)
  115.       {
  116.       BuzzerSound(100);
  117.           if(!Check_Card_Exist())
  118.           {
  119.             lcd.setCursor(17,1);
  120.             lcd.print(".");
  121.             delay(500);
  122.             lcd.print(".");
  123.             delay(1000);
  124.             lcd.setCursor(17,1);
  125.             lcd.print("  ");
  126.           } 
  127.           else
  128.           {
  129.             LCD_Clear_Line(0);
  130.             lcd.setCursor(2,0);
  131.             lcd.print("Card Number:");
  132.             if(CardNumber<10)
  133.             lcd.print("0");
  134.             lcd.print(CardNumber);
  135.             PW_Error=3;
  136.             do
  137.             {
  138.                if(Password_In()==false) break;
  139. //               {
  140.                  if(KeyNum==12) break;
  141.           //          goto Start;
  142.                  delay(100);
  143.                  equal=true;
  144.                  for(byte i=0;i<4;i++)
  145.                  {
  146.                     Serial.print(RFID_PW[i],HEX);
  147.                     Serial.print(",");
  148.                     Serial.println(EEPROM_data[i+4],HEX);
  149.                     if(RFID_PW[i]!=EEPROM_data[i+4])
  150.                       equal=false;
  151.                  }
  152.                  if(equal)
  153.                  {
  154.                     if(CardNumber!=1)
  155.                     {
  156.                       LCD_Clear_Line(1);
  157.                       lcd.setCursor(3,1);
  158.                       lcd.print("Gate Open!");
  159.                       digitalWrite(LockOut,1);
  160.                       BeeBee();
  161.                       delay(1000);
  162.                       digitalWrite(LockOut,0);
  163.                       break;
  164.                     } 
  165.                     else
  166.                     {
  167.                       do
  168.                       {
  169.      Master_Card:
  170.                         LCD_Clear_Line(0);
  171.                         lcd.setCursor(1,0);
  172.                         lcd.print("1.Open Gate");
  173.                         LCD_Clear_Line(1);
  174.                         lcd.setCursor(1,1);
  175.                         lcd.print("2.Card Management");
  176.                         if((WaitKeyIn(10)==false)||(KeyNum==12))
  177.                           goto Start;
  178.                         if(KeyNum==1)
  179.                         {
  180.                           LCD_Clear_Line(1);
  181.                           lcd.setCursor(3,1);
  182.                           lcd.print("Gate Open!");
  183.                           digitalWrite(LockOut,1);
  184.                           BeeBee();
  185.                           delay(1000);
  186.                           digitalWrite(LockOut,0);
  187.                           goto Start;
  188.                         }
  189.                         if(KeyNum==2)                      
  190.                         {
  191.       Card_Management:                        
  192.                           LCD_Clear_Line(0);
  193.                           lcd.setCursor(0,0);
  194.                           lcd.print("1.Add  2.Del. Card");
  195.                           LCD_Clear_Line(1);
  196.                           lcd.setCursor(0,1);
  197.                           lcd.print("3.Chg. PW 4.Init All");
  198.                           BeeBee();
  199.                           if((WaitKeyIn(10)==false)||(KeyNum==12))
  200.                             goto Master_Card;
  201.                           Serial.println(KeyNum);
  202.                           if(KeyNum==1)
  203.                           {
  204.                               if(AddCard()==false)
  205.                               {
  206.                                   LCD_Clear_Line(0);
  207.                                   lcd.setCursor(1,0);
  208.                                   lcd.print("1.Add Card ");
  209.                                   LCD_Clear_Line(1);
  210.                                   lcd.setCursor(3,1);
  211.                                   lcd.print("function end...");
  212.                                   BeeBee();
  213.                                   WaitKeyIn(2);
  214.                                   goto Card_Management;
  215.                               }
  216.                            }
  217.                            if(KeyNum==2)
  218.                            {
  219.                               if(DelCard()==false)
  220.                               {
  221.                                   LCD_Clear_Line(0);
  222.                                   lcd.setCursor(1,0);
  223.                                   lcd.print("2.Delete Card ");
  224.                                   LCD_Clear_Line(1);
  225.                                   lcd.setCursor(2,1);
  226.                                   lcd.print("function end...");
  227.                                   BeeBee();
  228.                                   WaitKeyIn(2);
  229.                                   goto Card_Management;
  230.                               }
  231.                            }
  232.                            if(KeyNum==3)
  233.                            {
  234.                               if(ChangePW()==false)
  235.                               {
  236.                                   LCD_Clear_Line(0);
  237.                                   lcd.setCursor(1,0);
  238.                                   lcd.print("3.Change PW.");
  239.                                   LCD_Clear_Line(1);
  240.                                   lcd.setCursor(2,1);
  241.                                   lcd.print("function end...");
  242.                                   BeeBee();
  243.                                   WaitKeyIn(2);
  244.                                   goto Card_Management;
  245.                               }
  246.                            }
  247.                            if(KeyNum==4)
  248.                            {
  249.                               if(Initial_All()==false)
  250.                               {
  251.                                   LCD_Clear_Line(0);
  252.                                   lcd.setCursor(1,0);
  253.                                   lcd.print("4.Initial All");
  254.                                   LCD_Clear_Line(1);
  255.                                   lcd.setCursor(2,1);
  256.                                   lcd.print("function end...");
  257.                                   BeeBee();
  258.                                   WaitKeyIn(2);
  259.                                   goto Card_Management;
  260.                               }
  261.                             }
  262.                             BeeBee();
  263.                          }
  264.                          delay(1000);
  265. //                       break;
  266.                       } while(1);
  267.                       BeeBee();                   
  268.                     }
  269.                 } 
  270.                 else
  271.                 {
  272.                   LCD_Clear_Line(1);
  273.                   lcd.setCursor(1,1);
  274.                   lcd.print("Password Error!");
  275.                   PW_Error--;
  276.                   BeeBee();
  277.                   delay(1500);
  278.                 }
  279.             } while(PW_Error>0);
  280.             if(PW_Error==0)
  281.             {
  282.               LCD_Clear_Line(0);
  283.               lcd.setCursor(1,0);
  284.               lcd.print("System Hold 10 Min!");
  285.               BeeBee();
  286.               delay(600000);
  287.             }
  288.           }
  289.         }
  290.         else
  291.         {
  292.            LCD_Clear_Line(1);
  293.            lcd.setCursor(3,1);
  294.            lcd.print("RFID Read Error!");
  295.            BeeBee();
  296.         // Look for new cards
  297.       while( mfrc522.PICC_IsNewCardPresent())
  298.             {};
  299.          }
  300.     delay(100);
  301.         mfrc522.PICC_HaltA(); // Halt PICC
  302.         mfrc522.PCD_StopCrypto1();  // Stop encryption on PCD
  303.     delay(1000);
  304.     //
  305. }  

  306. //
  307. boolean AddCard()
  308. {
  309.  Wait_RFID_Card:
  310.     LCD_Clear_Line(0);
  311.     LCD_Clear_Line(1);
  312.     CardCount=EEPROM.read(0);
  313.     if(CardCount==100)
  314.     {
  315.       lcd.setCursor(2,0);
  316.       lcd.print("Card Number=100");
  317.       lcd.setCursor(1,1);
  318.       lcd.print("Sorry Cards Full!");
  319.       WaitKeyIn(10);
  320.       return(false);
  321.     }    
  322.     lcd.setCursor(1,0);
  323.     lcd.print("Place New Card on");
  324.     lcd.setCursor(2,1);
  325.     lcd.print("RFID Reader....");
  326.     WaitKeyRelease();
  327.     flag=true;
  328.     Wait_Time=millis();
  329.       while( ! mfrc522.PICC_IsNewCardPresent())
  330.     {
  331.         if(((millis()-Wait_Time)>=10000)||(keyPress()==true))
  332.         {
  333.           flag=false;
  334.           BeeBee();
  335.           return(false);
  336.         }
  337.      }
  338.      if(flag==true)
  339.      {
  340.        if(Read_RFID_SN()==false)
  341.        {
  342.           LCD_Clear_Line(1);
  343.           lcd.setCursor(3,1);
  344.           lcd.print("Setting Error!");
  345.           BeeBee();
  346.           exit;
  347.         }
  348.         else
  349.         {
  350.           LCD_Clear_Line(0);
  351.           LCD_Clear_Line(1);
  352.           lcd.setCursor(2,0);
  353.           lcd.print("Card SN:");
  354.           Show_RFID_SN();
  355.           if(Check_Card_Exist())
  356.           {
  357.             lcd.setCursor(1,1);
  358.             lcd.print("Card Already Exist!");
  359.             BeeBee();
  360.             WaitKeyIn(10);
  361.             goto Wait_RFID_Card;
  362.           }
  363.           if(Password_In()==true)
  364.           {
  365.             if(KeyNum==12)
  366.               goto Wait_RFID_Card;
  367.             if(Accept_Cancel()==false)
  368.               return(false);
  369.             if(KeyNum==12)
  370.               goto Wait_RFID_Card;
  371.             CardCount=EEPROM.read(0);
  372.             CardCount++;
  373.             for(byte j=0;j<4;j++)
  374.             {
  375.               EEPROM_data[j]=RFID_SN[j];
  376.               Serial.print(EEPROM_data[j],HEX);
  377.             }
  378.             Serial.println();
  379.             for(byte j=4;j<8;j++)
  380.             {
  381.                 EEPROM_data[j]=RFID_PW[j-4];
  382.               Serial.print(EEPROM_data[j],HEX);
  383.             }
  384.             if(Save_Check_EEPROM(CardCount*8,EEPROM_data)==true)
  385.             {
  386.                 LCD_Clear_Line(0);
  387.                 lcd.setCursor(2,0);
  388.                 lcd.print("Card Number=");
  389.                 if(CardCount<10)
  390.                    lcd.print("0");
  391.                 lcd.print(CardCount);
  392.                 LCD_Clear_Line(1);
  393.                 lcd.setCursor(2,1);
  394.                 lcd.print("New Card Saved!");
  395.                 EEPROM.write(0,CardCount);
  396.                 BeeBee();
  397.                 delay(1000);
  398.                 LCD_Clear_Line(0);
  399.                 lcd.setCursor(2,0);
  400.                 lcd.print("Press any key ..");
  401.                 WaitKeyIn(10);
  402.                 if(KeyNum==12)
  403.                     return(true);
  404.                 goto Wait_RFID_Card;
  405.             }
  406.           }
  407. /*          else
  408.           {
  409.             if(KeyNum==12)
  410.               goto Wait_RFID_Card;
  411.           }
  412. */       }
  413.      }
  414. }

  415. //
  416. boolean DelCard()
  417. {
  418.   int NumDigital,CardNum;
  419.   
  420.   Delete_Card:
  421.     LCD_Clear_Line(1);
  422.     lcd.setCursor(2,1);
  423.     lcd.print("2.Delete Card ");
  424.     LCD_Clear_Line(0);
  425.     lcd.setCursor(1,0);
  426.     lcd.print("Card Number:");
  427.        BeeBee();
  428.     CardNum=0;
  429.     NumDigital=0;
  430.     do
  431.     {
  432.  Input_Card_Num:
  433.       if((WaitKeyIn(10)==false)||(KeyNum==12))
  434.         return(false);
  435.       if(KeyNum==15)
  436.       {
  437.         if(NumDigital==0)
  438.           return(false);
  439.         else
  440.           goto Card_Num_End;
  441.       }
  442.       if((KeyNum>9))
  443.       {
  444.          LCD_Clear_Line(1);
  445.          lcd.setCursor(2,1);
  446.          lcd.print("Illegal Input!!");
  447.          BeeBee();
  448.          delay(1500);
  449.          LCD_Clear_Line(1);
  450.          lcd.setCursor(13+NumDigital,0);
  451.          goto Input_Card_Num;
  452.       }
  453.       lcd.print(KeyNum);
  454.       CardNum=CardNum*10+KeyNum;
  455.       NumDigital++;
  456.             Serial.println(NumDigital);
  457.     } while(NumDigital<3);
  458.   Card_Num_End:
  459.             Serial.println(CardNum);
  460.             Serial.println("CardNum Input end!");
  461.      CardCount=EEPROM.read(0);
  462.             Serial.println(CardCount);
  463.      if(CardNum==0)
  464.      {
  465.          LCD_Clear_Line(1);
  466.          lcd.setCursor(2,1);
  467.          lcd.print("Card Number Error!");
  468.          BeeBee();
  469.          delay(2000);
  470.          goto Delete_Card;
  471.      }  
  472.      if(CardNum==1)
  473.      {
  474.          LCD_Clear_Line(1);
  475.          lcd.setCursor(2,1);
  476.          lcd.print("Is Master Card!");
  477.          BeeBee();
  478.          delay(2000);
  479.          goto Delete_Card;
  480.      }  
  481.      if(CardNum>CardCount)
  482.      {
  483.          LCD_Clear_Line(1);
  484.          lcd.setCursor(0,1);
  485.          lcd.print("Card number too big!");
  486.          BeeBee();
  487.          delay(2000);
  488.          goto Delete_Card;
  489.      }  
  490.      if(CardNum==CardCount)
  491.        goto Dec_Card_Count;
  492.      for(byte j=0;j<8;j++)
  493.      {
  494.         EEPROM_data[j]=EEPROM.read(CardCount*8+j);
  495.         EEPROM.write(CardNum*8+j,EEPROM_data[j]);
  496.      }
  497.   Dec_Card_Count:
  498.      CardCount--;
  499.             Serial.println(CardCount);
  500.      EEPROM.write(0,CardCount);
  501.          LCD_Clear_Line(1);
  502.          lcd.setCursor(2,1);
  503.          lcd.print("Card ");
  504.          lcd.print(CardNum);
  505.          lcd.print(" been Del.!");
  506.          BeeBee();
  507.          delay(2000);
  508.          goto Delete_Card;
  509.   WaitKeyIn(0);
  510.   return(false);
  511. }
  512.             
  513. //            
  514. boolean ChangePW()
  515. {
  516.   int NumDigital,CardNum;
  517.   
  518.   Change_Password:
  519.     LCD_Clear_Line(1);
  520.     lcd.setCursor(2,1);
  521.     lcd.print("3.Change PW.");
  522.     LCD_Clear_Line(0);
  523.     lcd.setCursor(1,0);
  524.     lcd.print("Card Number:");
  525.        BeeBee();
  526.     CardNum=0;
  527.     NumDigital=0;
  528.     do
  529.     {
  530.  Input_Card_Num:
  531.       if((WaitKeyIn(10)==false)||(KeyNum==12))
  532.         return(false);
  533.          Serial.println(KeyNum);
  534.       if(KeyNum==15)
  535.       {
  536.         if(NumDigital==0)
  537.           return(false);
  538.         else
  539.           goto Card_Num_End;
  540.       }
  541.       if((KeyNum>9))
  542.       {
  543.          LCD_Clear_Line(1);
  544.          lcd.setCursor(2,1);
  545.          lcd.print("Illegal Input!!");
  546.          BeeBee();
  547.          delay(1500);
  548.          LCD_Clear_Line(1);
  549.          lcd.setCursor(13+NumDigital,0);
  550.          goto Input_Card_Num;
  551.       }
  552.       lcd.print(KeyNum);
  553.       CardNum=CardNum*10+KeyNum;
  554.       NumDigital++;
  555.             Serial.println(NumDigital);
  556.     } while(NumDigital<3);
  557.   Card_Num_End:
  558.      CardCount=EEPROM.read(0);
  559.             Serial.println(CardCount);
  560.      if(CardNum>CardCount)
  561.      {
  562.          LCD_Clear_Line(1);
  563.          lcd.setCursor(0,1);
  564.          lcd.print("Input Numbe Over!");
  565.          BeeBee();
  566.          delay(2000);
  567.          goto Change_Password;
  568.      }  
  569.      New_Password_In:
  570.      if(Password_In()==false)
  571.        return(false);
  572.      else
  573.      {
  574.        if(KeyNum==12)
  575.            goto New_Password_In;
  576.        if(Accept_Cancel()==false)
  577.          return(false);
  578.        else
  579.        {
  580.          if(KeyNum==12)
  581.            goto Change_Password;
  582.          for(byte i=0;i<4;i++)
  583.          {
  584.         Serial.print(RFID_PW[i]);
  585.            EEPROM.write(CardNum*8+4+i,RFID_PW[i]);
  586.          }
  587.         Serial.println();
  588.         Serial.println("New Password Saved!");
  589.         LCD_Clear_Line(0);
  590.         lcd.setCursor(1,0);
  591.         lcd.print("New Password Saved!");
  592.         BeeBee();
  593.        delay(1500); 
  594.        goto Change_Password;
  595.        }
  596.      }
  597. }
  598.             
  599. //
  600. boolean Initial_All()      
  601. {
  602.       BeeBee();
  603.       LCD_Clear_Line(0);
  604.       lcd.setCursor(0,0);
  605.       lcd.print("4.Initial All");
  606.       LCD_Clear_Line(1);
  607.       lcd.setCursor(2,1);
  608.       lcd.print("Are you sure?!");
  609.       delay(1500);
  610.       BeeBee();
  611.       if(Accept_Cancel()==true)
  612.       {
  613.         if(KeyNum==10)
  614.         {
  615.            EEPROM.write(0,1);
  616.           LCD_Clear_Line(0);
  617.           lcd.setCursor(0,0);
  618.           lcd.print("4.Initial All");
  619.             LCD_Clear_Line(1);
  620.             lcd.setCursor(2,1);
  621.             lcd.print("Job done!");
  622.             BeeBee();
  623.             delay(1500);
  624.             return(false);
  625.         }  
  626.       }
  627.       return(false);
  628. }            
  629. //
  630. boolean Initial_System()      
  631. {
  632.       BeeBee();
  633.       LCD_Clear_Line(0);
  634.       lcd.setCursor(0,0);
  635.       lcd.print("Master Card Setting.");
  636.       LCD_Clear_Line(1);
  637.       lcd.setCursor(2,1);
  638.       lcd.print("Please Process...");
  639.       while( ! mfrc522.PICC_IsNewCardPresent())
  640.         {};
  641.       if(Read_RFID_SN()==false)
  642.       {
  643.         LCD_Clear_Line(1);
  644.         lcd.setCursor(3,1);
  645.         lcd.print("Read UID Error!");
  646.         BeeBee();
  647.         delay(2000);
  648.         return(false);
  649.       }
  650.       else
  651.       {
  652.           LCD_Clear_Line(0);
  653.           lcd.setCursor(2,0);
  654.           lcd.print("Card SN:");
  655.           Show_RFID_SN();
  656.           LCD_Clear_Line(1);
  657.           for(byte i=4;i<8;i++)
  658.             EEPROM_data[i]=i-3;         
  659.           if(Save_Check_EEPROM(8,EEPROM_data)==true)
  660.           {
  661.             lcd.setCursor(4,1);
  662.             lcd.print("Setting OK!");
  663.             delay(2000);
  664.           }
  665.           else
  666.           {
  667.             lcd.setCursor(0,1);
  668.             lcd.print("Master SN Write Err!");
  669.             BeeBee();
  670.             BeeBee();
  671.             delay(2000);
  672.             return(false);
  673.           }
  674.         BeeBee();
  675.       }
  676.       while( mfrc522.PICC_IsNewCardPresent())
  677.             {};
  678.       LCD_Clear_Line(0);
  679.       lcd.setCursor(0,0);
  680.       lcd.print("1.Keep Card Setting.");
  681.       LCD_Clear_Line(1);
  682.       lcd.setCursor(0,1);
  683.       lcd.print("2.Initial All");
  684.       do
  685.       {
  686.           if(WaitKeyIn(10)==true)
  687.           {
  688.             if(KeyNum==1)
  689.               break;
  690.             if(KeyNum==2)
  691.             {
  692.                EEPROM.write(0,1);
  693.                  break;
  694.              }
  695.              BeeBee();
  696.            }
  697.       } while(1);
  698.       LCD_Clear_Line(0);
  699.       lcd.setCursor(1,0);
  700.       lcd.print("System Initial OK!");
  701.       delay(2000);
  702. }
  703. //            
  704. boolean Password_In()
  705. {
  706.     LCD_Clear_Line(1);
  707.     lcd.setCursor(3,1);
  708.     lcd.print("Password:");
  709.     BuzzerSound(300);
  710.     for(byte i=0;i<4;i++)
  711.     {
  712.       do
  713.       {
  714.          flag=WaitKeyIn(10);
  715.       } while(!((KeyNum==12)||((KeyNum<10))));
  716.       if(flag==false)
  717.            return(false);
  718.        if(KeyNum==12)
  719.          return(true);
  720.        RFID_PW[i]=KeyNum;
  721.        lcd.print(KeyNum,HEX);
  722.      }
  723.      return(true);
  724. }              

  725. //
  726. boolean Accept_Cancel()
  727. {
  728.     LCD_Clear_Line(0);
  729.     lcd.setCursor(0,0);
  730.     lcd.print("A-Accept C-Cancel:");
  731.     do
  732.     {
  733.       if(WaitKeyIn(10)==false)
  734.         return(false);
  735.       else if((KeyNum==10)||(KeyNum==12))
  736.         break;
  737.       else
  738.         BeeBee();
  739.     } while(1);
  740.     return(true);        
  741. }  

  742.   //
  743. boolean WaitKeyIn(byte wait)
  744. {
  745.    WaitKeyRelease();
  746.     Wait_Time=millis();
  747.    while(!keyPress())
  748.     {
  749.       if(wait!=0)
  750.       {
  751.         if((millis()-Wait_Time)>=(wait*1000))
  752.           return(false);
  753.       }  
  754.      }
  755.   delay(20);
  756.     KeyNum=findKeyNumber();
  757.   BuzzerSound(100);
  758.   return(true);
  759. }

  760.   //
  761. void WaitKeyRelease()
  762. {
  763.   while(keyPress())
  764.     {}
  765.   delay(30);
  766. }

  767. //
  768. boolean Check_Card_Exist()
  769. {
  770.     CardCount=EEPROM.read(0);
  771.     Serial.println(CardCount);
  772.     for(int i=0;i<CardCount;i++)
  773.     {
  774.       for(byte j=0;j<8;j++)
  775.         EEPROM_data[j]=EEPROM.read(i*8+8+j);
  776.       equal=true;
  777.       for(byte j=0;j<4;j++)
  778.       {
  779.     Serial.print(EEPROM_data[j],HEX);
  780.     Serial.print(",");
  781.     Serial.println(RFID_SN[j],HEX);
  782.         if(EEPROM_data[j]!=RFID_SN[j])
  783.           equal=false;
  784.       }
  785.       if(equal)
  786.       {
  787.         CardNumber=i+1;
  788.         return(true);
  789.       }
  790.     }
  791.     return(false);
  792. }

  793. //
  794. boolean Save_Check_EEPROM(int address,byte data[])
  795. {
  796.     for(byte i=0;i<8;i++)
  797.       EEPROM.write(address+i,data[i]);
  798.     for(byte i=0;i<8;i++)
  799.     {
  800.       byte Echo_Data=EEPROM.read(address+i);
  801.       if(Echo_Data!=data[i])
  802.       {
  803.     Serial.println("Data Error!");
  804.         return(false);
  805.       }
  806.      }
  807.     return(true);
  808. }  

  809. //
  810. void Show_RFID_SN()
  811. {
  812.           for(byte i=0;i<4;i++)
  813.           {
  814.             EEPROM_data[i]=Receive_Data[i];
  815.             if(Receive_Data[i]<0x10)
  816.               lcd.print("0");
  817.             lcd.print(Receive_Data[i],HEX);
  818.           }
  819. }          

  820. //
  821. boolean Read_RFID_SN()
  822. {
  823.     flag=false;
  824.     for(byte j=0;j<5;j++)
  825.     {
  826.         delay(20);
  827.         if (  mfrc522.PICC_ReadCardSerial())
  828.         {
  829.             flag=true;
  830.             break;
  831.         }
  832.     }
  833.       if(flag==false) return(false);
  834.         // Select one of the cards
  835.         Serial.print("Card UID:");    //Dump UID
  836.         for (byte i = 0; i < mfrc522.uid.size; i++) {
  837.             Receive_Data[i]=mfrc522.uid.uidByte[i];
  838.             RFID_SN[i]=Receive_Data[i];
  839.           Serial.print(Receive_Data[i] < 0x10 ? " 0" : " ");
  840.           Serial.print(Receive_Data[i], HEX);
  841.         } 
  842.         Serial.print(" PICC type: ");   // Dump PICC type
  843.         byte piccType = mfrc522.PICC_GetType(mfrc522.uid.sak);
  844.         Serial.println(mfrc522.PICC_GetTypeName(piccType));
  845.         Serial.println(" ");
  846.   return(true);
  847. }  
  848. //
  849. void LCD_Clear_Line(int Line_Num){

  850.     lcd.setCursor(0,Line_Num);
  851.     lcd.print("                    ");
  852. }  
  853. //
  854. //
  855. void BeeBee(){
  856.   BuzzerSound(100);
  857.   delay(100);
  858.   BuzzerSound(500);
  859. }  

  860. //
  861. void BuzzerSound(int BeeTime){
  862.   for(int i=0;i<BeeTime;i++)
  863.   {
  864.     digitalWrite(Buzzer,0);
  865.     delayMicroseconds(500);
  866.     digitalWrite(Buzzer,1);
  867.     delayMicroseconds(500);
  868.   }
  869. }  

  870. //
  871. byte findKeyNumber(){
  872.     delay(10);
  873.        KeyIn=analogRead(A0);
  874.        KeyIn=analogRead(A0);
  875. //    Serial.println(KeyIn);
  876.     for(byte i=0;i<16;i++)
  877.     {
  878.       if(KeyIn>keyValue[i])
  879.         return(keyNumbers[i]) ;
  880.     }
  881. }

  882. boolean keyPress(){
  883.      KeyIn=analogRead(A0);
  884.   if(KeyIn>40)
  885.     return(true);
  886.   else
  887.     return(false);
  888. }    


陸、全體程式列表

#include <LiquidCrystal.h> #include <EEPROM.h> #include <SPI.h> #include <MFRC522.h> /* * Signal     Pin            ...