We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpSyntax Questions › how to read time integers
Page Index Toggle Pages: 1
how to read time integers? (Read 271 times)
how to read time integers?
Jan 25th, 2009, 8:58am
 
hello,
i am using second(), minute(), and hour() and some coded vector shapes i created to represent numbers for a clock.

i was wondering what do i need to use to read the separate numbers such as the "0" and "8" in the seconds "4:13:08"?
do i need to use arrays?

here's some of the code. it's too long to post. any help is appreciated! thanks!


int s = second();
int m = minute();
int h = hour();


void setup(){
 size(500,694);
 smooth();
 bg = loadImage("casio.jpg");
}

void draw(){
 background(bg);
 colon();
   
 pushMatrix();
 translate(-65,-12);
 hourSpot1();
 popMatrix();
 
 pushMatrix();
 translate(-32,-12);
 hourSpot2();
 popMatrix();
}

void hourSpot1(){
 
 if ((h == 10) || (h==11) || (h==12) || (h==22) || (h==23) || (h==24)){
   num1();  
 }

}


//draw numbers for hour spot2
void hourSpot2(){
 
 if (h == 24){
   num2();  
 }
 if (h == 1){
   num1();  
 }
 if (h == 2){
   num2();  
 }
 if (h == 3){
   num3();  
 }
 if (h == 4){
   num4();  
 }

 if (h == 5){
   num5();  
 }
 
 if (h == 6){
   num6();  
 }
 if (h == 7){
   num7();  
 }
 if (h == 8){
   num8();  
 }
 if (h == 9){
   num9();  
 }
 if (h == 10){
   num0();  
 }
 if (h == 11){
   num1();  
 }
 if (h == 12){
   num2();  
 }
 if (h == 13){
   num1();  
 }
 if (h == 14){
   num2();  
 }
 if (h == 15){
   num3();  
 }  
 if (h == 16){
   num4();  
 }
 if (h == 17){
   num5();  
 }
 if (h == 18){
   num6();  
 }
 if (h == 19){
   num7();  
 }
 if (h == 20){
   num8();  
 }
 if (h == 21){
   num9();  
 }
 if (h == 22){
   num0();  
 }
 if (h == 23){
   num1();  
 }
 if (h == 24){
   num2();  
 }

}
void num1(){
   pushMatrix();

   pushMatrix();
   translate(16,2);
   vertDashRTop();
   popMatrix();

   pushMatrix();
   translate(16,22);
   vertDashRBott();
   popMatrix();

   popMatrix();
}

void vertDashRTop(){
 fill(0);
 noStroke();
 smooth();
 beginShape();
 vertex(0,4);
 vertex(4,0);
 vertex(4,18);
 vertex(0,14);
 endShape();
}
Re: how to read time integers?
Reply #1 - Jan 25th, 2009, 11:02am
 
It is simple...
Code:
void hourSpot1(){

if ((h == 10) || (h==11) || (h==12) || (h==22) || (h==23) || (h==24)){
num1();
} else {
num0();
}

}

I see a little inefficiency in your code, for hourSpot2(), which is slow (although it doesn't really matter here, to be honest) because you shouldn't test h after finding a value (if it is 2 it won't be 13) - this can be addressed by adding else before each if (except the first, of course!).
But still, it isn't very readable and hard to maintain (proof: 24 is tested twice! Wink).
I suggest you use the switch construct, perfect for these cases:
Code:
switch (h) {
case 1:
case 11:
case 13:
case 23:
num1();
break;
case 2:
case 12:
case 14:
case 24:
num2();
break;
case 3:
case 15:
num3();
break;
case 4:
case 16:
num4();
break;
case 5:
case 17:
num5();
break;
case 6:
case 18:
num6();
break;
case 7:
case 19:
num7();
break;
case 8:
case 20:
num8();
break;
case 9:
case 21:
num9();
break;
case 10:
case 22:
num0();
break;
}

Both faster than cascaded ifs, particularly your form (switch jumps directly at the right case) and easier to understand (and smaller).

Exercice: handle option for 24hrs display (I am used to that!). :-D
Re: how to read time integers?
Reply #2 - Jan 25th, 2009, 9:35pm
 
thanks so much! it worked!

my last questions is:

how do i constantly have the time updated in the window? so far, i can only get the time to update when i refresh and run the app again (imagine the seconds not moving).

this is what i have in my draw():


PImage bg;  
int s = second();
int m = minute();
int h = hour();

void setup(){
 size(500,694);
 smooth();
 bg = loadImage("casio.jpg");
}

void draw(){
 background(bg);
 colon();

 pushMatrix();
 translate(-65,-12);
 hourSpot1();
 popMatrix();

 pushMatrix();
 translate(-32,-12);
 hourSpot2();
 popMatrix();

 pushMatrix();
 translate(12,-12);
 minSpot1();
 popMatrix();

 pushMatrix();
 translate(45,-12);
 minSpot2();
 popMatrix();

 pushMatrix();
 scale(0.8);
 translate(96,-5);
 secSpot1();
 popMatrix();

 pushMatrix();
 scale(0.8);
 translate(126,-5);
 secSpot2();
 popMatrix();
}
Re: how to read time integers?
Reply #3 - Jan 25th, 2009, 10:31pm
 
Make the declarations like that:
Code:
int s;
int m;
int h;

And at the start of draw(), just init the variables:
Code:
s = second();
m = minute();
h = hour();
Re: how to read time integers?
Reply #4 - Jan 25th, 2009, 10:39pm
 
wow you are really helpful! i'm on my way to slowly learning processing thanks to you! thanks so much again!
Page Index Toggle Pages: 1