Tmrpcm MFRC522 incompatability - cannot save .wav file and scanning NFC Tag

Hi Guys, I am currently working on a project where the following is my goal:

It should be able to record sth with a mic, save as .wav to a SD card and then saving the information (folder number and title number) on a mifare classic tag to make the tag. Everything by its own has worked so far and has been tested, the recording and (afterwards) scanning of the tag is giving me a great headache.

The problem is that the .wav files become corrupted (0 Bytes) when "using" the scanNFCTag() method (see bold in code). Using here does not mean that I enter the method, even if i do not scan a card, the file gets corrupted, whereas if I comment the method out (or delete the line) the file is saved correctly.

I am using a RFID-RC522 and SD Card Adapter HW 125. I am native Java programmer, so I have some difficulties with C++ but code-wise I do not see any problem.

Thanks in advance for any help! Thanks a lot!

#include <Wire.h>
#include <SPI.h>
#include <MFRC522.h>
#include <SD.h>
#include <TMRpcm.h>
#define SS_PIN 10
#define RST_PIN 9
#include "Arduino.h"



/* Create an instance of MFRC522 */
MFRC522 mfrc522(SS_PIN, RST_PIN);
/* Create an instance of MIFARE_Key */
MFRC522::MIFARE_Key key;

/* Set the block to which we want to write data */
/* Be aware of Sector Trailer Blocks */
int blockNum = 2;
/* Create an array of 16 Bytes and fill it with data */
/* This is the actual data which is going to be written into the card */
byte blockData [16] = {"Folder1Title1"};

/* Create another array to read data from Block */
/* Legthn of buffer should be 2 Bytes more than the size of Block (16 Bytes) */
byte bufferLen = 18;
byte readBlockData[18];
char whatWasRead[18];
int folderNumber, titleNumber;

const int folderPosition = 6;
const int titlePosition = 12;

int currentTime = 0;
int previousTime = 0;
const int period = 1000;


MFRC522::StatusCode status;

int LED = 3;
int TASTER = 2;
int tasterstatus = 0;

bool executedOnce = false;

File myFile;
String fileName = "01/001.wav";
bool startedRecording = false;

bool readyToScan = false;

TMRpcm audio;

void setup() {
  digitalWrite(10, HIGH);
  digitalWrite(5, HIGH);
  mfrc522.PCD_Init();

  Serial.begin(9600);

  Serial.print("Initializing SD card...");

  if (!SD.begin(5)) {
    Serial.println("initialization failed!");
    while (1);
  }
  Serial.println("initialization done.");

  if (!SD.exists("/01")) {
    SD.mkdir("01");
  }

}

void loop() {


  /* Prepare the ksy for authentication */
  /* All keys are set to FFFFFFFFFFFFh at chip delivery from the factory */
  for (byte i = 0; i < 6; i++)
  {
    key.keyByte[i] = 0xFF;
  }



  tasterstatus = digitalRead(TASTER);

  //record here
  if (tasterstatus == HIGH) {

    if (!startedRecording) {
      digitalWrite(10, HIGH);
      digitalWrite(5, LOW);
      Serial.println(F("Starting to record"));
      // with bitrate of 4000 the cracklign in the BG is takable
      audio.startRecording("01/001.wav", 4000, A1);
      startedRecording = true;
      executedOnce = false;
    }



    // put LED on
    digitalWrite(LED, HIGH);

  } else {
    digitalWrite(LED, LOW);
    if (startedRecording && !executedOnce) {
      //Serial.print(startedRecording);
      Serial.println(F("stop to record"));
      audio.stopRecording("01/001.wav");
      delay(2000);
      startedRecording = false;

      // scan card here
      executedOnce = true;
      readyToScan = true;
    }

    if (readyToScan) {
      Serial.println(F("scan now!"));

      int Time = millis();
      digitalWrite(10, LOW);
      digitalWrite(5, HIGH);
      while (millis() < Time + 7000) {
        if (!mfrc522.PICC_IsNewCardPresent() || ! mfrc522.PICC_ReadCardSerial()) {
          Serial.print(F("."));
        } else {
          Serial.println(F(""));
          Serial.println(F("Found Card!"));

          **scanNFCTag();**
          delay(1000);
          break;
        }
        delay(100);
      }
      Serial.println(" ");
      Serial.println("No Scanning anymore");
      // Halt PICC
      mfrc522.PICC_HaltA();
      // Stop encryption on PCD
      mfrc522.PCD_StopCrypto1();
      readyToScan = false;
    }
  }
}



void scanNFCTag() {
  for (byte i = 0; i < mfrc522.uid.size; i++) {
    Serial.print(mfrc522.uid.uidByte[i], HEX);
    Serial.print(" ");
  }

  Serial.println();

  Serial.print("\n");
  Serial.println(F("Writing to Data Block..."));
  WriteDataToBlock(blockNum, blockData);



  Serial.print("\n");
  Serial.println(F("Reading from Data Block..."));
  ReadDataFromBlock(blockNum, readBlockData);

  Serial.print("\n");
  Serial.print(F("Data in Block:"));
  Serial.print(blockNum);
  Serial.print(" --> ");
  for (int j = 0 ; j < 16 ; j++)
  {
    Serial.write(readBlockData[j]);
    whatWasRead[j] = (char)readBlockData[j];
  }



  Serial.print("\n");
  Serial.print(F("What was read: "));
  Serial.println(whatWasRead);
  Serial.println();

  folderNumber = whatWasRead[folderPosition] - '0';
  titleNumber = whatWasRead[titlePosition] - '0';

  Serial.print(F("folder number: "));
  Serial.println(folderNumber);
  Serial.print(F("title number: "));
  Serial.println(titleNumber);

}


void WriteDataToBlock(int blockNum, byte blockData[])
{
  /* Authenticating the desired data block for write access using Key A */
  status = mfrc522.PCD_Authenticate(MFRC522::PICC_CMD_MF_AUTH_KEY_A, blockNum, &key, &(mfrc522.uid));
  if (status != MFRC522::STATUS_OK)
  {
    Serial.print(F("Authentication failed for Write: "));
    Serial.println(mfrc522.GetStatusCodeName(status));
    return;
  }
  else
  {
    Serial.println(F("Authentication success"));
  }


  /* Write data to the block */
  status = mfrc522.MIFARE_Write(blockNum, blockData, 16);
  if (status != MFRC522::STATUS_OK)
  {
    Serial.print(F("Writing to Block failed: "));
    Serial.println(mfrc522.GetStatusCodeName(status));
    return;
  }
  else
  {
    Serial.println(F("Data was written into Block successfully"));
  }

}

void ReadDataFromBlock(int blockNum, byte readBlockData[])
{
  /* Authenticating the desired data block for Read access using Key A */
  byte status = mfrc522.PCD_Authenticate(MFRC522::PICC_CMD_MF_AUTH_KEY_A, blockNum, &key, &(mfrc522.uid));

  if (status != MFRC522::STATUS_OK)
  {
    Serial.print(F("Authentication failed for Read: "));
    Serial.println(mfrc522.GetStatusCodeName(status));
    return;
  }
  else
  {
    Serial.println(F("Authentication success"));
  }

  /* Reading data from the Block */
  status = mfrc522.MIFARE_Read(blockNum, readBlockData, &bufferLen);
  if (status != MFRC522::STATUS_OK)
  {
    Serial.print("Reading failed: ");
    Serial.println(mfrc522.GetStatusCodeName(status));
    return;
  }
  else
  {
    Serial.println(F("Block was read successfully"));
  }
}
```**fett gedruckter Text**

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