Hi Everyone,
I am new to programming and Arduino. Working on a school project right now, so have been trying things out one step at a time.
The interaction of the project is:
- scan an NFC tag
- touch a touchpad on MPR121 capacitive sensor (doesn't matter which touchpad it is. There are 12 in total)
- When I touch one pad for the first time, it starts recording
- When it is the second time, touch on the same pad, stop recording
- When the third time, start playing the recording.
So for each recording, I want the name of the file to be "NFC tag UID + number of the pad", i.e. 4D2e5C7h1, 4D2e5C7h6, 4D2e5C7h3, 4D2e5C7h12 and so forth. (the order of the touchpad doesn't matter.)
I have been struggling to get this right. I used the Serial.println to debug. The name is reflected correctly but it is not recording.
So here is my code: (The codes here are just to test the name + recording. I haven't integrated with the capacitive sensor part yet. )
#include <SPI.h> // RC522 Module uses SPI protocol
#include <MFRC522.h> // Library for Mifare RC522 Devices
#include <SD.h>
#include <TMRpcm.h>
#define RST_PIN 9 // Configurable, see typical pin layout above
#define SS_PIN 8 // Configurable, see typical pin layout above
MFRC522 mfrc522(SS_PIN, RST_PIN); // Create MFRC522 instance
MFRC522::MIFARE_Key key;
#define sdPin 10 // Set SD Pin
TMRpcm audio;
byte readCard[4]; // Stores scanned ID read from RFID Module
int pad[]={1,2,3,4,5,6,7,8,9,10,11,12};
char filename[] = "XXXXXXXXXX.wav"; // 4 bytes from UID + touchpad number
char extension[] = "wav";
void setup() {
Serial.begin(9600); // Initialize serial communications with the PC
while (!Serial) {
delay(10);
}
SD.begin(sdPin);
if (!SD.begin(sdPin)) { // Initialize SD Hardware
Serial.println(F("SD initialization failed!"));
return;// Could not initialize // Do not go further
}else{
Serial.println(F("SD initialization done.")); }
audio.CSPin = sdPin;
SPI.begin(); // Init SPI bus
mfrc522.PCD_Init(); // Init MFRC522 card
for (byte i = 0; i < 6; i++) {
key.keyByte[i] = 0xFF;
}
Serial.println("Ready to read a card");
}
void loop() {
if ( mfrc522.PICC_IsNewCardPresent() && mfrc522.PICC_ReadCardSerial() ) {
// Now a card is selected. The UID and SAK is in mfrc522.uid.
Serial.print(F("Card UID:"));
for (byte i = 0; i < 4; i++) {
readCard[i] = mfrc522.uid.uidByte[i];
Serial.print(readCard[i], HEX);
}
Serial.println("");
mfrc522.PICC_HaltA();// Stop reading
if(Serial.available()>0){
switch(Serial.read()){
case 'r':
sprintf(filename, "%02x%02x%02x%02x%u.%s", readCard[0], readCard[1], readCard[2], readCard[3], pad[0], extension);
Serial.println(filename);//debug
audio.startRecording(filename, 16000, A0); break;
case 's':
Serial.println("r done");//debug
audio.stopRecording(filename); break;
case 'a':
sprintf(filename, "%02x%02x%02x%02x%u.%s", readCard[0], readCard[1], readCard[2], readCard[3], pad[1], extension);
Serial.println(filename);//debug
audio.startRecording(filename, 16000, A0); break;
case 'b':
Serial.println("a done"); //debug
audio.stopRecording(filename); break;
}
}
}
}
When I just try: case 'r': audio.startRecording("test.wav", 16000, A0); break; , the testing file is recorded.
(So eventually, I would want to scan the NFC tag, search if it exists in the SD card. If it is, then no matter which touchpad is touched, it starts to play the recording associated with that "nfc tag + touchpad".
If it is new, then it starts to record. But this would be for the future. Just want to let you know this is the whole interaction I am trying to build here. )
Could you help to let me know what went wrong and how to fix it? Thank you very much!