PN532 can detect card when using the example code from library but can't when i code it on my own

Here is my code

uint8_t success;
    uint8_t uid[] = { 0, 0, 0, 0, 0, 0, 0 };
    uint8_t uidLength;
    success = nfc.readPassiveTargetID(PN532_MIFARE_ISO14443A, uid, uidLength);

    do {
      uint8_t success;
      uint8_t uid[] = { 0, 0, 0, 0, 0, 0, 0 };
      uint8_t uidLength;
      success = nfc.readPassiveTargetID(PN532_MIFARE_ISO14443A, uid, &uidLength);
      if (success) {
        delay(1000);
        lcd.clear();
        lcd.setCursor(0, 0);
        lcd.print("Kartu terdeteksi");
        delay(5000);
        nfc.PrintHex(uid, uidLength);

        uint32_t szUid;
        String uidData;
        for (szUid=0; szUid < uidLength; szUid++){
            uidData += String(uid[szUid]&0xFF, HEX);
        }
        uidData.toUpperCase(); 
        UID_CARD =uidData;
    
        if (uidLength == 4){  
          uint8_t keya[6] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
      
          success = nfc.mifareclassic_AuthenticateBlock(uid, uidLength, 4, 0, keya);
    
          if (success) {
            Serial.println("Blocks 4) has been authenticated");
            uint8_t data[16];
            success = nfc.mifareclassic_ReadDataBlock(4, data);
    
            if (success){
              nfc.PrintHexChar(data, 16);

              uint32_t szPos;
              String phrase;
              for (szPos=0; szPos < 16; szPos++){
                  phrase += (char)data[szPos];
              }
            lcd.clear();
            lcd.setCursor(0, 0);
            lcd.print("User: ");
            lcd.setCursor(0,1);
            lcd.print(phrase);
            nama_user=phrase;
            delay(5000);
          }
        }

        success = nfc.mifareclassic_AuthenticateBlock(uid, uidLength, 5, 0, keya);
        if (success){
          uint8_t data1[16];

          byte readBlockData[18];
          nfc.mifareclassic_ReadDataBlock(5, readBlockData);
          memcpy(&poin_skrg, readBlockData, sizeof poin_skrg);
    
          if (success) {
            lcd.clear();
            for (int i= berat; i >= 10; i - 10) {
              poin_skrg += 1;
            }

            lcd.setCursor(0,0);
            lcd.print("Total Poin: ");
            lcd.setCursor(0,1);
            lcd.print(poin_skrg);
            digitalWrite(ledPin, HIGH);
            nfc.mifareclassic_WriteDataBlock(5, (byte*) &poin_skrg);
            delay(200);
            digitalWrite(ledPin, LOW);
            SendtoDB();
            }
          }
        }
        success = nfc.readPassiveTargetID(PN532_MIFARE_ISO14443A, uid, uidLength);
      }
      } while (!success) ;

Please post your full sketch

#include "PN532.h"
#include <PN532_SWHSU.h>
#include <SoftwareSerial.h>
#include <Wire.h>
#include <SoftwareSerial.h>
#include <LiquidCrystal_I2C.h>
#include <Servo.h>
#include <Ethernet.h>
#include <EEPROM.h>
#include <HX711_ADC.h>

LiquidCrystal_I2C lcd(0x27, 20, 4);
Servo myservo; 
SoftwareSerial SWSerial( 10, 11 ); // RX, TX
const int HX711_dout = 4;
const int HX711_sck = 5;
const int buttonPin = 2;
const int ledPin = LED_BUILTIN;
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
char server[] = "192.168.10.10";
IPAddress ip(192,168,10,5);
EthernetClient client; 
String UID_CARD;
String nama_user;

uint8_t buttonState = 0;
float berat = 0;
int pos = 0;
uint8_t poin_skrg = 0;
PN532_SWHSU pn532swhsu( SWSerial );
PN532 nfc( pn532swhsu );
HX711_ADC LoadCell(HX711_dout, HX711_sck);

