element14 Community
element14 Community
    Register Log In
  • Site
  • Search
  • Log In Register
  • About Us
  • Community Hub
    Community Hub
    • What's New on element14
    • Feedback and Support
    • Benefits of Membership
    • Personal Blogs
    • Members Area
    • Achievement Levels
  • Learn
    Learn
    • Ask an Expert
    • eBooks
    • element14 presents
    • Learning Center
    • Tech Spotlight
    • STEM Academy
    • Webinars, Training and Events
    • Learning Groups
  • Technologies
    Technologies
    • 3D Printing
    • FPGA
    • Industrial Automation
    • Internet of Things
    • Power & Energy
    • Sensors
    • Technology Groups
  • Challenges & Projects
    Challenges & Projects
    • Design Challenges
    • element14 presents Projects
    • Project14
    • Arduino Projects
    • Raspberry Pi Projects
    • Project Groups
  • Products
    Products
    • Arduino
    • Avnet Boards Community
    • Dev Tools
    • Manufacturers
    • Multicomp Pro
    • Product Groups
    • Raspberry Pi
    • RoadTests & Reviews
  • Store
    Store
    • Visit Your Store
    • Choose another store...
      • Europe
      •  Austria (German)
      •  Belgium (Dutch, French)
      •  Bulgaria (Bulgarian)
      •  Czech Republic (Czech)
      •  Denmark (Danish)
      •  Estonia (Estonian)
      •  Finland (Finnish)
      •  France (French)
      •  Germany (German)
      •  Hungary (Hungarian)
      •  Ireland
      •  Israel
      •  Italy (Italian)
      •  Latvia (Latvian)
      •  
      •  Lithuania (Lithuanian)
      •  Netherlands (Dutch)
      •  Norway (Norwegian)
      •  Poland (Polish)
      •  Portugal (Portuguese)
      •  Romania (Romanian)
      •  Russia (Russian)
      •  Slovakia (Slovak)
      •  Slovenia (Slovenian)
      •  Spain (Spanish)
      •  Sweden (Swedish)
      •  Switzerland(German, French)
      •  Turkey (Turkish)
      •  United Kingdom
      • Asia Pacific
      •  Australia
      •  China
      •  Hong Kong
      •  India
      •  Korea (Korean)
      •  Malaysia
      •  New Zealand
      •  Philippines
      •  Singapore
      •  Taiwan
      •  Thailand (Thai)
      • Americas
      •  Brazil (Portuguese)
      •  Canada
      •  Mexico (Spanish)
      •  United States
      Can't find the country/region you're looking for? Visit our export site or find a local distributor.
  • Translate
  • Profile
  • Settings
Arduino
  • Products
  • More
Arduino
Arduino Forum I have a project and facing an issue in coding
  • Blog
  • Forum
  • Documents
  • Quiz
  • Events
  • Polls
  • Files
  • Members
  • Mentions
  • Sub-Groups
  • Tags
  • More
  • Cancel
  • New
Join Arduino to participate - click to join for free!
Actions
  • Share
  • More
  • Cancel
Forum Thread Details
  • State Not Answered
  • Replies 1 reply
  • Subscribers 391 subscribers
  • Views 218 views
  • Users 0 members are here
Related

I have a project and facing an issue in coding

Former Member
Former Member over 11 years ago

Really i need your help

 

i have a project using mega arduino 2560 and MR 522 reader

 

this code is not working and when tag passing reader nothing is shown

 

//Library Imports

#include <SoftwareSerial.h>

#include <EEPROM.h>

#include <LiquidCrystal.h>

#include <SPI.h>

 

 

 

 

 

//START USER EDIT

const int SS_PIN = 53;

const int RST_PIN = 5;

 

 

const int CARD_SIZE = 5;

byte MASTER_ADD[5] = {185, 39, 76, 181, 103};

byte MASTER_DEL[5] = {51, 185, 228, 199, 169 };

//END USER EDIT

 

SoftwareSerial rfid(SS_PIN, RST_PIN); //pin10 Rx, pin3 Tx, mega doesn't support change interrupts on pin 2z

byte readCard[CARD_SIZE];

byte storedCard[CARD_SIZE];

boolean match = false;

boolean programMode = false;

 

void setup()

{

  pinMode(STRIKEDOOR, OUTPUT);

  Serial.begin(9600);

  Serial.println("Welcome to Chuck's RFID access control:");

  // set the data rate for the SoftwareSerial port

  rfid.begin(9600);

  delay(10);

  startAutoScan();

}

 

void loop() // run over and over

{

  if(rfid.available() > 0) {

    getID();

    printID();

    if (programMode)

    {

      if ( !checkTwo(readCard,MASTER_ADD) )

      {

        writeID(readCard);

        Serial.println("New key added");

        programMode = false;

      }

    }

    else if ( checkTwo(readCard,MASTER_ADD) )

    {

      programMode = true;

      Serial.println("Detected MASTER ADD key");

      Serial.println("Swipe key you wish to add...");

    }

    else if ( checkTwo(readCard,MASTER_DEL) )

    {

      if (EEPROM.read(0) > 0) { EEPROM.write(0,0); }

      Serial.println("Detected MASTER DEL key");

      Serial.println("All guest keys disabled");

    }

    else if ( findID(readCard) )

    {

      Serial.println("Detected VALID key");

      Serial.println("Open Door");

      openDoor(2);

    }

    else

    {

      Serial.println("ID not valid");

    }

  }

}

 

 

void startAutoScan()

{

  rfid.write(0x02); //Send the command to read RFID tag, please refer to the manual for more detail.

}

 

 

void getID()

