Bulk reset vriables to default

(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.

a board reset would do that ? :wink:

joke aside, it's hard - esp. if you take into account also Object construction and dynamic initialisation that the compiler generates.

Read about the C++ specs for Initialization:

the easiest way is to have a function

void reset2Defaults() {
   x = 3;
   y = 6; 
   myInstance.setXYZ(1,2,3); 
  ...
}

that you will call in setup() or that you'll call when you want to reset the values.

So are variables declared in setup() treated as global variables?

No, they would be local to setup().

something like this:

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?

yes, that's the idea

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.

struct t_importantStuff {
  int x;
  int y;
  int z;
} params;

void reset2Defaults() {
  params.x = 3;
  params.y = 6;
  params.z = 9;
}


void setup() {
  reset2Defaults();
  // ...
}

void loop() {
  // ...
}
1 Like

Thanks.
(I'll go and learn about structures now.:grin:)

can do a block copy of entire structure without need to copy values explicitly

results for code below

main: x 3, y, 6, z 9
main: x 0, y, 0, z 0
main: x 3, y, 6, z 9
struct t_importantStuff {
  int x;
  int y;
  int z;
};

t_importantStuff defaults = { 3, 6, 9 };
t_importantStuff params   = defaults;

// -----------------------------------------------------------------------------
#include <stdio.h>

int main () {
  printf ("%s: x %d, y, %d, z %d\n", __func__, params.x, params.y, params.z);

  params  = { 0, 0, 0 };
  printf ("%s: x %d, y, %d, z %d\n", __func__, params.x, params.y, params.z);

  params  = defaults;
  printf ("%s: x %d, y, %d, z %d\n", __func__, params.x, params.y, params.z);
}

cool idea @gcjr !

(As this is the Arduino Forum I think we should stick to sample code with setup() and loop() so that newbies do not get confused)

struct t_importantStuff {
  int x;
  int y;
  int z;
};

t_importantStuff defaults = { 3, 6, 9 };
t_importantStuff params   = defaults;

void setup() {
  Serial.begin(115200);
  Serial.print(params.x); Serial.write(' ');
  Serial.print(params.y); Serial.write(' ');
  Serial.println(params.z);

  params  = { 0, 0, 0 };
  Serial.print(params.x); Serial.write(' ');
  Serial.print(params.y); Serial.write(' ');
  Serial.println(params.z);

  params  = defaults;
  Serial.print(params.x); Serial.write(' ');
  Serial.print(params.y); Serial.write(' ');
  Serial.println(params.z);
}

void loop() {}
// using a struct with built-in initialise

struct myStruct {
  // Sample variavbles
  int16_t varA;
  bool flagA;

  // Initialising function
  void initVars();
};

void myStruct::initVars() {
  varA = 15;
  flagA = false;
}

myStruct myVars;

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);

  myVars.initVars();

  Serial.print("varA = ");
  Serial.print(myVars.varA);
  Serial.print("; flagA = ");
  Serial.println(myVars.flagA ? "true" : "false");
}

void loop() {
  // put your main code here, to run repeatedly:
}

Willem

that's cool too (getting into Class territory, may be advanced for OP who was discovering the basics)

True.

Willem

struct t_importantStuff {
  int x;
  int y;
  int z;
};

t_importantStuff defaults = { 3, 6, 9 };
t_importantStuff params   = defaults;

char s [40];

// -----------------------------------------------------------------------------
void setup (void) {
  Serial.begin (9600);

  sprintf (s, " x %d, y, %d, z %d\n", params.x, params.y, params.z);
  Serial.print (s);

  params  = { 0, 0, 0 };
  sprintf (s, " x %d, y, %d, z %d\n", params.x, params.y, params.z);
  Serial.print (s);

  params  = defaults;
  sprintf (s, " x %d, y, %d, z %d\n", params.x, params.y, params.z);
  Serial.print (s);
}

void loop (void) {
}

is a function really needed when an "=" sign will do?

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);

no buffer, no risk :slight_smile:

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.