Memory problems, sd card stored arrays ...

Hello team,

I have a lot of data that i want to work with.
I use arduino uno so i have only 2k ram.... :confused:

My problems are two

  1. how can i read a file (txt) and store it as an array? (example file.txt : 123,234,121,2323,2323, - comma separated values)
  2. I do all kinds of stuff on arduino. I actually filled all my ports also. So its no wonder that if i load all my data in the same time
    my ram will be not enough! How can i load,create,new array for example , use it , and after used destroy and free the ram from it?
    With this 2K it is really pain in the ass to work :confused: P.S. i dont wanna use "PROGMEM" , i have 4GB sd card and a lot of files on it.
    Even if the speed of those operations will not be fast it will work for my case.

Regards

How big an array are you planning to read?
The SD card library uses a 512 byte buffer to be able to read the card so the 2kB ram is down to 1.5kB before you've read anything from the card. You might have to rethink the whole project.

Pete

You might have to rethink the whole project.

Or the hardware. The Mega, Yun, and Due all have more memory.

No, i don't use standard sd library. i wrote some code and i manage to have (with all my other libraries on and functions) 900B free ram.

But the data i want to process is bigger (like changing bitmaps on 5110 display etc.)
i need to get data from sd card (i know how to do that), then i have to assign it to variable.
i know how to do it with strings and int but i don't know how to make it with arrays.
And last i need to free the ram variable after use, otherwise even 8KB won't be enough.

I know i can use "delete variable" but is it safe?

//Create a dynamic array of ints with 16 elements.
int *myArr = new int[ 16 ]; 
   
//Use myArr just like a normal array.
myArr[ 0 ] = 4;
   
//Clean up once finished with the array.
delete [] myArr;

You don't need to use new/delete. It will create more problems than it is worth.
Declare a static array and when you are done with it, just overwrite it with the next set of values.

Pete

i know how to do it with strings and int but i don't know how to make it with arrays

Then, perhaps what you need to start with is an understanding of exactly what an array is.

It is simply a contiguous block of memory that can hold a collection of values of the same type. So, if you can store a value in a variable, you can store a value in one variable in the collection.

I know how to write a function to do that but i was looking for "ready" solution.

For the "static" solution it is not working for me.
It reserves space in memory. i need to free this space not to reserve it for that specific variable.
Otherwise i will run out of mem.
This is why i need to free / delete them?
any advice

For the "static" solution it is not working for me.
It reserves space in memory. i need to free this space not to reserve it for that specific variable.

How do you intend to use the array, after you've deleted it?

I think you've just about beaten the bush to death. It's time to quit beating around it, and explain what you are trying to do.

simple example:
function that plays music

-> create/load array from sd card (with hz to play)
-> play it
-> on finish delete array / free memory

.... p.s. all other important variables (smaller vars) i will put in static and i will give them size so they will not fragment the memory.

create/load array from sd card

I fail to see how a dynamically created array can use less memory than a statically defined array. The array has to fit in SRAM in either case. If this is running on a 328-based Arduino, and there is only one SD file open, there is no more than 1500 bytes of memory available, for the array, the stack, and for all other variables.

So, the first question is will the 1500 bytes be enough to hold your entire song/music?

i am running:

1 - LCD display 5110
2 - SD card reader
3 - DHT22 heat/pressure
4 - clock module
5 - active buzzer module

so i don't have that much space.
the project is already build in, so it will be kind a hard to remove again everything and start again with mega2560

i want first to try with uno.
i think it is possible just harder....

i think it is possible just harder....

You've got that right. It's like pulling hen's teeth to get data from you.

Nothing in that list can play music.

ok, play simple melodies with the buzzer.

any way my point is to dynamically create delete variables the right way!

in order to fit in 2 KB SRAM

ok, play simple melodies with the buzzer.

any way my point is to dynamically create delete variables the right way!

in order to fit in 2 KB SRAM

Why do you need an array for that? What, exactly, does the data consist of? Typically, it would be a frequency and a duration. That's two values. That hardly needs dynamic memory allocation.

Perhaps the key to solving your problem is to:

  • define ALL the requirements up front.
  • use a state machine to determine what to do on any given pass through loop().

That might involve reading the next frequency and duration from the SD card, or it might not.

you nailed it

...reading the next frequency and duration from the SD card, or it might not...

i want also to do other things - on display, on some data manipulation etc.

and i cannot store my arrays in sram.

either i need to use small vars like such for the buzzer.

but i definitely need arrays for images on 84 by 84 lcd display....

that's the other sram eating issue.

Why do you think it would be difficult to relocate your project to a Mega ? Should take about 4 minutes.

michinyon:
Why do you think it would be difficult to relocate your project to a Mega ? Should take about 4 minutes.

Maybe this:

so i don't have that much space.
the project is already build in, so it will be kind a hard to remove again everything and start again with mega2560