{

  for(int i=0; i<CARD_SIZE; i++){

    delay(10);

    readCard[i] = rfid.read();

  }

}

 

void printID()

{

  for(int i=0; i<CARD_SIZE; i++) {

    Serial.print(readCard[i],DEC);

    Serial.print(" ");

  }

  Serial.println();

}

 

// Check two arrays of bytes to see if they are exact matches

boolean checkTwo ( byte a[], byte b[] )

{

  if ( a[0] != NULL )             // Make sure there is something in the array first

    match = true;                 // Assume they match at first

  

  for ( int k = 0;  k < CARD_SIZE; k++ )  // Loop 5 times

  {

    /*

    Serial.print("[");

    Serial.print(k);

    Serial.print("] ReadCard [");

    Serial.print(a[k], DEC);

    Serial.print("] StoredCard [");

    Serial.print(b[k], DEC);

    Serial.print("] \n");

    */

    if ( a[k] != b[k] )           // IF a != b then set match = false, one fails, all fail

     match = false;

  }

  if ( match )                    // Check to see if if match is still true

  {

    //Serial.print("Strings Match! \n");

    return true;                  // Return true

  }

  else {

    //Serial.print("Strings do not match \n");

    return false;                 // Return false

  }

}

 

// Read an ID from EEPROM and save it to the storedCard[6] array

void readID( int number )  // Number = position in EEPROM to get the 5 Bytes from

{

   int start = (number * CARD_SIZE ) - CARD_SIZE + 1;  // Figure out starting position

   //Serial.print("Start: ");

   //Serial.print(start);

   //Serial.print("\n\n");

 

   for ( int i = 0; i < CARD_SIZE; i++ )  // Loop 5 times to get the 5 Bytes

   {

     storedCard[i] = EEPROM.read(start+i);  // Assign values read from EEPROM to array

     /*

     Serial.print("Read [");

     Serial.print(start+i);

     Serial.print("] [");

     Serial.print(storedCard[i], DEC);

     Serial.print("] \n");

     */

   }

}

 

// Looks in the EEPROM to try to match any of the EEPROM ID's with the passed ID

boolean findID( byte find[] )

{

  int count = EEPROM.read(0);             // Read the first Byte of EEPROM that

  //Serial.print("Count: ");                // stores the number of ID's in EEPROM

  //Serial.print(count);

  //Serial.print("\n");

  for ( int i = 1; i <= count; i++ )      // Loop once for each EEPROM entry

  {

    readID(i);                            // Read an ID from EEPROM, it is stored in storedCard[6]

    if( checkTwo( find, storedCard ) )    // Check to see if the storedCard read from EEPROM

    {                                     // is the same as the find[] ID card passed

      //Serial.print("We have a matched card!!! \n");

      return true;

      break;                              // Stop looking we found it

    }

    else                                  // If not, return false

    {

      //Serial.print("No Match here.... \n");

    }

  

  }

  return false;

}

 

// Write an array to the EEPROM in the next available slot

void writeID( byte a[] )

{

  if ( !findID( a ) )          // Before we write to the EEPROM, check to see if we have seen this card before!

  {

    int num = EEPROM.read(0);  // Get the numer of used spaces, position 0 stores the number of ID cards

    /*

    Serial.print("Num: ");

    Serial.print(num);

    Serial.print(" \n");

    */

    int start = ( num * CARD_SIZE ) + 1;   // Figure out where the next slot starts

 

    num++;                         // Increment the counter by one

    EEPROM.write( 0, num );        // Write the new count to the counter

  

    for ( int j = 0; j < CARD_SIZE; j++ )  // Loop 5 times

    {

      EEPROM.write( start+j, a[j] );  // Write the array values to EEPROM in the right position

      /*

      Serial.print("W[");

      Serial.print(start+j);

      Serial.print("] Value [");

      Serial.print(a[j], );

      Serial.print("] \n");

      */

    }

    //successWrite();

  }

  else

  {

    //failedWrite();

  }

}

 

void openDoor(int setDelay)

{

  setDelay *=1000; // Sets delay in seconds

  digitalWrite(STRIKEDOOR, HIGH); // Unlock door

  delay(setDelay);

  digitalWrite(STRIKEDOOR,LOW); // Relock door

}

  • Sign in to reply
  • Cancel
Parents
  • ravi_butani
    0 ravi_butani over 11 years ago

    First as arduino mega hve many as 4 hardware serial port, I cant understand why r u using software serial? next first put bare minimum code on your arduino than use arduino as just USB - UART converter than connect ur rfid reader to check that its working or not...

     

    Please put schematic with question.... it will be better to understand for us..

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
Reply
  • ravi_butani
    0 ravi_butani over 11 years ago

    First as arduino mega hve many as 4 hardware serial port, I cant understand why r u using software serial? next first put bare minimum code on your arduino than use arduino as just USB - UART converter than connect ur rfid reader to check that its working or not...

     

    Please put schematic with question.... it will be better to understand for us..

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
Children
No Data
element14 Community

element14 is the first online community specifically for engineers. Connect with your peers and get expert answers to your questions.

  • Members
  • Learn
  • Technologies
  • Challenges & Projects
  • Products
  • Store
  • About Us
  • Feedback & Support
  • FAQs
  • Terms of Use
  • Privacy Policy
  • Legal and Copyright Notices
  • Sitemap
  • Cookies

An Avnet Company © 2025 Premier Farnell Limited. All Rights Reserved.

Premier Farnell Ltd, registered in England and Wales (no 00876412), registered office: Farnell House, Forge Lane, Leeds LS12 2NE.

ICP 备案号 10220084.

Follow element14

  • X
  • Facebook
  • linkedin
  • YouTube