Getting global constants from an SD card

Hello everyone,

I am very new to Arduino so hopefully this is just an issue with my lack of knowledge. I am trying to pull variables off my SD card (example: Altitude, wind data, step size. Things that I don't want hardcoded into my program but can be easily changed via SD card) and then use them in other functions of my program such as in the loop and in ones of my own. I am using the adaFruit sd reader but I can only find ways to pull data off of it if it is within void setup(). This means anything I pull off of it is local and I cannot use it with other functions. Is there a way to change local variables to global? Or a different way to pull information off of my SD card without making it local?

Thank you and any help is appreciated!

Variable scope depends on where the variable is declared how long it persists depends on how it is declared.

Any variable type preceded by the word 'static' retains its value for the life of the execution. In embedded software like Arduino this means until the processor is reset.

The scope of any variable (ie, where it is visible) is only within the block (between curly braces) it is in, unless it is outside of all blocks in which case the variable is global and available to all blocks. In the case of duplicate names the most restricted scope wins.

So what does this mean?

Declare a variable in a function and it is only available to the function and created every time you call the function. Declare a variable as static within a function and it will be available within the function, initialised to zero the first time you use it and it will retain its value between function calls.

Declare a variable at the top of the file (or anywhere, really) and it persist for the life of the program. This also makes it automatically 'static' as it is never recreated and can retain its value.

I would also add that you can use the on board EEPROM to store your variables and not have to add extra hardware and software to manage the SD card.

This may help

Thank you Marco_C, I will definitely look into the EEPROM.

As for the scope of the variables, I understand that variables made within a function are local and cannot be used elsewhere and that if I declare the variables at the start of my program then they will work globally.

This issue I am having is that I can't find a way to pull variables off of my SD card without doing it within a function(like setup). I need to find a way to either get variables off the SD card outside of a function or pull variables out of the setup function for further use.

I just started my graduate program and my advisor alerted me to the fact that my thesis will be about 90% arduino programming. I'm a mechanical engineering student so this is going to be a rough road.

Thanks again!

I need to find a way to either get variables off the SD card outside of a function

In C++ all user code must run in a function so this statement does not make any sense to me.

pull variables out of the setup function for further use

If you understand scoping then why aren't you declaring global variables and reading them from the SD card during setup()?

Maybe describe what you are trying to do rather than asking for a specific solution to a perceived problem.