Writing a float to a MIfare card via NFC

Hi,

Creating a cashless payment system and looking to store the balance of the card on the card itself (its all theoretical dw about security), and having trouble writing the float to the card.

I assumed you had to write it byte by byte so (I think) I converted it to a byte array using a union, but its not working.

I'm new, and Im having a bit of trouble understanding how the union is working in the "nfc.writeMemoryBlock" function, I saw it used on another forum (although not for this function).

Any help would be great,

Thanks,

Tom :slight_smile:

CODE -

#include <PN532.h>
#include <SPI.h>


//#define NFC_DEMO_DEBUG 1
uint32_t cardID;


PN532 nfc(10);

//create union to convert float to byte

union Balance{
  float balanceFloat;
  char balanceBytes[sizeof(float)];
};



void setup(void){
  Serial.begin(9600);
  Serial.println("Welcome");
  nfc.begin();
  nfc.SAMConfig();
  Serial.println("Place card on reader");
}

//get the cards id

void getcardID(){

  cardID = nfc.readPassiveTargetID(PN532_MIFARE_ISO14443A);
  
    if(cardID != 0){
      Serial.println(" ");
      Serial.println("Read card id");
      Serial.println(cardID);
      Serial.println("Remove card");
      delay(5000);
    }
    else{
        delay(5000);
        }
}

void loop(){
  
  getcardID();
  
  union Balance balance;
  
  balance.balanceFloat = 10.00;

// simulation for entering amount
  Serial.println("Enter Amount");
  Serial.print("Amount entered is "); Serial.println(balance.balanceFloat); //works fine
  
  
  uint8_t keys[]= {
      0xFF,0xFF,0xFF,0xFF,0xFF,0xFF};
    
    //write the byte version of the float to a memory block on the card
    
  nfc.authenticateBlock(1, cardID ,0x08,KEY_A,keys); 
  nfc.writeMemoryBlock(1,0x08,balance.balanceBytes); 

  
  }

The trick is not to use a floating point but to write a long int where the value you write is in the lowest unit of currency, for example cents.
Floating point numbers are not precise and will be subject to errors unlike ints.

Grumpy_Mike:
The trick is not to use a floating point but to write a long int where the value you write is in the lowest unit of currency, for example cents.
Floating point numbers are not precise and will be subject to errors unlike ints.

Sorted, thank-you!