As a newbie on Arduino I still have some learning to do. But as a programmer I have been there for a while. So I have noticed some habits in Arduino programming; I am not going to say these are good or bad habits. I am just going to wonder if "doing the things you do is the best way to do it".
The most common way to set up a sketch is:
{declare a bunch of constants and variables}
void setup{
setup pins
setup other stuff
setup interrupt handler
run through some code
}
void loop {
stuff the loop part with code
and more code
and some more code
}
Declaring the constants and variables in the above part of the code means that one is allocating memory during the time the sketch runs. So you have ask yourself if that's necessary, maybe you use that specific variable just once.
First wonder if it's necessary to declare a constant, maybe you could use #define instead. The define statement is a compiler directive, which means the compiler will translate the defined label into it's value everywhere it exists in your code. Meaning that it won't allocate memory.
Second: if one is using a variable or a constant just once it may be possible to declare a variable locally. (= in a function.) Which has the advantage that the memory will be freed once the function is finished.
Third: sometimes it wise to pass a variable to a called function. Advantage is that you keep track of where a variable is used and the variable will be passed using the stack instead of the data area and that it's often possible to pass a direct value instead of a variable.
Example:
void myFunc(int myVariable){
//do some stuff
}
You can call this function with
myFunc(123)