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 Greate the prices sum with Arduino
  • 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
  • Replies 3 replies
  • Subscribers 391 subscribers
  • Views 377 views
  • Users 0 members are here
Related

Greate the prices sum with Arduino

ritamüller
ritamüller over 10 years ago

Hallo,

I have RFID 13.56 MHz / NFC Module for Arduino from Cooking Hacks (Cooking Hacks - RFID 13.56 MHz / NFC Module for Arduino) .

I have 4 RFID cards and I have saved a Price in the first byte in block 4 for each card,  and I want to write a Progrm that creates the sum of the price each times when I read a card.

at the end I want to get a total price of the four cards , is it possible to apply this program with my Model?

 

i don't know how should i start it ?

 

Thx

 

Rita

  • Sign in to reply
  • Cancel

Top Replies

  • mcb1
    mcb1 over 10 years ago in reply to ritamüller +1
    Rita Your line Total price = analogRead(2); won't work. What you have done there is read the analogue value on A2, and then assigned that value to Total Price. That is likely to be either 0 or 1024, and…
Parents
  • mcb1
    mcb1 over 10 years ago

    Rita

    Yes it is possible ...

    You simply read the card (there are multiple examples around on the internet/supplier of your parts)

    Then add the value you read to another variable that is used for the total.

     

    This is probably the simplist approach for a beginner.

     

    Mark

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Cancel
  • ritamüller
    ritamüller over 10 years ago in reply to mcb1

    Hallo Mark ,

    Thanks for replay.

     

    i have tried with the same program in Cooking hacks seite (read block )

     

    i have added a variable'' Total price ''.

    should I add this variable with dataTX[0] ( the first byte which the price contained ) , Total price = Total price + dataTX[0]; ?

    can i use the analogRead(2) as output for my variable ?

     

    Thx

     

    Rita

     

     

    Program :

    /*

    * RFID/NFC module for Arduino.

    * Calculation the total price 

    */

     

     

    uint8_t data RX[35];//Receive buffer.

    uint8_t data TX[35];//Transmit buffer.

    uint8_t _UID[4];// stores the UID (unique identifier) of a card.

    uint8_t keyAccess[] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF } ;// stores the key or password.

    uint8_t address = 0x04;//Address to read.

    uint8_t ATQ[2];//Answer to request

    uint8_t state;//state of the process

    uint8_t aux[16];//Auxiliar buffer.

    int Total Price = 0;// Varialbe

    Total price = analogRead(2);

     

     

    void setup()

    {

      //Start serial port 115200 bps:

      Serial.begin(115200);

      delay(100);

      Serial.print("RFID/NFC @ 13.56 MHz module started");

      delay(1000);

      //!It is needed to launch a simple command to sycnchronize

      getFirmware();

      configureSAM();

    }

     

     

    void loop()

    {

      Serial.print("\n");

      Serial.println("Ready to read...");

      /////////////////////////////////////////////////////////////

      //Get the UID Identifier

      init(_UID, ATQ);

      Serial.print("\n");

      Serial.print( "The UID : ");

      print(_UID , 4);

      /////////////////////////////////////////////////////////////

      //Auntenticate a block with his keyAccess

      state = authenticate(_UID, address, keyAccess);

      Serial.print("\n");

     

     

      if ( state == 0) {

      Serial.println("Authentication block OK");

      } else {

      Serial.println("Authentication failed");

      }

      /////////////////////////////////////////////////////////////

      //Read from address after authentication

      state = readData(address, aux);

      Serial.print("\n");

     

     

      if (state == 0) {

      Serial.println("Read Price ok ");

      } else {

      Serial.println("Read failed");

      }

     

     

      Serial.print("\n");

      Serial.print("Price : ");

      print(aux , 16);

      Serial.print("\n");

      delay(2000);

    }

     

     

    // Total price

    {

      Total price = Total price + dataTX[0];

      Serial.println(analogRead(2));

      delay(1000);

    }

     

     

    //**********************************************************************

    //!The goal of this command is to detect as many targets (maximum MaxTg)

    // as possible in passive mode.

    uint8_t init(uint8_t *UID , uint8_t *ATQ)   //! Request InListPassive

    {

      Serial.flush();

     

     

      dataTX[0] = 0x04; // Length

      lengthCheckSum(dataTX); // Length Checksum

      dataTX[2] = 0xD4;

      dataTX[3] = 0x4A; // Code

      dataTX[4] = 0x01; //MaxTarget

      dataTX[5] = 0x00; //BaudRate = 106Kbps

      dataTX[6] = 0x00; // Clear checkSum position

      checkSum(dataTX);

     

     

      sendTX(dataTX , 7 ,23);

     

     

      for (int i = 17; i < (21) ; i++){

      _UID[i-17] = dataRX[i];

      UID[i-17] = _UID[i-17];

      }

     

     

      ATQ[0] = dataRX[13];

      ATQ[1] = dataRX[14];

     

     

      if ((dataRX[9]== 0xD5) & (dataRX[10] == 0x4B) & (dataRX[11] == 0x01)) {

      return 0;

      } else {

      return 1;

      }

    }

    //**********************************************************************

    //!A block must be authenticated before read and write operations

    uint8_t authenticate(uint8_t *UID, uint8_t blockAddress, uint8_t *keyAccess)

    {

      dataTX[0] = 0x0F;

      lengthCheckSum(dataTX);

      dataTX[2] = 0xD4;

      dataTX[3] = 0x40; // inDataEchange

      dataTX[4] = 0x01; //Number of targets

      dataTX[5] = 0x60; // Authentication code

      dataTX[6] = blockAddress;

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

      dataTX[i + 7] = keyAccess[i];

      }

      dataTX[13] = UID[0];  dataTX[14] = UID[1];

      dataTX[15] = UID[2];  dataTX[16] = UID[3];

      dataTX[17] = 0x00;

      checkSum(dataTX);

      sendTX(dataTX , 18 ,14);

     

     

      if ((dataRX[9]== 0xD5) & (dataRX[10] == 0x41) & (dataRX[11] == 0x00)) {

      return 0;

      } else {

      return 1;

      }

    }

    //**********************************************************************

    //!Write 16 bytes in address .

    uint8_t writeData(uint8_t address, uint8_t *blockData)  //!Writing

    {

      Serial.print("                ");

      dataTX[0] = 0x15;

      lengthCheckSum(dataTX); // Length Checksum

      dataTX[2] = 0xD4;

      dataTX[3] = 0x40;//inDataEchange CODE

      dataTX[4] = 0x01;//Number of targets

      dataTX[5] = 0xA0;//Write Command

      dataTX[6] = address; //Address

     

     

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

      dataTX[i+7] = blockData[i];

      }

     

     

      dataTX[23] = 0x00;

      checkSum(dataTX);

      sendTX(dataTX , 24 ,14);

     

     

      if ((dataRX[9]== 0xD5) & (dataRX[10] == 0x41) & (dataRX[11] == 0x00)) {

      return 0;

      } else {

      return 1;

      }

    }

    //**********************************************************************

    //!Read 16 bytes from  address .

    uint8_t readData(uint8_t address, uint8_t *readData) //!Reading

    {

      Serial.print("                ");

     

     

      dataTX[0] = 0x05;

      lengthCheckSum(dataTX); // Length Checksum

      dataTX[2] = 0xD4; // Code

      dataTX[3] = 0x40; // Code

      dataTX[4] = 0x01; // Number of targets

      dataTX[5] = 0x30; //ReadCode

      dataTX[6] = address;  //Read address

      dataTX[7] = 0x00;

      checkSum(dataTX);

      sendTX(dataTX , 8, 30);

      memset(readData, 0x00, 16); 

     

     

      if ((dataRX[9]== 0xD5) & (dataRX[10] == 0x41) & (dataRX[11] == 0x00)) {

      for (int i = 12; i < 28; i++) {

      readData[i-12] = dataRX[i];

      }

      return 0;

      } else {

      return 1;

      }

    }

    //**********************************************************************

    //!The PN532 sends back the version of the embedded firmware.

    bool getFirmware(void)  //! It is needed to launch a simple command to sycnchronize

    {

      Serial.print("                ");

     

     

      memset(dataTX, 0x00, 35);

      dataTX[0] = 0x02; // Length

      lengthCheckSum(dataTX); // Length Checksum

      dataTX[2] = 0xD4; // CODE

      dataTX[3] = 0x02; //TFI

      checkSum(dataTX); //0x2A; //Checksum  

     

     

      sendTX(dataTX , 5 , 17);

      Serial.print("\n");

      Serial.print("Your Firmware version is : ");

     

     

      for (int i = 11; i < (15) ; i++){

      Serial.print(dataRX[i], HEX);

      Serial.print(" ");

      }

      Serial.print("\n");

    }

     

     

    //**********************************************************************

    //!Print data stored in vectors .

    void print(uint8_t * _data, uint8_t length)

    {

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

      Serial.print(_data[i], HEX);

      Serial.print(" ");

      }

      Serial.print("\n");

    }

    //**********************************************************************

    //!This command is used to set internal parameters of the PN532,

    bool configureSAM(void)//! Configure the SAM

    {

      Serial.print("               ");

     

     

      dataTX[0] = 0x05; //Length

      lengthCheckSum(dataTX); // Length Checksum

      dataTX[2] = 0xD4;

      dataTX[3] = 0x14;

      dataTX[4] = 0x01; // Normal mode

      dataTX[5] = 0x14; // TimeOUT

      dataTX[6] = 0x00; // IRQ

      dataTX[7] = 0x00; // Clean checkSum position

      checkSum(dataTX);

     

     

      sendTX(dataTX , 8, 13);

    }

    //**********************************************************************

    //!Send data stored in dataTX

    void sendTX(uint8_t *dataTX, uint8_t length, uint8_t outLength)

    {

      Serial.print(char(0x00));

      Serial.print(char(0x00));

      Serial.print(char(0xFF));

     

     

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

      Serial.print(char(dataTX[i]));

      }

     

     

      Serial.print(char(0x00));

      getACK();

      waitResponse();// Receive response

      getData(outLength);

    }

    //**********************************************************************

    //!Wait for ACK response and stores it in the dataRX buffer

    void getACK(void)

    {

      delay(5);

      waitResponse();

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

      dataRX[i] = Serial.read();

      }

    }

    //**********************************************************************

    //!Wait the response of the module

    void waitResponse(void)

    {

      int val = 0xFF;

      int cont = 0x00;

      while(val != 0x00) { //Wait for 0x00 response

      val = Serial.read();

      delay(5);

      cont ++;

      }

    }

    //**********************************************************************

    //!Get data from the module

    void getData(uint8_t outLength)

    {

      for (int i=5; i < outLength; i++) {

      dataRX[i] = Serial.read();//read data from the module.

      }

    }

    //**********************************************************************

    //!Calculates the checksum and stores it in dataTX buffer

    void checkSum(uint8_t *dataTX)

    {

      for (int i = 0; i < dataTX[0] ; i++) {

      dataTX[dataTX[0] + 2] += dataTX[i + 2];

      }

      byte(dataTX[dataTX[0] + 2]= - dataTX[dataTX[0] + 2]);

    }

    //**********************************************************************

    //!Calculates the length checksum and sotres it in the buffer.

    uint8_t lengthCheckSum(uint8_t *dataTX)

    {

      dataTX[1] = byte(0x100 - dataTX[0]);

    }

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Cancel
Reply
  • ritamüller
    ritamüller over 10 years ago in reply to mcb1

    Hallo Mark ,

    Thanks for replay.

     

    i have tried with the same program in Cooking hacks seite (read block )

     

    i have added a variable'' Total price ''.

    should I add this variable with dataTX[0] ( the first byte which the price contained ) , Total price = Total price + dataTX[0]; ?

    can i use the analogRead(2) as output for my variable ?

     

    Thx

     

    Rita

     

     

    Program :

    /*

    * RFID/NFC module for Arduino.

    * Calculation the total price 

    */

     

     

    uint8_t data RX[35];//Receive buffer.

    uint8_t data TX[35];//Transmit buffer.

    uint8_t _UID[4];// stores the UID (unique identifier) of a card.

    uint8_t keyAccess[] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF } ;// stores the key or password.

    uint8_t address = 0x04;//Address to read.

    uint8_t ATQ[2];//Answer to request

    uint8_t state;//state of the process

    uint8_t aux[16];//Auxiliar buffer.

    int Total Price = 0;// Varialbe

    Total price = analogRead(2);

     

     

    void setup()

    {

      //Start serial port 115200 bps:

      Serial.begin(115200);

      delay(100);

      Serial.print("RFID/NFC @ 13.56 MHz module started");

      delay(1000);

      //!It is needed to launch a simple command to sycnchronize

      getFirmware();

      configureSAM();

    }

     

     

    void loop()

    {

      Serial.print("\n");

      Serial.println("Ready to read...");

      /////////////////////////////////////////////////////////////

      //Get the UID Identifier

      init(_UID, ATQ);

      Serial.print("\n");

      Serial.print( "The UID : ");

      print(_UID , 4);

      /////////////////////////////////////////////////////////////

      //Auntenticate a block with his keyAccess

      state = authenticate(_UID, address, keyAccess);

      Serial.print("\n");

     

     

      if ( state == 0) {

      Serial.println("Authentication block OK");

      } else {

      Serial.println("Authentication failed");

      }

      /////////////////////////////////////////////////////////////

      //Read from address after authentication

      state = readData(address, aux);

      Serial.print("\n");

     

     

      if (state == 0) {

      Serial.println("Read Price ok ");

      } else {

      Serial.println("Read failed");

      }

     

     

      Serial.print("\n");

      Serial.print("Price : ");

      print(aux , 16);

      Serial.print("\n");

      delay(2000);

    }

     

     

    // Total price

    {

      Total price = Total price + dataTX[0];

      Serial.println(analogRead(2));

      delay(1000);

    }

     

     

    //**********************************************************************

    //!The goal of this command is to detect as many targets (maximum MaxTg)

    // as possible in passive mode.

    uint8_t init(uint8_t *UID , uint8_t *ATQ)   //! Request InListPassive

    {

      Serial.flush();

     

     

      dataTX[0] = 0x04; // Length

      lengthCheckSum(dataTX); // Length Checksum

      dataTX[2] = 0xD4;

      dataTX[3] = 0x4A; // Code

      dataTX[4] = 0x01; //MaxTarget

      dataTX[5] = 0x00; //BaudRate = 106Kbps

      dataTX[6] = 0x00; // Clear checkSum position

      checkSum(dataTX);

     

     

      sendTX(dataTX , 7 ,23);

     

     

      for (int i = 17; i < (21) ; i++){

      _UID[i-17] = dataRX[i];

      UID[i-17] = _UID[i-17];

      }

     

     

      ATQ[0] = dataRX[13];

      ATQ[1] = dataRX[14];

     

     

      if ((dataRX[9]== 0xD5) & (dataRX[10] == 0x4B) & (dataRX[11] == 0x01)) {

      return 0;

      } else {

      return 1;

      }

    }

    //**********************************************************************

    //!A block must be authenticated before read and write operations

    uint8_t authenticate(uint8_t *UID, uint8_t blockAddress, uint8_t *keyAccess)

    {

      dataTX[0] = 0x0F;

      lengthCheckSum(dataTX);

      dataTX[2] = 0xD4;

      dataTX[3] = 0x40; // inDataEchange

      dataTX[4] = 0x01; //Number of targets

      dataTX[5] = 0x60; // Authentication code

      dataTX[6] = blockAddress;

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

      dataTX[i + 7] = keyAccess[i];

      }

      dataTX[13] = UID[0];  dataTX[14] = UID[1];

      dataTX[15] = UID[2];  dataTX[16] = UID[3];

      dataTX[17] = 0x00;

      checkSum(dataTX);

      sendTX(dataTX , 18 ,14);

     

     

      if ((dataRX[9]== 0xD5) & (dataRX[10] == 0x41) & (dataRX[11] == 0x00)) {

      return 0;

      } else {

      return 1;

      }

    }

    //**********************************************************************

    //!Write 16 bytes in address .

    uint8_t writeData(uint8_t address, uint8_t *blockData)  //!Writing

    {

      Serial.print("                ");

      dataTX[0] = 0x15;

      lengthCheckSum(dataTX); // Length Checksum

      dataTX[2] = 0xD4;

      dataTX[3] = 0x40;//inDataEchange CODE

      dataTX[4] = 0x01;//Number of targets

      dataTX[5] = 0xA0;//Write Command

      dataTX[6] = address; //Address

     

     

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

      dataTX[i+7] = blockData[i];

      }

     

     

      dataTX[23] = 0x00;

      checkSum(dataTX);

      sendTX(dataTX , 24 ,14);

     

     

      if ((dataRX[9]== 0xD5) & (dataRX[10] == 0x41) & (dataRX[11] == 0x00)) {

      return 0;

      } else {

      return 1;

      }

    }

    //**********************************************************************

    //!Read 16 bytes from  address .

    uint8_t readData(uint8_t address, uint8_t *readData) //!Reading

    {

      Serial.print("                ");

     

     

      dataTX[0] = 0x05;

      lengthCheckSum(dataTX); // Length Checksum

      dataTX[2] = 0xD4; // Code

      dataTX[3] = 0x40; // Code

      dataTX[4] = 0x01; // Number of targets

      dataTX[5] = 0x30; //ReadCode

      dataTX[6] = address;  //Read address

      dataTX[7] = 0x00;

      checkSum(dataTX);

      sendTX(dataTX , 8, 30);

      memset(readData, 0x00, 16); 

     

     

      if ((dataRX[9]== 0xD5) & (dataRX[10] == 0x41) & (dataRX[11] == 0x00)) {

      for (int i = 12; i < 28; i++) {

      readData[i-12] = dataRX[i];

      }

      return 0;

      } else {

      return 1;

      }

    }

    //**********************************************************************

    //!The PN532 sends back the version of the embedded firmware.

    bool getFirmware(void)  //! It is needed to launch a simple command to sycnchronize

    {

      Serial.print("                ");

     

     

      memset(dataTX, 0x00, 35);

      dataTX[0] = 0x02; // Length

      lengthCheckSum(dataTX); // Length Checksum

      dataTX[2] = 0xD4; // CODE

      dataTX[3] = 0x02; //TFI

      checkSum(dataTX); //0x2A; //Checksum  

     

     

      sendTX(dataTX , 5 , 17);

      Serial.print("\n");

      Serial.print("Your Firmware version is : ");

     

     

      for (int i = 11; i < (15) ; i++){

      Serial.print(dataRX[i], HEX);

      Serial.print(" ");

      }

      Serial.print("\n");

    }

     

     

    //**********************************************************************

    //!Print data stored in vectors .

    void print(uint8_t * _data, uint8_t length)

    {

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

      Serial.print(_data[i], HEX);

      Serial.print(" ");

      }

      Serial.print("\n");

    }

    //**********************************************************************

    //!This command is used to set internal parameters of the PN532,

    bool configureSAM(void)//! Configure the SAM

    {

      Serial.print("               ");

     

     

      dataTX[0] = 0x05; //Length

      lengthCheckSum(dataTX); // Length Checksum

      dataTX[2] = 0xD4;

      dataTX[3] = 0x14;

      dataTX[4] = 0x01; // Normal mode

      dataTX[5] = 0x14; // TimeOUT

      dataTX[6] = 0x00; // IRQ

      dataTX[7] = 0x00; // Clean checkSum position

      checkSum(dataTX);

     

     

      sendTX(dataTX , 8, 13);

    }

    //**********************************************************************

    //!Send data stored in dataTX

    void sendTX(uint8_t *dataTX, uint8_t length, uint8_t outLength)

    {

      Serial.print(char(0x00));

      Serial.print(char(0x00));

      Serial.print(char(0xFF));

     

     

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

      Serial.print(char(dataTX[i]));

      }

     

     

      Serial.print(char(0x00));

      getACK();

      waitResponse();// Receive response

      getData(outLength);

    }

    //**********************************************************************

    //!Wait for ACK response and stores it in the dataRX buffer

    void getACK(void)

    {

      delay(5);

      waitResponse();

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

      dataRX[i] = Serial.read();

      }

    }

    //**********************************************************************

    //!Wait the response of the module

    void waitResponse(void)

    {

      int val = 0xFF;

      int cont = 0x00;

      while(val != 0x00) { //Wait for 0x00 response

      val = Serial.read();

      delay(5);

      cont ++;

      }

    }

    //**********************************************************************

    //!Get data from the module

    void getData(uint8_t outLength)

    {

      for (int i=5; i < outLength; i++) {

      dataRX[i] = Serial.read();//read data from the module.

      }

    }

    //**********************************************************************

    //!Calculates the checksum and stores it in dataTX buffer

    void checkSum(uint8_t *dataTX)

    {

      for (int i = 0; i < dataTX[0] ; i++) {

      dataTX[dataTX[0] + 2] += dataTX[i + 2];

      }

      byte(dataTX[dataTX[0] + 2]= - dataTX[dataTX[0] + 2]);

    }

    //**********************************************************************

    //!Calculates the length checksum and sotres it in the buffer.

    uint8_t lengthCheckSum(uint8_t *dataTX)

    {

      dataTX[1] = byte(0x100 - dataTX[0]);

    }

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Cancel
Children
  • mcb1
    mcb1 over 10 years ago in reply to ritamüller

    Rita

     

    Your line Total price = analogRead(2); won't work.

    What you have done there is read the analogue value on A2, and then assigned that value to Total Price.

    That is likely to be either 0 or 1024, and I'm not sure if it will read it during the declaration part ...it might but that's not want you want anyway.

     

    You've declared Total Price as an integer (see link below for limits) BUT I'm pretty sure a space won't be accepted .. (I could be wrong) so try

    int TotalPrice = 0;// Varialbe

     

    You haven't explained how you want to see the Total Price, but your code suggests you are sending it out the serial.

      Total price = Total price + dataTX[0]; 
      Serial.println(analogRead(2));

     

    You will need to change the above to

      TotalPrice = TotalPrice + dataTX[0]; 
      Serial.println(TotalPrice);

     

    Your comment Total price = Total price + dataTX[0]; is the correct method to add the values.

    But I'm not sure dataTx[0] holds the price value ...

    I don't have one of these and I don't have time to try the code but these lines here suggest the data is the 16 blocks in the aux variable.

      Serial.print("\n");
      Serial.print("Price : ");
      print(aux , 16);
      Serial.print("\n");
      delay(2000);

    aux is an array (the aux[] tells the code that) so that line won't work (the print also won't work it needs to be Serial.print)

    Think of an array as a calendar where the array is month[12]  ..... January will be month[0] (computers start at zero) and Dec will be month[11].


    So in order to print each of the 16 values held in the aux array, you need a for loop that

      for (int i=0; i < 16; i++) {
         Serial.print(aux[i]);
      }



    Variables

    Since you are only storing numbers less than 255 on the card a simple integer (int) will handle all 4 until you get to 32768 ( see http://www.arduino.cc/en/Reference/Int )

     

    Lesson 2

    For posting code, highlight your code select the >> and drop down to Syntax Highlighting, then select C++ for Arduino.

    It will show as below ...

    /*
    * RFID/NFC module for Arduino. 
    * Calculation the total price  
    */
    
    
    uint8_t data RX[35];//Receive buffer.
    uint8_t data TX[35];//Transmit buffer. 
    uint8_t _UID[4];// stores the UID (unique identifier) of a card.
    uint8_t keyAccess[] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF } ;// stores the key or password.
    uint8_t address = 0x04;//Address to read.
    uint8_t ATQ[2];//Answer to request
    uint8_t state;//state of the process
    uint8_t aux[16];//Auxiliar buffer. 
    int Total Price = 0;// Varialbe 
    Total price = analogRead(2);
    
    
    void setup()
    {
      //Start serial port 115200 bps:
      Serial.begin(115200);
      delay(100);
      Serial.print("RFID/NFC @ 13.56 MHz module started");
      delay(1000);
      //!It is needed to launch a simple command to sycnchronize
      getFirmware();
      configureSAM();
    }
    
    
    void loop()
    {
      Serial.print("\n");
      Serial.println("Ready to read...");
      /////////////////////////////////////////////////////////////
      //Get the UID Identifier
      init(_UID, ATQ);
      Serial.print("\n");
      Serial.print( "The UID : ");
      print(_UID , 4);
      /////////////////////////////////////////////////////////////
      //Auntenticate a block with his keyAccess
      state = authenticate(_UID, address, keyAccess);
      Serial.print("\n");
    
    
      if ( state == 0) {
      Serial.println("Authentication block OK");
      } else {
      Serial.println("Authentication failed");
      }
      /////////////////////////////////////////////////////////////
      //Read from address after authentication
      state = readData(address, aux);
      Serial.print("\n");
    
    
      if (state == 0) {
      Serial.println("Read Price ok ");
      } else {
      Serial.println("Read failed");
      }
    
    
      Serial.print("\n");
      Serial.print("Price : ");
      print(aux , 16);
      Serial.print("\n");
      delay(2000);
    }
    
    
    // Total price 
    {
      Total price = Total price + dataTX[0]; 
      Serial.println(analogRead(2)); 
      delay(1000);
    }
    
    
    //**********************************************************************
    //!The goal of this command is to detect as many targets (maximum MaxTg)
    // as possible in passive mode.
    uint8_t init(uint8_t *UID , uint8_t *ATQ)   //! Request InListPassive
    {
      Serial.flush();
    
    
      dataTX[0] = 0x04; // Length
      lengthCheckSum(dataTX); // Length Checksum
      dataTX[2] = 0xD4;
      dataTX[3] = 0x4A; // Code
      dataTX[4] = 0x01; //MaxTarget
      dataTX[5] = 0x00; //BaudRate = 106Kbps
      dataTX[6] = 0x00; // Clear checkSum position
      checkSum(dataTX); 
    
    
      sendTX(dataTX , 7 ,23);
    
    
      for (int i = 17; i < (21) ; i++){
      _UID[i-17] = dataRX[i];
      UID[i-17] = _UID[i-17];
      }
    
    
      ATQ[0] = dataRX[13];
      ATQ[1] = dataRX[14];
    
    
      if ((dataRX[9]== 0xD5) & (dataRX[10] == 0x4B) & (dataRX[11] == 0x01)) {
      return 0;
      } else {
      return 1;
      }
    }
    //**********************************************************************
    //!A block must be authenticated before read and write operations
    uint8_t authenticate(uint8_t *UID, uint8_t blockAddress, uint8_t *keyAccess)
    {
      dataTX[0] = 0x0F;
      lengthCheckSum(dataTX);
      dataTX[2] = 0xD4;
      dataTX[3] = 0x40; // inDataEchange
      dataTX[4] = 0x01; //Number of targets
      dataTX[5] = 0x60; // Authentication code
      dataTX[6] = blockAddress;
      for (int i = 0; i < 6 ; i++) {
      dataTX[i + 7] = keyAccess[i];
      }
      dataTX[13] = UID[0];  dataTX[14] = UID[1];
      dataTX[15] = UID[2];  dataTX[16] = UID[3];
      dataTX[17] = 0x00;
      checkSum(dataTX);
      sendTX(dataTX , 18 ,14);
    
    
      if ((dataRX[9]== 0xD5) & (dataRX[10] == 0x41) & (dataRX[11] == 0x00)) {
      return 0;
      } else {
      return 1;
      }
    }
    //**********************************************************************
    //!Write 16 bytes in address .
    uint8_t writeData(uint8_t address, uint8_t *blockData)  //!Writing
    {
      Serial.print("                ");
      dataTX[0] = 0x15;
      lengthCheckSum(dataTX); // Length Checksum
      dataTX[2] = 0xD4;
      dataTX[3] = 0x40;//inDataEchange CODE
      dataTX[4] = 0x01;//Number of targets
      dataTX[5] = 0xA0;//Write Command
      dataTX[6] = address; //Address
    
    
      for (int i = 0; i < 16; i++) {
      dataTX[i+7] = blockData[i];
      }
    
    
      dataTX[23] = 0x00;
      checkSum(dataTX);
      sendTX(dataTX , 24 ,14);
    
    
      if ((dataRX[9]== 0xD5) & (dataRX[10] == 0x41) & (dataRX[11] == 0x00)) {
      return 0;
      } else {
      return 1;
      }
    }
    //**********************************************************************
    //!Read 16 bytes from  address .
    uint8_t readData(uint8_t address, uint8_t *readData) //!Reading
    {
      Serial.print("                ");
    
    
      dataTX[0] = 0x05;
      lengthCheckSum(dataTX); // Length Checksum
      dataTX[2] = 0xD4; // Code
      dataTX[3] = 0x40; // Code
      dataTX[4] = 0x01; // Number of targets
      dataTX[5] = 0x30; //ReadCode
      dataTX[6] = address;  //Read address
      dataTX[7] = 0x00;
      checkSum(dataTX);
      sendTX(dataTX , 8, 30);
      memset(readData, 0x00, 16);  
    
    
      if ((dataRX[9]== 0xD5) & (dataRX[10] == 0x41) & (dataRX[11] == 0x00)) {
      for (int i = 12; i < 28; i++) {
      readData[i-12] = dataRX[i];
      }
      return 0;
      } else {
      return 1;
      }
    }
    //**********************************************************************
    //!The PN532 sends back the version of the embedded firmware.
    bool getFirmware(void)  //! It is needed to launch a simple command to sycnchronize
    {
      Serial.print("                ");
    
    
      memset(dataTX, 0x00, 35);
      dataTX[0] = 0x02; // Length
      lengthCheckSum(dataTX); // Length Checksum
      dataTX[2] = 0xD4; // CODE
      dataTX[3] = 0x02; //TFI
      checkSum(dataTX); //0x2A; //Checksum   
    
    
      sendTX(dataTX , 5 , 17);
      Serial.print("\n");
      Serial.print("Your Firmware version is : ");
    
    
      for (int i = 11; i < (15) ; i++){
      Serial.print(dataRX[i], HEX);
      Serial.print(" ");
      }
      Serial.print("\n");
    }
    
    
    //**********************************************************************
    //!Print data stored in vectors .
    void print(uint8_t * _data, uint8_t length)
    {
      for (int i = 0; i < length ; i++){
      Serial.print(_data[i], HEX);
      Serial.print(" ");
      }
      Serial.print("\n");
    }
    //**********************************************************************
    //!This command is used to set internal parameters of the PN532,
    bool configureSAM(void)//! Configure the SAM
    {
      Serial.print("               ");
    
    
      dataTX[0] = 0x05; //Length
      lengthCheckSum(dataTX); // Length Checksum
      dataTX[2] = 0xD4;
      dataTX[3] = 0x14;
      dataTX[4] = 0x01; // Normal mode
      dataTX[5] = 0x14; // TimeOUT
      dataTX[6] = 0x00; // IRQ
      dataTX[7] = 0x00; // Clean checkSum position
      checkSum(dataTX);
    
    
      sendTX(dataTX , 8, 13);
    }
    //**********************************************************************
    //!Send data stored in dataTX
    void sendTX(uint8_t *dataTX, uint8_t length, uint8_t outLength)
    {
      Serial.print(char(0x00));
      Serial.print(char(0x00));
      Serial.print(char(0xFF)); 
    
    
      for (int i = 0; i < length; i++) {
      Serial.print(char(dataTX[i]));
      }
    
    
      Serial.print(char(0x00));
      getACK();
      waitResponse();// Receive response
      getData(outLength);
    }
    //**********************************************************************
    //!Wait for ACK response and stores it in the dataRX buffer
    void getACK(void)
    {
      delay(5);
      waitResponse();
      for (int i = 0; i < 5 ; i++) {
      dataRX[i] = Serial.read();
      }
    }
    //**********************************************************************
    //!Wait the response of the module
    void waitResponse(void)
    {
      int val = 0xFF;
      int cont = 0x00;
      while(val != 0x00) { //Wait for 0x00 response
      val = Serial.read();
      delay(5);
      cont ++;
      }
    }
    //**********************************************************************
    //!Get data from the module
    void getData(uint8_t outLength)
    {
      for (int i=5; i < outLength; i++) {
      dataRX[i] = Serial.read();//read data from the module.
      }
    }
    //**********************************************************************
    //!Calculates the checksum and stores it in dataTX buffer
    void checkSum(uint8_t *dataTX)
    {
      for (int i = 0; i < dataTX[0] ; i++) {
      dataTX[dataTX[0] + 2] += dataTX[i + 2];
      }
      byte(dataTX[dataTX[0] + 2]= - dataTX[dataTX[0] + 2]);
    }
    //**********************************************************************
    //!Calculates the length checksum and sotres it in the buffer.
    uint8_t lengthCheckSum(uint8_t *dataTX)
    {
      dataTX[1] = byte(0x100 - dataTX[0]);
    }

     

     

    I've suggested downloading this book in the past, and I will do it again.

    http://it-ebooks.info/book/1982/

     

    I have a lot to do so hopefully with the book and th hints you can proceed.

    Mark

    • Cancel
    • Vote Up +1 Vote Down
    • Sign in to reply
    • Cancel
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