Notice. New forum software under development. It's going to miss a few functions and look a bit ugly for a while, but I'm working on it full time now as the old forum was too unstable. Couple days, all good. If you notice any issues, please contact me.

Forum Index : Microcontroller and PC projects : simple clock_22

Author Message
homa

Newbie

Joined: 05/11/2021
Location: Germany
Posts: 18
 Posted: 05:09pm 27 Jan 2022 Copy link to clipboard Print this post

hello, I like analogue clocks.

Here is a simple graphic version of a clock. The second hand jumps, as does the minute hand. The hour hand is sluggish / creeping.
I would have liked to make the hands not only different lengths, but also wide. Unfortunately, the LINE command with diagonal lines only works with the default value of 1 for the thickness.
Does anyone here know the reason for this?

Otherwise, have fun.

Regards
Matthias

' clock_22 ver 01
' by homa SDG
Backlight 60
pi=3.14159 '265359
x=MM.HRes/2
y=MM.VRes/2
r=MM.VRes/2-1
CLS
ClockFace
Do
datum\$=Date\$
datum\$=Left\$(datum\$,2)+"."+Mid\$(datum\$,4,2)+"."+Right\$(datum\$,4)
Text 0,0,Datum\$
Text MM.HRes-97,0,Time\$
s=Val(Right\$(Time\$,2))
m=Val(Mid\$(Time\$,4,2))
h=Val(Left\$(Time\$,2))
If h>12 Then h=h-12
If h<>hold Then
hold=h
HourHand h, 0
EndIf
If m<>mold Then
mold=m
MinuteHand m
EndIf
If s<>sold Then
sold=s
SecondHand s
EndIf
Loop

Sub SecondHand s
Static w
x2=Sin(w)*130+x
y2=Cos(w)*130+y
Line x,y,x2,y2,1,RGB(black)
hourhand h, 1
w=(180-s*6)*Pi/180
x2=Sin(w)*130+x
y2=Cos(w)*130+y
Line x,y,x2,y2,1,RGB(red)
sc=s-1
If sc<0 Then sc=59
If sc=m Then minutehand m
End Sub

Sub MinuteHand m
Static w
x2=Sin(w)*115+x
y2=Cos(w)*115+y
Line x,y,x2,y2,1,RGB(black)
w=(180-m*6)*Pi/180
x2=Sin(w)*115+x
y2=Cos(w)*115+y
Line x,y,x2,y2,1,RGB(white)
hourhand h, 0
End Sub

Sub HourHand h, d
Static w
If d=0 Then
x2=Sin(w)*85+x
y2=Cos(w)*85+y
Line x,y,x2,y2,1,RGB(black)
EndIf
k = m\2
w=(180-(h*30)-k)*Pi/180
x2=Sin(w)*85+x
y2=Cos(w)*85+y
Line x,y,x2,y2,1,RGB(white)
End Sub

Sub ClockFace
Font 2
'Circle x,y,r,1,1,RGB(32,32,32)
For i=0 To 11
w=(180-i*30)*Pi/180
x2=Sin(w)*150+x
y2=Cos(w)*150+y
If i=0 Then i=12
Text x2-8,y2-8,Str\$(i)
If i=12 Then i=0
Next i
End Sub

By the way, who cares: I have mounted the DS3231 from amazon plug-in for the Pico ResTouch LCD 3.5 and found it to be a good fit.

> option list
OPTION AUTORUN ON
OPTION COLOURCODE ON
OPTION SDCARD GP22
OPTION SYSTEM SPI GP10,GP11,GP12
OPTION SYSTEM I2C GP26,GP27
OPTION LCDPANEL ILI9488W, RLANDSCAPE,GP8,GP15,GP9,GP13
OPTION RTC AUTO ENABLED
OPTION TOUCH GP16,GP17
GUI CALIBRATE 0, 251, 3862, 1278, -854
>

Edited 2022-01-28 03:11 by homa

lizby
Guru

Joined: 17/05/2016
Location: United States
Posts: 1997
 Posted: 06:44pm 27 Jan 2022 Copy link to clipboard Print this post

Nice.

homa said  I would have liked to make the hands not only different lengths, but also wide. Unfortunately, the LINE command with diagonal lines only works with the default value of 1 for the thickness.

One possible, if imperfect solution is to draw multiple lines to the same endpoint, but offset from the center by a pixel (but you have to figure out "offset" in which direction).

~
Edited 2022-01-28 04:45 by lizby

Turbo46

Guru

Joined: 24/12/2017
Location: Australia
Posts: 1001
 Posted: 01:30am 28 Jan 2022 Copy link to clipboard Print this post

