Button not reading state

#include <Wire.h>
#include <SPI.h>
#include <Adafruit_PN532.h>

// If using the breakout with SPI, define the pins for SPI communication.
#define PN532_SCK  (2)
#define PN532_MOSI (3)
#define PN532_SS   (4)
#define PN532_MISO (5)

// If using the breakout or shield with I2C, define just the pins connected
// to the IRQ and reset lines.  Use the values below (2, 3) for the shield!
#define PN532_IRQ   (2)
#define PN532_RESET (3)  // Not connected by default on the NFC Shield

// Uncomment just _one_ line below depending on how your breakout or shield
// is connected to the Arduino:

// Use this line for a breakout with a software SPI connection (recommended):
//Adafruit_PN532 nfc(PN532_SCK, PN532_MISO, PN532_MOSI, PN532_SS);

// Use this line for a breakout with a hardware SPI connection.  Note that
// the PN532 SCK, MOSI, and MISO pins need to be connected to the Arduino's
// hardware SPI SCK, MOSI, and MISO pins.  On an Arduino Uno these are
// SCK = 13, MOSI = 11, MISO = 12.  The SS line can be any digital IO pin.
//Adafruit_PN532 nfc(PN532_SS);

// Or use this line for a breakout or shield with an I2C connection:
Adafruit_PN532 nfc(PN532_IRQ, PN532_RESET);
const int buttonPin = 3;
int buttonState = 0;

void setup(void) {
  pinMode(buttonPin, INPUT);
  while (!Serial) delay(10); // for Leonardo/Micro/Zero



  uint32_t versiondata = nfc.getFirmwareVersion();
  if (! versiondata) {
    Serial.print("Didn't find PN53x board");
    while (1); // halt
  // Got ok data, print it out!
  Serial.print("Found chip PN5"); Serial.println((versiondata>>24) & 0xFF, HEX); 
  Serial.print("Firmware ver. "); Serial.print((versiondata>>16) & 0xFF, DEC); 
  Serial.print('.'); Serial.println((versiondata>>8) & 0xFF, DEC);
  // configure board to read RFID tags
  attachInterrupt(digitalPinToInterrupt(buttonPin), buttonDown, CHANGE);
  Serial.println("Waiting for an ISO14443A Card ...");

void loop(void) {
  buttonState = digitalRead(buttonPin);
  uint8_t success;
  uint8_t uid[] = { 0, 0, 0, 0, 0, 0, 0 };  // Buffer to store the returned UID
  uint8_t uidLength;                        // Length of the UID (4 or 7 bytes depending on ISO14443A card type)
  // Wait for an ISO14443A type cards (Mifare, etc.).  When one is found
  // 'uid' will be populated with the UID, and uidLength will indicate
  // if the uid is 4 bytes (Mifare Classic) or 7 bytes (Mifare Ultralight)

  while (digitalRead(buttonPin) == HIGH){
  success = nfc.readPassiveTargetID(PN532_MIFARE_ISO14443A, uid, &uidLength, 6000);
  if (success) {
    // Display some basic information about the card
    Serial.println("Found an ISO14443A card");
    Serial.print("  UID Length: ");Serial.print(uidLength, DEC);Serial.println(" bytes");
    Serial.print("  UID Value: ");
    nfc.PrintHex(uid, uidLength);
    if (uidLength == 4)
      // We probably have a Mifare Classic card ... 
      Serial.println("Seems to be a Mifare Classic card (4 byte UID)");
      // Now we need to try to authenticate it for read/write access
      // Try with the factory default KeyA: 0xFF 0xFF 0xFF 0xFF 0xFF 0xFF
      Serial.println("Trying to authenticate block 4 with default KEYA value");
      uint8_t keya[6] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
	  // Start with block 4 (the first block of sector 1) since sector 0
	  // contains the manufacturer data and it's probably better just
	  // to leave it alone unless you know what you're doing
      success = nfc.mifareclassic_AuthenticateBlock(uid, uidLength, 4, 0, keya);
      if (success)
        Serial.println("Sector 1 (Blocks 4..7) has been authenticated");
        uint8_t data[16];
        // If you want to write something to block 4 to test with, uncomment
		// the following line and this text should be read back in a minute
        //memcpy(data, (const uint8_t[]){ 'a', 'd', 'a', 'f', 'r', 'u', 'i', 't', '.', 'c', 'o', 'm', 0, 0, 0, 0 }, sizeof data);
        // success = nfc.mifareclassic_WriteDataBlock (4, data);

        // Try to read the contents of block 4
        success = nfc.mifareclassic_ReadDataBlock(4, data);
        if (success)
          // Data seems to have been read ... spit it out
          Serial.println("Reading Block 4:");
          nfc.PrintHexChar(data, 16);
          // Wait a bit before reading the card again
          Serial.println("Ooops ... unable to read the requested block.  Try another key?");
        Serial.println("Ooops ... authentication failed: Try another key?");
    if (uidLength == 7)
      // We probably have a Mifare Ultralight card ...
      Serial.println("Seems to be a Mifare Ultralight tag (7 byte UID)");
      // Try to read the first general-purpose user page (#4)
      Serial.println("Reading page 4");
      uint8_t data[32];
      success = nfc.mifareultralight_ReadPage (4, data);
      if (success)
        // Data seems to have been read ... spit it out
        nfc.PrintHexChar(data, 4);
        // Wait a bit before reading the card again
        Serial.println("Ooops ... unable to read the requested page!?");
while (digitalRead(buttonPin)== LOW){
  Serial.println("Button Up");
  buttonState = digitalRead(buttonPin);

The aim of this code is to only read RFID tags when a push button is pressed. When I press the button, it reads the right state and when I release it, it also displays the right state. However, when I press it again, it remains in a locked state and doesn't change.

The button has a wire going from 5V, a wire going to pin 3 and a 10k ohm resistor going to ground.

A lot of this code is example code from the P532 library.

Hello tufty21
I havĀ“nt check you sketch in detail. You shall take a view here to get some ideas.

A while() conditional query in a button task do not always necessarily get along.
Have a nice day and enjoy coding in C++.

Because of the delay(1000) in the while() at the bottom, you need to hold the button down for a second so it can escape from there. If you just give the button a normal "blip" during that second, it won't get read. You need to be holding the button down when the delay ends.

BTW, the reading of the button into the buttonstate variable seems a waste of time, since you don't use the variable to decide on which while() to use, and you read it again anyway.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.