Hi all,
I have a big issue here, my program is exceeding the program space on my Arduino Uno V3 by a mere percent >:( it uses up 101% and I have no good idea how to reduce it.
My sketch is attached, so if someone likes to take a look and give me some sound advise, I'd really appreciate it.
To the program itself: It shall read in an NFC tag, get a directory from it and then start playing the corresponding album off the SD card inserted in the Adafruit Music Maker Shield.
So basically I have 3 components:
- Arduino Uno V3
- Adafruit Music Maker Shield
- SunFounder PN532 NFC Module
At the top I then of course include the following libraries:
// include the string header so we can use strcpy() and strcat()
#include <string.h>
// setup SPI, MP3 and SD libraries
#include <SPI.h>
#include <Adafruit_VS1053.h>
#include <SD.h>
// These are the pins used for the music maker shield
#define SHIELD_RESET -1 // VS1053 reset pin (unused!)
#define SHIELD_CS 7 // VS1053 chip select pin (output)
#define SHIELD_DCS 6 // VS1053 Data/command select pin (output)
#define CLK 13 // SPI Clock, shared with SD card
#define MISO 12 // Input data, from VS1053/SD card
#define MOSI 11 // Output data, to VS1053/SD card
// These are common pins between breakout and shield
#define CARDCS 4 // Card chip select pin
// DREQ should be an Int pin, see attachInterrupt() - Arduino Reference
#define DREQ 3 // VS1053 Data request, ideally an Interrupt pin
Adafruit_VS1053_FilePlayer musicPlayer = Adafruit_VS1053_FilePlayer(SHIELD_RESET, SHIELD_CS, SHIELD_DCS, DREQ, CARDCS);
// setup NFC Adapter
#include <PN532_SPI.h>
#include <PN532.h>
#include <NfcAdapter.h>
#define PN532_SCK (13) //changed from pin 2
#define PN532_MISO (12) //changed from pin 5
#define PN532_MOSI (11) //changed from pin 3
#define PN532_SS (10) //changed from 4
PN532_SPI pn532spi(SPI, PN532_SS);
NfcAdapter nfc = NfcAdapter(pn532spi);
for the rest of the sketch, please see the attached ino.
The topic I'd like to solve first, is how to get rid of the String-class usage in my function that reads in the NFC tag content:
boolean getNfcCardData() {
boolean usableContent = false; // in case the tag has everything we need to play an album - in essence there must be a message with the directory - we return this as true
NfcTag tag = nfc.read();
if (DEBUG) { Serial.print(F("Tag Type: "));Serial.println(tag.getTagType()); Serial.print(F("UID: "));Serial.println(tag.getUidString()); }
if (tag.hasNdefMessage()){
NdefMessage message = tag.getNdefMessage();
// If you have more than 1 Message then it wil cycle through them
int recordCount = message.getRecordCount();
for (int i = 0; i < recordCount; i++) {
NdefRecord record = message.getRecord(i);
int payloadLength = record.getPayloadLength();
byte payload[payloadLength];
record.getPayload(payload);
String payloadAsString = ""; // Processes the message as a string vs as a HEX value
for (int c = 0; c < payloadLength; c++) {
payloadAsString += (char)payload
;
}
String cleanString = payloadAsString;
cleanString.remove(0,3);
if (DEBUG) { Serial.print(F("content element ")); Serial.print(i); Serial.print(F(": ")); Serial.println(cleanString); }
if (cleanString.charAt(spacePosition - 1) == 'D') {
usableContent = true;
String contentString = cleanString.substring(3);
contentString.toCharArray(plrCurrentFolder, 9);
if (DEBUG) { Serial.print(F("Directory: "));Serial.println(contentString); }
usableContent = true;
}
if (cleanString.charAt(spacePosition - 1) == 'T') {
String contentString = cleanString.substring(3);
firstTrackToPlay = contentString.toInt();
if (DEBUG) { Serial.print(F("Track: "));Serial.println(contentString); }
}
String uid = record.getId();
if (uid != "") { if (DEBUG) { Serial.print(F(" ID: "));Serial.println(uid); } }
}
} else { // does the card have NDEF messages?
if (DEBUG) { Serial.println(F("THIS CARD DOES NOT HAVE MESSAGES AT ALL - NOT USABLE FOR THE MP3 PLAYER"));}
usableContent = false;
}
return (usableContent);
}
Any other tips and hints are also very much appreciated,
Chris
[main2ndTry.ino|attachment](upload://4w8x09Z7P65SHvSnXINfXim4Ylz.ino) (27.2 KB)