A Clock program in Processing ( Analog & Digital )
in
Share your Work
•
1 years ago
Hi guys;
I just finish a clock program. It show a digital and a analog clock. It use the time from your system and show the time in digital format ( 24 Hr ) and an analog format. I am new at Processing ( since mid-October 2011 ) I was trying to learn Processing and try something cool. I realize learning Processing required a lot of math, geometry, and programing. I am not a student, I am just an hobbyist ( electronics & computer ), I did programing before ( BASIC, Pascal, C, C++ , some assembly ), recently, Java and Java ME <-- for my cell phone ... I did 2 pratical program so far. And Processing. <-- I was "forced" to learn it because I was trying to send a file from my PC to my Arduino, and Procesing was the most easiest to use for that purpose. ( I still need a keyboard routine in Processing like a cin function or scanf() function )
Here my code and tell me of what you guys thinks...
- /* A analog and digital clock display
- by Serge J Desjardins aka techone / tech37
- Toronto, Ontario Canada
- */
- PFont roman; // Analog Clock Font
- PFont hightech; // Digital Clock Font
- // list of variables
- int lex=600; // the display size x,y
- int ley=600;
- // Needles Co-ordinates
- int xsec;
- int ysec;
- int xmin;
- int ymin;
- int xhr;
- int yhr;
- int xst;
- int yst;
- // Time variable
- int sec;
- int mini;
- int hr;
- int twentyfour;
- // Needle size
- int distancesec=150;
- int distancemin=100;
- int distancehr=50;
- void setup()
- {
- // init the program
- size(lex,ley);
- smooth();
- // select the font file.
- // It came from Tools --> Create Fonts
- roman = loadFont("ImprintMT-Shadow-48.vlw");
- hightech = loadFont("LCD5x8H-48.vlw");
- // Calculate the centre point of the analog clock
- xst=lex/2;
- yst=(ley/2)+50;
- // time extract from your computer
- sec=second();
- mini=minute();
- hr=hour();
- twentyfour=hr;
- if (hr>11)
- {
- hr=hr-12;
- }
- // the co-ordinate calculation routine
- thehand();
- }
- void draw()
- {
- background(255);
- //draw the big circles
- fill(255);
- strokeWeight(2);
- stroke(18,90,12);
- ellipse(xst,yst,(distancesec+50)*2,(distancesec+50)*2);
- fill(255);
- strokeWeight(2);
- stroke(18,90,12);
- ellipse(xst,yst,(distancesec+10)*2,(distancesec+10)*2);
- // display the Romans numbers
- textFont(roman,25);
- fill(0);
- textAlign(CENTER);
- text("XII",xst,(yst-(distancesec+20)));
- fill(0);
- textAlign(CENTER);
- text("VI",xst,(yst+(distancesec+37)));
- fill(0);
- textAlign(CENTER);
- text("III",(xst+(distancesec+32)),yst);
- fill(0);
- textAlign(CENTER);
- text("IX",(xst-(distancesec+28)),yst);
- //draw the needles ( Hour, Minute, Second )
- strokeWeight(4);
- stroke(0,0,255);
- line(xst,yst,xmin,ymin);
- strokeWeight(6);
- stroke(0,255,0);
- line(xst,yst,xhr,yhr);
- strokeWeight(2);
- stroke(0,0,0);
- line(xst,yst,xsec,ysec);
- // a centre dot
- fill(255,0,0);
- noStroke();
- ellipse(xst,yst,10,10);
- // digital box
- fill(255);
- strokeWeight(4);
- stroke(0,69,162);
- quad(100,20,500,20,500,120,100,120);
- // the four dot inside the digital box
- // That code portion came from Strawberry15
- // is multi dot/circles program. A modify version
- for (int x1 = 233; x1 < 400; x1 = x1+133)
- {
- for (int y1 = 55; y1 < 100; y1 = y1+30)
- {
- fill(255,145,0);
- noStroke();
- ellipse(x1,y1,20,20);
- }
- }
- // the high tech digital display
- textFont(hightech,75);
- fill(0);
- textAlign(CENTER);
- text(sec,440,98);
- fill(0);
- textAlign(CENTER);
- text(mini,305,98);
- fill(0);
- textAlign(CENTER);
- text(twentyfour,168,98);
- // the counter ( second, minute, hour )
- // to be use for the co-ordinate calculation
- sec++;
- if (sec==60)
- {
- sec=0;
- mini++;
- if (mini==60)
- {
- mini=0;
- hr++;
- twentyfour++;
- if (twentyfour==24)
- {
- twentyfour=0;
- }
- if (hr==12)
- {
- hr=0;
- }
- }
-
- }
- // the co-ordinate calculation
- thehand();
- // the 1 second delay
- delay(1000);
-
- }
- // the co-ordinate calculation routine
- void thehand()
- {
- // the routine variable
- float xtemp;
- float ytemp;
- int anglesec;
- int anglemin;
- int anglehr;
- // find the angle in degree
- anglesec=(sec*6)+270;
- anglemin=(mini*6)+270;
- anglehr=(hr*30)+270;
- // calculate the x - y co-ordinate
- // to display the needles
- xtemp=distancesec*cos(radians(anglesec));
- ytemp=distancesec*sin(radians(anglesec));
- xsec=(int)(xst+xtemp);
- ysec=(int)(yst+ytemp);
- xtemp=distancemin*cos(radians(anglemin));
- ytemp=distancemin*sin(radians(anglemin));
- xmin=(int)(xst+xtemp);
- ymin=(int)(yst+ytemp);
- xtemp=distancehr*cos(radians(anglehr));
- ytemp=distancehr*sin(radians(anglehr));
- xhr=(int)(xst+xtemp);
- yhr=(int)(yst+ytemp);
- }