Created
January 11, 2017 17:57
-
-
Save anonymous/f8d389a41e30a96b416f0d403e2fe6d3 to your computer and use it in GitHub Desktop.
This file contains 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
StopWatchTimer sw; | |
void setup() { | |
size(400, 400); | |
sw = new StopWatchTimer(); | |
} | |
void draw () { | |
background(0); | |
time(); | |
} | |
void time() { | |
fill(255); // Barva števca | |
textAlign(CENTER); | |
textSize(15); // Velikost časa | |
text(nf(sw.minute(), 2)+":"+nf(sw.second(), 2), width / 2, height / 2); | |
} | |
void keyPressed() { | |
println(keyCode); | |
// 84 = t, T | |
if (keyCode == 84) | |
sw.start(); | |
// 83 = s, S | |
if (keyCode == 83) | |
sw.stop(); | |
} | |
// CLASS | |
class StopWatchTimer { | |
int startTime; | |
int stopTime; | |
boolean running; | |
StopWatchTimer() { | |
startTime = 0; | |
stopTime = 0; | |
running = false; | |
} | |
void start() { | |
startTime = millis(); | |
running = true; | |
} | |
void stop() { | |
stopTime = millis(); | |
running = false; | |
} | |
int getElapsedTime() { | |
int elapsed; | |
if (running) { | |
elapsed = (millis() - startTime); | |
} else { | |
elapsed = (stopTime - startTime); | |
} | |
return elapsed; | |
} | |
int second() { | |
return (getElapsedTime() / 1000) % 60; | |
} | |
int minute() { | |
return (getElapsedTime() / (1000*60)) % 60; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment