SD Keyes shield memory leak issues

I am writing an arduino sketch for a one use only barcode system. currently I have 2 arduinos, one with a USB host shield dealing with the scanner and a second with a keyes data logging shield. The USB shield arduino takes the scanned barcode and sends via serial connection to the data logging arduino.

On the data logging arduino side I seem to be getting a lot of strange issues with the SD operations. Writes tend to be ok but when reading from the SD card i seem to be getting a lot of gibberish.

When using the SdFat object I run out of global variable space (89%) being used up hence why I moved that object to within the functions its called in.

If anyone could give me some pointers on how to sort out these SD issues I would grateful,

(Code Attached)

Thanks

Andy

BSCAN_SDFAT_240816_0812.ino (11.4 KB)

      if((stxFlag = true)&& (din != '+'))

stxFlag will always be true, in that code.

Got my hopes up there! Unfortunately that didn’t fix the issues i’m having, here is the output from serial monitor:

//*****************************

Start
initialization done.
LCD Address: 63
Scanned: 014720311490
CD MATCHED
cDigit: 0
UNIX TIME: 1472031864
BARCODE TIME: 1472031149
Reject Time: 3600
Unix - Barcode: 715
bcode.txt:
EOF: NO MATCH
Scanned: 014720311490
CD MATCHED

cDigit: 0

UNIX TIME: 1472031868

BARCODE TIME: 1472031149

Reject Time: 3600

Unix - Barcode: 719

bcode.txt:
+PLUS
�%'

//*********************************

Still getting garbled output from barcode.txt

Thanks again for the help, been scratching my head all morning!

UPDATE:

I have removed all the Serial.print() lines in the SDReadCode function and everything is now working as it should.

Any ideas why the two would conflict?

As far as I'm aware the SD shield only uses pins 10-13 so I don't think its hardware related.

Thanks again for the help,

Andy

how much RAM used when the serial is included?
Perhaps you're running out of free ram for stack/strings/heap usage.
Are your char buffers large enough?

As its only 8 serial lines that I removed (not all the serial prints throughout the entire program) the RAM usage hasn't changed at all.

The buffers should be fine as the barcodes are standard 12 digit UPC-A codes with '+' and '-' deliminators.

The compiler hasn't thrown any memory issues at me:

//******************************************************************************************

Sketch uses 24,054 bytes (74%) of program storage space. Maximum is 32,256 bytes.
Global variables use 1,143 bytes (55%) of dynamic memory, leaving 905 bytes for local variables. Maximum is 2,048 bytes.

//******************************************************************************************

You really should read this: The Evils of Arduino Strings

        input+=din;

You are doing a lot of memory allocations by doing this. Possibly you are getting fragmentation, as Ps991 has hinted at. You are much better off using a static array of characters, and probably not passing that around all over the place like you are doing.

andyowenwest:
When using the SdFat object I run out of global variable space (89%) being used up hence why I moved that object to within the functions its called in.

Moving that object doesn't fix the problem though. The memory usage you see on compile only tells you about the global variables, so you made that number look nicer. BUT, the SdFat object still has to be allocated when that function is called, it just happens on the stack now instead of the heap. You're still using the same amount of memory at that point, you've just hidden that fact from yourself.