# Getting an If statement to revert to previous state after else?

**URL:** <https://discourse.processing.org/t/getting-an-if-statement-to-revert-to-previous-state-after-else/10278>\
**Category:** Libraries\
**Created:** [April 14, 2019, 8:43pm UTC](https://discourse.processing.org/t/getting-an-if-statement-to-revert-to-previous-state-after-else/10278 "2019-04-14T20:43:41Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![1schroeteeri](https://avatars.discourse-cdn.com/v4/letter/1/439d5e/32.png) [@1schroeteeri](https://discourse.processing.org/u/1schroeteeri)\
**Post date:** [April 14, 2019, 8:43pm UTC](https://discourse.processing.org/t/getting-an-if-statement-to-revert-to-previous-state-after-else/10278/1 "2019-04-14T20:43:41Z")

</div>

Hi, I’m trying to get my if statement (which I’m trying to have stay “activated” for a few moments upon triggering it then disappear after staying up for a few seconds) to revert back to it’s state before the else if upon the else if staying visible and active for a few moments. (basically have the sound play for a few seconds then everything goes back to how it was before)

I’m new to coding and even after watching a video or two I’m entirely stuck on this likely simple problem! All I want is for the sound and cube to change colors for a set amount of time then pop back to the sketches previous state.

```auto
int time;
import ddf.minim.*;
Minim minim;
AudioPlayer player2;
 
void setup() {
  size(400, 400);
  minim = new Minim(this);
  player2= minim.loadFile("Shhh Sound effect.mp3");
  time = millis();
  }
 
void draw() {
  background(0);
  if (dist(mouseX, mouseY, 200, 200)<100) {
    fill(36);
    player2.play();
    if (millis() < time) {
      fill(255,0,0);

    }
  } else if(millis() < time) {
    time = millis() + 300;
    player2.rewind();
    fill(36);
  }
  ellipse(200, 200, 200, 200);
}

```

---

<div class="post-metadata">

**Author:** ![jb4x](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jb4x/32/789_2.png) [@jb4x](https://discourse.processing.org/u/jb4x)\
**Post date:** [April 14, 2019, 9:03pm UTC](https://discourse.processing.org/t/getting-an-if-statement-to-revert-to-previous-state-after-else/10278/2 "2019-04-14T21:03:15Z")

</div>

Hi,

You messed up things a bit here =)

Because of the way you wrote your code, you will never enter your if statements.  
Your start by setting `time` to `millis()` in `setup()` so time will never be bigger than `millis()` since `millis()` return the number of milliseconds spared since the beginning of the program…

To convince you try this remake of your code and you’ll see that the “test” will never be prined out in the console.

```auto
int time;

void setup() {
  size(400, 400);
  time = millis();
}

void draw() {
  background(0);
  if (dist(mouseX, mouseY, 200, 200)<100) {
    fill(36);
    if (millis() < time) {
      println("test");
      fill(255, 0, 0);
    }
  } else if (millis() < time) {
    println("test");
    time = millis() + 300;
    fill(36);
  }
  ellipse(200, 200, 200, 200);
}

```

Now what you want to do is to save the time when your event start. Let’s say I’m having the basic sketch as followed:

```auto
boolean light;

void setup() {
  size(400, 400);
  background(200);
  light = true;
}

void draw() {
  if (light) {
    background(200);
  } else {
    background (20);
  }
}

void mouseClicked() {
 light = !light; 
}

```

By clicking the mouse I’m turning the light on and off.

Now let’s imagine that I want it to be dark for 2 seconds and then return to light. Then I first need to record when I clicked to get dark with a new variable `long startDark` and save the time when I clicked with the `millis()` function

```auto
void mouseClicked() {
 light = false; 
 startDark = millis();
}

```

Now in draw I can check the new time and see if 2 seconds have passed since the event that triggered the dark:

```auto
if (millis() > startDark + 2000) { // If 2 seconds have passed since it got dark
  light = true; // We can put the light back on
}

```

The full code will be something like this:

```auto
boolean light;
long startDark;

void setup() {
  size(400, 400);
  background(200);
  light = true;
}

void draw() {
  if (millis() > startDark + 2000) { // If 2 seconds have passed since it got dark
    light = true; // We can put the light back on
  }

  if (light) {
    background(200);
  } else {
    background (20);
  }
}

void mouseClicked() {
  light = false; 
  startDark = millis();
}

```
