Analoging a Digital Clock

This simple project shows how use the internal trig functions to draw an analog clock. We will be showing it on an OLED display.

The first function calculates and plots dots around the clock. Small dots for minutes and larger dots for hours.

# Draw the clock face
fn face()
  # Draw the small dot on the clock face
  for i=0 to 60
    a1[0]=25*cos(i*_p/30):a1[1]=25*sin(i*_p/30)
    Pixel(1,64+a1[0],32+a1[1])
  next

  # Draw the large dots for every hour
  for i=0 to 12
    a1[0]=25*cos(i*_p/6):a1[1]=25*sin(i*_p/6)
    Circle(1,64+a1[0],32+a1[1],2)
  next
fend 

Now we can show the clock hands.

# Calculate and draw the new hand positions
fn time()
  a0[0]=16*cos(_h*_p/6):a0[1]=16*sin(_h*_p/6)
  a1[0]=20*cos(_m*_p/30):a1[1]=20*sin(_m*_p/30)
  a2[0]=25*cos(_s*_p/30):a2[1]=25*sin(_s*_p/30)
  if _s=60:_s=0:_m=_m+1:end
  if _m=60:_m=0:_h=_h+1:end
  if _h=12:_h=0:end
  _s=_s+1
  
  Line(1,64,32,64+a0[0],32+a0[1])
  Line(1,64,32,64+a1[0],32+a1[1])
  Line(1,64,32,64+a2[0],32+a2[1])
fend

Here is the entire code.

1 Like