const int calVal_eepromAdress = 0;
unsigned long t = 0;

void setup() 
{
  //LCD Init
  Serial.begin(115200);
  lcd.init();
  lcd.backlight();
  pinMode(buttonPin, INPUT);
  lcd.setCursor(0,0);
  lcd.print("Halo!");
  lcd.setCursor(0,1);
  lcd.print("Silakan masukkan");
  lcd.setCursor(0,2);
  lcd.print("sampah kelubang");
  lcd.setCursor(0,3);
  lcd.print("dibawah");

  //Load Cell Init
  delay(10);
  LoadCell.begin();
  //LoadCell.setReverseOutput(); //uncomment to turn a negative output value to positive
  uint8_t calibrationValue; // calibration value (see example file "Calibration.ino")
  calibrationValue = 0.0; // uncomment this if you want to set the calibration value in the sketch
  EEPROM.get(calVal_eepromAdress, calibrationValue); // uncomment this if you want to fetch the calibration value from eeprom

  unsigned long stabilizingtime = 2000; // preciscion right after power-up can be improved by adding a few seconds of stabilizing time
  boolean _tare = true; //set this to false if you don't want tare to be performed in the next step
  LoadCell.start(stabilizingtime, _tare);
  LoadCell.setCalFactor(calibrationValue); // set calibration value (float)
  Serial.println("Startup is complete");
  
  //NFC Init
  nfc.begin();
  uint32_t versiondata = nfc.getFirmwareVersion();
  
  nfc.SAMConfig();
  Ethernet.begin(mac, ip);

  //Servo Init
  myservo.attach(9);
}

