/* When you declare a variable outside of all functions, it becomes a global variable.
This means it can be accessed from anywhere in your code - in any function,
or in the loop() and setup().
*/
int myVar = 10; // Global variable
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
int myVar = 5; // Local variable with the same name
Serial.print("Local myVar: ");
Serial.println(myVar); // Prints 5, referring to the local variable
/* If you declare a variable with the same name inside a function, this creates a
new, separate variable that is local to that function. This local variable is only
accessible within the scope of the function where it's declared.
When you use the variable name inside the function, the Arduino compiler will refer
to the local variable, not the global one. This is because the local scope takes
precedence over the global scope.
The global variable is temporarily "hidden" within the function.
*/
}
void loop() {
delay(10); // this speeds up the simulation
Serial.print("Global myVar: ");
Serial.println(myVar); // Prints 10, referring to the global variable
// The local variable does not exisit in this function, it only exists in setup()
delay(10000);
}