(At the moment, this is question is not sketch specific.)
Is there a way to reset a global variable (or a defined set of global variables) to its default value, as initialised before setup() ?
I could copy/paste the default values at the appropriate point(s) in the sketch, but if I later change the default value, the same value change has to done twice. And there's a chance it's forgotten.
int x, y , z; // zero initialized by the compiler
void reset2Defaults() {
x = 3;
y = 6;
z = 9;
}
void setup() {
reset2Defaults();
...
}
void loop() {
...
}
and in the loop, when you want to revert to the defaults, you call reset2Defaults()
Ah, I see! (I think.)
The variable statements in reset2Defaults() just change the values (I'd missed the lack of variable type declarations).
The variables still need to be declared before setup() and their intialisation is done by calling reset2Defaults() within setup() and wherever else it needs to be done.
Right?
Another idea related to this is to create a big structure holding all the variables that are meaningful for your code. That will make it easy for you if at some point you want to store current state in EEPROM for example and it keeps all those in a well know place, so you know where to look if you think you missed something.
Sure you are right, I would have assumed that reseting the program would involve more than just resetting the variables, hence the idea of the function. you could inline it if you want and let the compiler decide.
using sprintf() is really heavy (flash memory impact) and you are at risk of overflowing your 39 bytes text buffer on a 32bit architecture (use -2147483647 for x,y and z).
It's safer to just print each element separately and you can even benefit of storing the text in Flash memory)
So I would recommend
Serial.print(F("x = ")); Serial.print(params.x);
Serial.print(F(", y = ")); Serial.print(params.y);
Serial.print(F(", z = ")); Serial.println(params.z);