About Restarting

Can I write a Reset code for my sketch. Like the Stop and Run method

1 Like

No, you have to do it manually

Move everything from setup except size into a new function and then call it from setup and Also when you want to restart

2 Likes

You need to manage the reset, as @Chrisir says. If you create a bunch of global variables objects and then run setup() again their values will persist – unless you manually reset them.

If your sketch relies on frameCount or millis(), you will also need to track them over restarts.

frameCount can be manually reset if you need to – if it is set to -1, then it will advance to zero and automatically re-run setup(). This is one easy way of restarting a sketch.

Note however that millis() will still measure from the time the sketch launched – not from restart – so if you want to measure the time since restart, you need a custom millis().

reset specific things with a reset function

int count = 0;
void setup() {
   size(200,200);
   reset();
}
void reset() {
  count = 0;
}
void draw() {
  background(128);
  text(count, width/2, height/2);
  count++;
}
void keyReleased() {
  reset();
}

reset by rerunning setup() with frameCount= -1

int count = 0;
void settings() {
  size(200,200);
}
void setup() {
   count = 0;
}
void draw() {
  background(128);
  text(count, width/2, height/2);
  count++;
}
void keyReleased() {
  frameCount = -1;  // reset
}

reset plus keep a per-reset timer

int count = 0;
int resetStart = 0;
void setup() {
   size(200,200);
   reset();
}
void reset() {
  count = 0;
  resetStart = millis();
}
void draw() {
  background(128);
  text(count, width/2, height/2);
  text(timer(), width/2, 12 + height/2);
  text(millis(), width/2, 24 + height/2);
  count++;
}
void keyReleased() {
  reset();
}
int timer() {
  return millis() - resetStart;
}
2 Likes