I'm very new to arduino programming and am having trouble understanding a few basics. I've been trying to unterstand why some code will set up a variable before the void setup() and others variables are defined inside the void loop()....I've also come accross code with int variables defined in the void setup() section... Can someone explain why you would do it in the setup section loop section or outside of both?? I could not find that info everywhere but it sounds like it should be very basic info...
It depends what you want the scope of the variables to be. If declared inside a function (like setup) they only "exist" there. If declared outside any function they are global variables that persist (exist) for the whole sketch.
fxmech: Can someone explain why you would do it in the setup section loop section or outside of both?? I could not find that info everywhere but it sounds like it should be very basic info...
Not just ints but all variables. Sometimes you need an int to do a calculation inside a function (like setup or loop), but you don't need it in other places. So that int is "local" to that function. It is called a "local variable".
Local variables keep things simple, because they only exist within the function, so you don't have to "worry" about what they are doing outside the function.
Sometimes though you need variables which are available to all functions and you can put these outside of the functions in your program. These are called "global" variables.
There have been religious wars about local and global variables.
Very roughly the "scope" of a variable is where it exists. In general a variable inside a function has local "scope" and cannot be "seen" by other functions.
That was a very short intro, but I hope it helps!
That helps tremendously! thanks