A triangle with the centre of the base at the centre of the clock?

TRIANGLE X1, Y1, X2, Y2, X3, Y3 [, C [, FILL]]

Bill
Keep safe. Live long and prosper.

handmixer

Newbie

Joined: 02/09/2020
Location: Denmark
Posts: 12
 Posted: 06:57am 28 Jan 2022 Copy link to clipboard Print this post

Nice work
/H

matherp
Guru

Joined: 11/12/2012
Location: United Kingdom
Posts: 6117
 Posted: 08:29am 28 Jan 2022 Copy link to clipboard Print this post

Use the triangle command with an array of points. The first element to draw the previous "hand" in black and the second to draw it in the colour required. You can have multiple triangles to make the hand. The attached was tested on a PicoMite with a 800x480 display and also a PicoMiteVGA @ 320x240 but could probably be tweaked to display better on the smaller screen

Option explicit
Option default integer
Dim integer small,nt=4 'Number of triangles being updated
Dim integer xx0(nt*2-1),yy0(nt*2-1),xx1(nt*2-1),yy1(nt*2-1)
dim integer xx2(nt*2-1),yy2(nt*2-1),tcol(nt*2-1),secs,mins,hours
CLS
small=MM.HRes:If MM.VRes < small Then small=MM.VRes
'RTC gettime
SetTick 600000,timecorrect
secs=Val(Right\$(Time\$,2))
mins=Val(Mid\$(Time\$,4,2))
hours=Val(Left\$(Time\$,2))
Do
secs=Val(Right\$(Time\$,2))
mins=Val(Mid\$(Time\$,4,2))
hours=Val(Left\$(Time\$,2))
hands(secs,mins,hours,(small\2-small\12),small\2, small\2)
Do
Loop While Val(Right\$(Time\$,2))=secs
Loop
End
'
Sub timecorrect
'  RTC gettime
End Sub

Sub hands(seconds , minutes ,  hours , size , x , y )
Local integer x1,y1,x2,y2,x0,y0,i
Local float angle=seconds*6
rotate(2,RGB(RED),angle,x,y,-3,50,3,50,-3,-size)
'make up the second hand with two triangles
rotate(3,RGB(RED),angle,x,y,3,-size,3,50,-3,-size)
angle=minutes*6 + seconds/10
rotate(0,RGB(BLUE),angle,x,y,-size/15,0,size/15,0,0,-size*0.8)
angle=hours*30 + minutes/2
rotate(1,RGB(GREEN),angle,x,y,-size/12,0,size/12,0,0,-size*0.5)
Triangle xx0(), yy0(), xx1(), yy1(), xx2(), yy2() ,tcol(), tcol()
Circle x,y, size\12, 0, , RGB(red), RGB(red))
Circle x,y, size\15, 0, , 0, 0
Circle x,y, size\20, 0, , RGB(gray), RGB(gray)
End Sub

Sub rotate(n , col , angle As float, x ,y , x0 , y0 , x1 , y1 r, x2 , y2 r)
Local integer x0a,y0a,x1a,y1a,x2a,y2a
x0a= x0*cosine - y0 * sine + x
y0a= y0*cosine + x0 * sine + y
x1a= x1*cosine - y1 * sine + x
y1a= y1*cosine + x1 * sine + y
x2a= x2*cosine - y2 * sine + x
y2a= y2*cosine + x2 * sine + y
xx0(n)=xx0(n+nt)
yy0(n)=yy0(n+nt)
xx1(n)=xx1(n+nt)
yy1(n)=yy1(n+nt)
xx2(n)=xx2(n+nt)
yy2(n)=yy2(n+nt)
xx0(n+nt)=x0a
yy0(n+nt)=y0a
xx1(n+nt)=x1a
yy1(n+nt)=y1a
xx2(n+nt)=x2a
yy2(n+nt)=y2a
tcol(n)=0
tcol(n+nt)=col
End Sub

Edited 2022-01-28 19:00 by matherp

homa

Newbie

Joined: 05/11/2021
Location: Germany
Posts: 18
 Posted: 09:53pm 28 Jan 2022 Copy link to clipboard Print this post

Thanks for the code, this is how it looks ...
I don't like the hands with the triangle. But the second hand is great.
I'll see what I can do with it.

hitsware2

Guru

Joined: 03/08/2019
Location: United States
Posts: 521
 Posted: 11:19pm 28 Jan 2022 Copy link to clipboard Print this post

( can't find ' delete message ' )
Edited 2022-01-29 09:25 by hitsware2
http://www.hitswares.com/