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

#1

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.

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);
}
1 Like

#2

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.

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:

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

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:

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:

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();
}
3 Likes