I know the proper way is to declare outside, but here is the thing.
I have a microSD reader, and I want to store the data in an array. So before setup, it doesn't know how big has to be.
it's a project that takes lots of memory, so I can't declare an oversized value
maybe some alloc or malloc to resize the space for the array (I don't know how those work)
or maybe some global class with a variable that can be resized
no if you are going to create dynamic array you can create it inside a function and return pointer. but if you haven’t done dynamic memory management before, create oversized static array and live a happy life
okay here it is a representation a bit more extended
#define NUM_ROW 4
#define NUM_COL 4
unsigned char val[NUM_ROW*NUM_COL];
void setup() {
if (SD.begin(A0)) {
sdInitialized=true;
}else{
error("initializing SD failed! ");
}
readConfig();
}
void readConfig(){
File myFile = SD.open("config.txt");
if (!myFile) {
error("opening conf");
return;
}
unsigned int count=0;
while (myFile.available()) {
unsigned char c = myFile.read();
val[count]=c;
count++;
}
myFile.close();
}
Again, this is a representation. The first line that i read from the SD give me the dimensions
The dimensions for what? What Arduino are you using?
I infer from your code that the dimensions you are talking about are row and column. Without knowing your actual application if you MUST read the entire file into memory then I suggest you read the dimensions, calculate the memory required, check that the memory does not exceed some threshold, and only then dynamically allocate the memory.
Keep in mind this is RAM we are talking about. Also, you will have to ensure you leave enough memory to support stack operations and any other dynamic allocation you will be performing.
i'm making a keyboard, and the microSD stores the values of each key, and im planing to store multiple layers. all of that with arduino pro micro, i'm just begining with the project and it says 59% of the space is used for the sketch, maybe its for the libraries SPI.h SD.h and Keyboard.h that uses almost all the space.
ensuring the space for all data of the SD i will do it later, at the moment I don't even know how to organize the data on the SD