Last active
August 29, 2015 14:14
-
-
Save darkwave/9484686051616effde56 to your computer and use it in GitHub Desktop.
Efficent timer (maybe)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
int lastTime; //variabile | |
boolean stopTime = false; | |
int currentTime = 0; | |
int delay = 1000; //millis | |
final int INIT = -1; | |
final int RESTING = 0; | |
final int SHOWING = 1; | |
int state = INIT; | |
boolean debug = true; | |
void setup() { | |
size(500, 500); | |
noStroke(); | |
} | |
void initTime() { //resettiamo tutto | |
currentTime = 0; | |
stopTime = false; | |
thread("timer"); | |
state = RESTING; | |
} | |
//int timeResting = 0; | |
int timeToRest = 5; | |
void draw() { | |
background(#333333); //nero "elegante" | |
switch(state) { | |
case INIT: | |
timeToRest = (int) random(2, 7); | |
initTime(); | |
text("Init...", width / 2, height / 2); | |
break; | |
case RESTING: | |
println(timeToRest); | |
if (currentTime > timeToRest) { | |
state = SHOWING; | |
} | |
break; | |
case SHOWING: | |
if (currentTime - timeToRest > 1) { | |
state = INIT; | |
} | |
ellipse(width / 2, height / 2, 100, 100); | |
break; | |
} | |
if (debug) | |
text(currentTime, 20, 20); | |
} | |
void keyReleased() { | |
if (keyCode == ESC) { | |
stopTime = true; | |
} | |
if (key == 's') { | |
if (stopTime == false) { | |
stopTime = true; | |
} else { | |
state = INIT; | |
} | |
} | |
if (key == ' ') { | |
paused = !paused; //tipico "toggle" | |
} | |
} | |
boolean paused = false; | |
void timer() { | |
println("Timer started!"); | |
lastTime = millis(); | |
while(!stopTime) { | |
if (!paused && millis() - lastTime > delay) { | |
currentTime++; | |
lastTime = millis(); | |
} | |
if (currentTime > 100000) | |
stopTime = true; | |
} | |
println("Timer stoped!"); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment