Hi, i made a very basic clock and i have two questions.
1: I wonder how can i reset the hours. Like when it past 24 it restart to 0.
2: How can i make a statement when a new day start. Can i use a boolean ?
void setup(){
size(200,40);
}
int time = 0;
int seconds;
int minutes;
int hour;
int day;
void draw(){
background(0);
clockTick();
text("minutes " + minutes,10,10);
text("hour " + hour,10,20);
text("day " + day,10,30);
}
void clockTick(){
time = millis();
seconds = (int) (time/1000)*7200;
minutes = seconds/60;
hour = minutes/60;
day = hour/24;
}
Thanks for your help.
1 Like
kll
2
-a- you not need to calculate:
void setup(){
size(200,40);
textSize(15);
}
void draw() {
background(0,0,80);
text(year()+"/"+month()+"/"+day()+" "+hour()+":"+minute()+":"+second(),10,25);
}
-b- millis() not give the clock time ,
it give the milli seconds since sketch start.
Hi, That’s not what i mean,
I know it’s what i want, an “in game” clock
kll
4
int time = 0;
int seconds;
int minutes;
int hour;
int day;
void draw_time(){
background(0);
clockTick();
text(" s " + seconds+" m " + minutes+" h " + hour+" d " + day,10,30);
}
void clockTick(){
time = millis();
seconds = (int) (time/1000);//????*7200;
minutes = seconds/60;
hour = minutes/60;
day = hour/24;
// repair
hour -= day*24;
minutes -= day*24*60 +hour*60;
seconds -= day*24*60*60 +hour*60*60 + minutes*60;
}
but that is the primitive way… pls find modulo % !!
also check if ‘time’ can be INT or should be LONG
Thanks i will check modulo.
How can i make a statement when a new day start ?
Like when day increment by one, i have a text say “a new day start”
i found a way, i pretty sure its not the best but it work.
if(hour <= 1){
text("new day",10,40);
}
Modulo wraps around to zero. % (modulo) / Reference / Processing.org
So if you are counting:
x |
0 |
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
x%8 |
0 |
1 |
2 |
3 |
4 |
5 |
6 |
7 |
0 |
1 |
2 |
x%5 |
0 |
1 |
2 |
3 |
4 |
0 |
1 |
2 |
3 |
4 |
0 |
x%3 |
0 |
1 |
2 |
0 |
1 |
2 |
0 |
1 |
2 |
0 |
1 |
x%2 |
0 |
1 |
0 |
1 |
0 |
1 |
0 |
1 |
0 |
1 |
0 |
In your case, if you want to count from 0-23 over and over, then:
h = (h+1)%24
1 Like