Project Clock Help!

Hey guys! I need help on my clock project. Why is the hour hand not moving? Help!

void setup() 
    {
      size(200, 200);
      stroke(255);

    }
    void draw()
    {
      background(34, 40, 20);
      fill(255, 255, 255);
      ellipse(width/2, height/2, width, height);

      pushMatrix();
      translate(width/2, height/2);
      rotate(radians(map(second(),0, 60, 0, 360)));
      fill(255, second()*2, second()*2);
      noStroke();
      rect(0, -50, 10, 10);
      popMatrix();

     pushMatrix();
     translate(width/2, height/2);
     rotate(radians(minute()*6));
     stroke(0);
     line(0, 0, 0, -50);
     popMatrix();

     pushMatrix();
     translate(width/2, height/2);
     rotate(radians(hour()*5));
     stroke(0);
     line(0, 0, 0, -50);
     popMatrix();
    }

Answers

  • Line 30 should be

    rotate(radians(hour()*30));

    since there is a full rotation every 12 hours (i.e. 360/ 12 = 30° per hour)

    Notice that since the method hour() return integers you will not get smooth movement of the hands but rather it will jump 30° every hour on te hour

  • Answer ✓

    To fix that you need to consider fractions of minutes and hours like this

    void setup() 
    {
      size(200, 200);
      stroke(255);
    }
    void draw()
    {
      background(34, 40, 20);
      noStroke();
      fill(255, 255, 255);
      ellipse(width/2, height/2, width, height);
    
      pushMatrix();
      translate(width/2, height/2);
      rotate(radians(map(second(), 0, 60, 0, 360)));
      fill(255, second()*2, second()*2);
      rect(0, -50, 10, 10);
      popMatrix();
    
      strokeWeight(3);
      float minutes = minute() + second()/60.0;
      pushMatrix();
      translate(width/2, height/2);
      rotate(radians(minutes * 6));
      stroke(0, 255, 0);
      line(0, 0, 0, -50);
      popMatrix();
    
      float hours = hour() + minutes/60.0;
      pushMatrix();
      translate(width/2, height/2);
      rotate(radians(hours * 30));
      stroke(0, 0, 255);
      line(0, 0, 0, -30);
      popMatrix();
    }
    
Sign In or Register to comment.