Start timer mid sketch

Hi,

I'm very confused at the moment - its another timer related question.

Basically, this is what I want to do: (I'm using inputs from and Arduino through serial, but I'll substitute it for key presses here):

-Play track on key press ‘A’

-On keypress ‘B’, begin a 5 second timer

-When timer starts, begin to turn the sound down

-If key 'A' is pressed, exit from the timer and turn track up again

-If timer gets to the end, stop the track.

Below is a 5 second timer that I had been using, but it doesn't seem to be any use. Does anyone have any ideas on how to sort this issue?

int timeThreshold = 5;
int timeLastSent[] = {
  hour(), minute(), second(), - 1
};

void draw() {
    doThing();
}

void doThing() {
  int[] presentTime = {
    hour(), minute(), second()
  };

  println("Present Time" + presentTime[0] + ":" + presentTime[1] + ":" + presentTime[2] + "\t");
  println("Last Time Sent" + timeLastSent[0] + ":" + timeLastSent[1] + ":" + timeLastSent[2]);

  if (presentTime[1] == timeLastSent[1]) {
    println("we're on the same minute");
    if (presentTime[2] - timeLastSent[2] >= timeThreshold) {
      println("present time (seconds) MINUS last time (seconds) is more than the timeThreshold ");

      changeBG(); // change colour of output so I know something has happened

      timeLastSent[0] = hour();
      timeLastSent[1] = minute();
      timeLastSent[2] = second();
    }
  }

  if (presentTime[1] != timeLastSent[1]) {
    //calculate the difference in seconds
    int secondDifference = (60 - timeLastSent[2]) + presentTime[2];

    if (secondDifference >= timeThreshold) {
      //DO THING
      changeBG();  // change colour of output so I know something has happened

      timeLastSent[0] = hour();
      timeLastSent[1] = minute();
      timeLastSent[2] = second();
    }
  }
}

void changeBG() {
  float cl = random(0, 126);
  float cl2 = random(0, 126);
  float cl3 = random(0, 126);
  background (cl, cl2, cl3);
}

------------EDIT--------

I just used something like this in the end. Sorry for bothering anyone...I think I just needed to get away from the computer for a while to come back to it with a fresh pair of eyes :/

int begintime = 3;
int window = 0;
int start;

void setup() {
  size(400, 400);
  background(0);
  frameRate(1);
  loop();
}

void draw() {
  if (window == 0) {
    background(#FF0000);
  }
  if (window == 1) {
    background(#FFFFFF);

    int ms = millis()-start;
    println(ms);

    // int sec = frameCount/60;
    int sec = ms/1000;

    int timer = begintime - sec;

    if (timer <= 0) {
      window = 2;
    }
  }

  if (window == 2) {
    background(0);
  }
}
void keyPressed() {
  if (keyCode == ENTER) {
    window =1;
    start = millis();
  }
}
Tagged:
Sign In or Register to comment.