void loop() 
{
  buttonState = digitalRead(buttonPin); 
  if (buttonState == HIGH)
  {
    delay(1000);
    //Load Cell Code
    static boolean newDataReady = 0;
    const int serialPrintInterval = 0;

    if (LoadCell.update()) newDataReady = true;

    if (newDataReady) {
      if (millis() > t + serialPrintInterval) {
        float berat = LoadCell.getData();
        lcd.clear();
        lcd.setCursor(0, 0);
        lcd.print("Berat: ...");
        t = millis();
      }
    }

    delay(5000);

    if (LoadCell.update()) newDataReady = true;

    if (newDataReady) {
      if (millis() > t + serialPrintInterval) {
        float berat = LoadCell.getData();
        lcd.clear();
        lcd.setCursor(0, 0);
        lcd.print("Berat: ");
        lcd.println(berat);
        lcd.setCursor(20, 0);
        lcd.print("g");
        newDataReady = 0;
        t = millis();
      }
    }

    // receive command from serial terminal, send 't' to initiate tare operation:
    if (Serial.available() > 0) {
      char inByte = Serial.read();
      if (inByte == 't') LoadCell.tareNoDelay();
    }

    // check if last tare operation is complete:
    if (LoadCell.getTareStatus() == true) {
      Serial.println("Tare complete");
    }

    
    delay(5000);
    lcd.clear();
    lcd.setCursor(0, 0);
    lcd.print("Proccessing");
    //Servo Code
    for (pos = 0; pos <= 90; pos += 1) {
      myservo.write(pos);
      delay(20);
    }
    for (pos = 0; pos >= 90; pos -= 1) {
      myservo.write(pos);
      delay(20);
    }

    delay(1000);
    //NFC Code
    lcd.clear();
    lcd.setCursor(0,0);
    lcd.print("Tempelkan kartu");
    lcd.setCursor(0,1);
    lcd.print("dibagian kanan");
    delay(3000);
    uint8_t success;
    uint8_t uid[] = { 0, 0, 0, 0, 0, 0, 0 };
    uint8_t uidLength;
    success = nfc.readPassiveTargetID(PN532_MIFARE_ISO14443A, uid, uidLength);

    do {
      uint8_t success;
      uint8_t uid[] = { 0, 0, 0, 0, 0, 0, 0 };
      uint8_t uidLength;
      success = nfc.readPassiveTargetID(PN532_MIFARE_ISO14443A, uid, &uidLength);
      if (success) {
        delay(1000);
        lcd.clear();
        lcd.setCursor(0, 0);
        lcd.print("Kartu terdeteksi");
        delay(5000);
        nfc.PrintHex(uid, uidLength);

        uint32_t szUid;
        String uidData;
        for (szUid=0; szUid < uidLength; szUid++){
            uidData += String(uid[szUid]&0xFF, HEX);
        }
        uidData.toUpperCase();
        UID_CARD =uidData;

        if (uidLength == 4){  
          uint8_t keya[6] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };

          success = nfc.mifareclassic_AuthenticateBlock(uid, uidLength, 4, 0, keya);
    
          if (success) {
            Serial.println("Blocks 4) has been authenticated");
            uint8_t data[16];
            success = nfc.mifareclassic_ReadDataBlock(4, data);
    
            if (success){
              nfc.PrintHexChar(data, 16);

              uint32_t szPos;
              String phrase;
              for (szPos=0; szPos < 16; szPos++){
                  phrase += (char)data[szPos];
              }
            lcd.clear();
            lcd.setCursor(0, 0);
            lcd.print("User: ");
            lcd.setCursor(0,1);
            lcd.print(phrase);
            nama_user=phrase;
            delay(5000);
          }
        }

        success = nfc.mifareclassic_AuthenticateBlock(uid, uidLength, 5, 0, keya);
        if (success){
          uint8_t data1[16];

          byte readBlockData[18];
          nfc.mifareclassic_ReadDataBlock(5, readBlockData);
          memcpy(&poin_skrg, readBlockData, sizeof poin_skrg);

          if (success) {
            lcd.clear();
            for (int i= berat; i >= 10; i - 10) {
              poin_skrg += 1;
            }

            lcd.setCursor(0,0);
            lcd.print("Total Poin: ");
            lcd.setCursor(0,1);
            lcd.print(poin_skrg);
            digitalWrite(ledPin, HIGH);
            nfc.mifareclassic_WriteDataBlock(5, (byte*) &poin_skrg);
            delay(200);
            digitalWrite(ledPin, LOW);
            SendtoDB();
            }
          }
        }
        success = nfc.readPassiveTargetID(PN532_MIFARE_ISO14443A, uid, uidLength);
      }
      } while (!success) ;
  }
  else {
    delay(1000);
    lcd.clear();
    lcd.setCursor(0,0);
    lcd.print("Halo!");
    lcd.setCursor(0,1);
    lcd.print("Silakan masukkan");
    lcd.setCursor(0,2);
    lcd.print("sampah kelubang");
    lcd.setCursor(0,3);
    lcd.print("dibawah");
  }
}

void SendtoDB(){
   if (client.connect(server, 80)) {

    // Make a HTTP request:
    Serial.print("GET /Poin_RVM/injector.php?uid=");
    Serial.print(UID_CARD);
    Serial.print("Poin=");
    Serial.println(poin_skrg);
    Serial.println("");
    
    client.print("GET /Poin_RVM/injector.php?uid=");     //YOUR URL
    client.print(UID_CARD);
    client.print("&poin=");
    client.print(poin_skrg);
    client.print(" ");
    client.print("HTTP/1.1");
    client.println();
    client.println("Host: 192.168.10.5");
    client.println("Connection: close");
    client.println();
  }
}

Thank you

…and your hardware.
I don’t have your gear right here, or an annotated schematic will help.


I thought the photo was too bad so I made this

It looks like you missed the comment about the Arduino is NOT a power supply for the rest of your project.

I tried to delete the codes one by one and I found when I delete the Ethernet code it's working now. But I still don't know why. Can you try to explain it to me?

the Ethernet shield is very likely to use 10 as CS and 11 is part of the SPI interface.

Okay I will try to change the PN532 pin use later and see if it works

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