Trying to reduce dynamic memory use

Hi folks,

First, I’m really, really green at this, so thanks in advance for bearing with me. I’m using an Uno.

I’m building a simple RFID based music player for my son, and I’ve run into problems adding more than about 12 songs to my code, as it gives me an error stating that my global variables exceed the dynamic memory limit (2,974 out of 2,048 with the code below). My goal is to have about 25 tracks on here if possible.

Here’s my code:


  this is the firmware for the contact-free jukebox.
  that can play audio tracks depending on rfid tags.

  attached to the arduino is an adafruit waveshield
  and an id-12 rfid reader.

  created @ 8.1.12 by dominik grob (@ruedi)

  more information:

#include <FatReader.h>
#include <SdReader.h>
#include <avr/pgmspace.h>
#include "WaveUtil.h"
#include "WaveHC.h"

#include <SoftwareSerial.h>

//  audio settings
boolean PlayComplete = false;
long playnext_prevmillis = 0;
long playanother_time = 3000;

//  sdcard settings
SdReader card;
FatVolume vol;
FatReader root;
FatReader f;

WaveHC wave;

//  rfid settings
SoftwareSerial RFIDSerial(8, 6);

int RFIDResetPin = 7;
char RFIDtag[14];
int RFIDindex = 0;
boolean RFIDreading = false;

boolean debug = true;

//  define tag id and tracks
#define NUMTAGS 27
char audiotags[NUMTAGS][27] = {"0000EE1E6E9E",
// make sure soundfile names are not longer then 8 chars (without filetype)
char audiofiles[NUMTAGS][27] = {"HELLO.wav",

void setup() {

  putstring("Free RAM: ");

  //  set output pins for DAC control
  //  pins are defined in the library
  pinMode(2, OUTPUT);
  pinMode(3, OUTPUT);
  pinMode(4, OUTPUT);
  pinMode(5, OUTPUT);

  // pin13 LED
  pinMode(13, OUTPUT);

  if (!card.init()) {
    putstring_nl("Card init. failed!");

  // enable optimize read

  // fat partition?
  uint8_t part;
  for (part = 0; part < 5; part++) {
    if (vol.init(card, part))
  if (part == 5) {
    putstring_nl("No valid FAT partition!");

  // show infos
  putstring("Using partition ");
  Serial.print(part, DEC);
  putstring(", type is FAT");

  if (!root.openRoot(vol)) {
    putstring_nl("Can't open root dir!");

  putstring_nl("> sdcard ready");

  // rfid setup
  pinMode(RFIDResetPin, OUTPUT);
  digitalWrite(RFIDResetPin, HIGH);

  putstring_nl("> rfid ready");

  // play startup chime
  delay(500);  // avoid loudspeaker click noise

void loop() {
  RFIDindex = 0;

  //  rfid data?
  while(RFIDSerial.available()) {
    int readByte =;

    if(readByte == 2) RFIDreading = true;
    if(readByte == 3) RFIDreading = false;

    if(RFIDreading && readByte != 2 && readByte != 10 && readByte != 13){
      RFIDtag[RFIDindex] = readByte;

  //  check tag and play track if tag id found
  //  prepare for next read

void playcomplete(char *name) {
  while (wave.isplaying) {
    // playing

void playfile(char *name) {
  if (wave.isplaying) {
  if (!, name)) {
    putstring("Couldn't open file "); Serial.println(name); return;
  if (!wave.create(f)) {
    putstring_nl("Not a valid WAV"); return;

void resetReader() {
  digitalWrite(RFIDResetPin, LOW);
  digitalWrite(RFIDResetPin, HIGH);

void clearTag(char one[]) {
  for(int i = 0; i < strlen(one); i++){
    one[i] = 0;

void checkTag(char tag[]) {
  if(strlen(tag) == 0) return;

  if(debug) {
    putstring("rfid tag id: ");
    for(int d = 0; d < 12; d++) {

  boolean matching = true;

  //  compare tag id
  for(int a = 0; a < NUMTAGS; a++) {
    matching = true;
    for(int c = 0; c < 12; c++) {
      if(tag[c] != audiotags[a][c]) {
        matching = false;

    //  in case of a match play the track
    if(matching) {
      putstring("playing: "); Serial.println(audiofiles[a]);
      delay(500);  // avoid loudspeaker click noise
      if(PlayComplete) {
        digitalWrite(RFIDResetPin, LOW);
      } else {
        unsigned long playnext_currentmillis = millis();
        // wait with playing another audio track
        if(playnext_currentmillis - playnext_prevmillis > playanother_time) {
          playnext_prevmillis = playnext_currentmillis;

// ---------------------------
// debug functions
// ---------------------------
int freeRam(void) {
  extern int  __bss_end;
  extern int  *__brkval;
  int free_memory;
  if((int)__brkval == 0) {
    free_memory = ((int)&free_memory) - ((int)&__bss_end);
  else {
    free_memory = ((int)&free_memory) - ((int)__brkval);
  return free_memory;

void sdErrorCheck(void) {
  if (!card.errorCode()) return;
  putstring("\n\rSD I/O error: ");
  Serial.print(card.errorCode(), HEX);
  putstring(", ");
  Serial.println(card.errorData(), HEX);

Is there anywhere in there that I can reduce the memory footprint so that I can have more than a dozen or so files linked? Note that I don’t really have a way (that I know of) to reduce the length of the RFID codes under “audiotags”, because I only have a reader, not a writer. I could change the “audiofiles” names to anything.

Thanks very much for any help you can give, I really appreciate it.

The first thing you could do is move those big character arrays into flash program memory.

Take a look at "PROGMEM". Some info here:- PROGMEM

That would save you almost 1.5KB of SRAM.

On an aside, this should be 'unsigned long', not 'long':-

long playnext_prevmillis = 0;

Fantastic, thanks!

I must apologize, though - I'm having trouble figuring out how to use that. Would I just be adding the

const dataType variableName[] PROGMEM =

in the place of my current

char audiotags[NUMTAGS][27]

Thanks very much for bearing with my inexperience at this!

elliotlevin: Fantastic, thanks!

I must apologize, though - I'm having trouble figuring out how to use that. Would I just be adding the

const dataType variableName[] PROGMEM =

in the place of my current

char audiotags[NUMTAGS][27]

Thanks very much for bearing with my inexperience at this!

You need to read, study and understand the whole page that I linked. Take your time, and even write a couple of small test programs. You need to use specialised methods of reading the data from flash memory, and a specialised method of storing an array of strings. It's all explained.

Pay particular attention to the "Arrays of strings" section.

I could only repeat what's written on that page.

You've got a load of strings in your prints that would benefit from the F() macro too. If you've got a memory card, why not store the indices on that?

char audiotags[NUMTAGS][27] = {"0000EE1E6E9E",

How big is a tag? 12 characters max? You can change the 27 to 13.

char audiotags[NUMTAGS][13] = {"0000EE1E6E9E",

Same for filenames; 8-dot-3 notation so max 12 characters; again change the second dimension to 13

That will reduce the memory usage for those arrays by over 50%.