S3196655 Stopwatch Processing sketch - PROJECT 3
From Physical Programming
For this sketch I decided to emulate the Stopwatch patch I created in Pure Data in Processing. The entire sketch is based around the millis() command. When called this returns the number of milliseconds since the program started running. Thus I can set up an integer variable for milliseconds. From this I deduced that seconds would be milliseconds/1000, minutes would be seconds/60 and so on.
int milliseconds = (millis()-startingTime); int seconds = milliseconds / 1000; int minutes = seconds / 60; int hours = minutes / 60; int days = hours / 24;
To reset these numbers I use the line milliseconds -= seconds * 1000. This equates to milliseconds = milliseconds - (seconds * 1000). So when milliseconds are < 1000, seconds = 0. When milliseconds = 1000, seconds = 1 sending milliseconds back to 0 again.
milliseconds -= seconds * 1000; seconds -= minutes * 60; minutes -= hours * 60; hours -= days * 24;
With the various units set up I created the buttons which would control start, pause, continue and reset. To do this I used the boolean command. This allowed me to set up an event which would return a true or false. The 's' key represents the start function. For this the boolean variable clockRunning is true and the integer variable startingTime is millis().
void keyPressed() {
if(key=='s') {
println("start");
clockRunning = true;
startingTime = millis();
}
if(key=='r') {
println("reset");
clockRunning = false;
timeSoFar = 0;
}
if(key=='p') {
println("pause");
clockRunning = false;
timeSoFar = millis() - startingTime;
}
if(key=='c') {
println("continue");
clockRunning = true;
startingTime = millis() - timeSoFar;
}
This then means that depending on what button is pushed clockRunning is either true or false. I linked the outcome to a string command. In simple terms a string is just a sequence of values.
if(clockRunning == true) {
background(0,0,0);
int milliseconds = (millis()-startingTime);
int seconds = milliseconds / 1000;
int minutes = seconds / 60;
int hours = minutes / 60;
int days = hours / 24;
milliseconds -= seconds * 1000;
seconds -= minutes * 60;
minutes -= hours * 60;
hours -= days * 24;
String message =
days + " : " +
nf(hours, 2) + " : " +
nf(minutes, 2) + " : " +
nf(seconds, 2) + " : " +
nf(milliseconds, 4);fill(255,255,255);
textAlign(CENTER);
text(message, 250, 100);
}
else {
background(0,0,0);
int milliseconds = (timeSoFar);
int seconds = milliseconds / 1000;
int minutes = seconds / 60;
int hours = minutes / 60;
int days = hours / 24;
milliseconds -= seconds * 1000;
seconds -= minutes * 60;
minutes -= hours * 60;
hours -= days * 24;
String message =
days + " : " +
nf(hours, 2) + " : " +
nf(minutes, 2) + " : " +
nf(seconds, 2) + " : " +
nf(milliseconds, 4);
fill(255,255,255);
textAlign(CENTER);
text(message, 250, 100);
By making milliseconds = millis() - startingTime (If clockRunning is true), I am able to control whether the stopwatch starts from 0 or continue from the previous value. This works much the same way when clockRunning is false. I can either stop and reset the count to 0 or just pause it. I then assign this string a variable. In this instance I have called it message. By using the nf() command I can format each unit so that the display always has the same number of digits. This is more for a cleaner looking stopwatch than anything else. Finally textAlign makes sure that the numbers always sit in the center of the screen.
Here is a folder containing the final sketch Media:S3196655_Stopwatch.zip

