The static keyword is used to create variables that are visible to only one function. However unlike local variables that get created and destroyed every time a function is called, static variables persist beyond the function call, preserving their data between function calls.
Variables declared as static will only be created and initialized the first time a function is called.
static datatype variable;
- datatype: any data type: int, double, long, char, byte, etc
- variablename: a valid variable name
// x is initialized only once across the calls of loop()
static int x = 0;
Serial.println(x); // outputs the value of x
x = x + 1;