PgmPrintln - it's all ascii to me!


I’ve been routing around with a problem with a SD card, and think I have narrowed it down to an error handling function that gets called.

I’ve used SD.h to create my own library of higher level functions for working with files. Although the library is mostly mine, like all my code it includes bit’s cribbed from other libraries. In this case the bit i copied was this…

void SdFileFunctions::error_P(const char* str) 
  PgmPrint("error: ");
  if (card.errorCode()) {
    PgmPrint("SD error: ");
    Serial.print(card.errorCode(), HEX);
    Serial.println(card.errorData(), HEX);

The first thing that I’ve only just worked out is that while(1) is an endless loop - which finally explains why whenever I get an issue with an SD card my code hangs! (I’ve puzzling that one for months!)

More baffling (until now) was that the error message generated was this…


I’ve finally twigged that this is the an error message expressed in ascii code! 101=‘e’ 114=‘r’ 114=‘r’ 111=‘0’ 114=‘r’ … spells ‘error’, etc etc…

So it looks to me like PgmPrintln() is printing out the ascii equivalent of everything, instead of the character string.

Here’s a working sketch to illustrate the issue…

#if defined(ARDUINO) && ARDUINO >= 100
#include "Arduino.h"                       
#include "WProgram.h"                      

#include <SD.h>                            // New wrapper library for all the low level SD file functions. Replaces Sd2Card.h, FatStructs.h, Sd2PinMap.h, SdFat.h, SdFatmainpage.h, SdFatUtil.h, SdInfo.h

void setup()
  Serial.begin(115200);               // Communication speed for the display

void loop()

when I run this I get…


All the examples I can find seem to suggest that PgmPrintln() should work just like Serial.print() except that it prints from Progmem instead of normal memory, thus saving space.

The first lesson learnt is don’t copy someone elses code unless you understand what it’s doing! But apart from that, what’s going on, have I misunderstood what SdFileFunctions::error_P(const char* str) is supposed to be doing? or the what PgmPrintln() does?

I’m working on with a Mega 2560 using Arduino version 1.0