![]() |
Forum Index : Microcontroller and PC projects : simple clock_22
Author | Message | ||||
homa![]() Guru ![]() Joined: 05/11/2021 Location: GermanyPosts: 459 |
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 StatesPosts: 3314 |
Nice. 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 PicoMite, Armmite F4, SensorKits, MMBasic Hardware, Games, etc. on fruitoftheshed |
||||
Turbo46![]() Guru ![]() Joined: 24/12/2017 Location: AustraliaPosts: 1636 |
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: DenmarkPosts: 12 |
Nice work /H |
||||
matherp Guru ![]() Joined: 11/12/2012 Location: United KingdomPosts: 10068 |
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 float sine=Sin(Rad(angle)),cosine=Cos(Rad(angle)) 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![]() Guru ![]() Joined: 05/11/2021 Location: GermanyPosts: 459 |
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 StatesPosts: 717 |
![]() Edited 2022-01-29 09:25 by hitsware2 my site |
||||
Andrew_G Guru ![]() Joined: 18/10/2016 Location: AustraliaPosts: 868 |
Hi all, I needed an analogue clock with a face etc so I fiddled with Peter's code and came up with the following. (it will form part of a larger Weather Station program). Cheers, Andrew 'Analog.BAS ' 'Based on Peter Mather's version of 28 Jan 2022 'https://www.thebackshed.com/forum/ViewTopic.php?TID=14512&PID=181192#181192 ' ' Be aware of mixed use of 'Analog' and 'Analogue' (as per the PicoMite manual) . . . ' 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 small=MM.HRes:If MM.VRes < small Then small=MM.VRes 'For either portrait or landscape ' Set the position and size of the analogue clock dial (can move up/down etc.) Const ana.x = MM.HRes\2 'Centre of the analogue clock Const ana.y = MM.VRes\2 ' " Const ana.r = small\2 'radius " ' Set the colours Const c.Dial = RGB(190,190,190) 'Is a light gray Const c.h_Hand = RGB(Green) Const c.m_Hand = RGB(Green) const c.s_Hand = RGB(RED) const c.Centre = RGB(Black) RTC gettime 'Assumes an RTC exists - comment-out this line if not - then set the time! SETTICK 1000, NewSecond, 1 'Used to update the analog clock every second SetTick 600000,timecorrect, 2 'Every hour get the correct time from the RTC - comment out if no RTC secs=Val(Right$(Time$,2)) mins=Val(Mid$(Time$,4,2)) hours=Val(Left$(Time$,2)) PrepareAnalog hands(secs,mins,hours, ana.r, ana.x, ana.y) 'Main Loop MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM Do 'Do whatever in the meantime loop 'mmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmm End ' ' Sub timecorrect RTC gettime 'If no RTC comment this out End Sub SUB NewSecond 'Each second do something ' "Don't hang around in interupts!" ' Do whatever every second ' 'Analogue clock - needs to be invoked every second for the sweep hand secs=Val(Right$(Time$,2)) mins=Val(Mid$(Time$,4,2)) hours=Val(Left$(Time$,2)) hands(secs,mins,hours,ana.r,ana.x, ana.y) 'Draw the hands END SUB 'NewSecond Sub hands(seconds , minutes , hours , size , x , y ) Local integer x1,y1,x2,y2,x0,y0,i Local float angle=seconds*6 rotate(2,c.s_Hand,angle,x,y,-3,50,3,50,-3,-size*0.87) 'Second hand 'make up the second hand with two triangles rotate(3,c.s_Hand,angle,x,y,3,-size*0.87,3,50,-3,-size*0.87) angle=minutes*6 + seconds/10 rotate(0,c.m_Hand,angle,x,y,-size/15,0,size/15,0,0,-size*0.85)'Minute hand angle=hours*30 + minutes/2 rotate(1,c.h_Hand,angle,x,y,-size/12,0,size/12,0,0,-size*0.6) 'Hour hand Triangle xx0(), yy0(), xx1(), yy1(), xx2(), yy2() ,tcol(), tcol() Hour_Text 'Refresh text overwritten by the hand(s) Circle x,y, size\12, 0, , c.S_Hand, c.S_Hand) Circle x,y, size\25, 0, , 0, 0 Circle x,y, size\20, 0, , c.Centre, c.Centre End Sub Sub rotate(n , col , angle As float, x ,y , x0 , y0 , x1 , y1 r, x2 , y2 r) Local float sine=Sin(Rad(angle)),cosine=Cos(Rad(angle)) 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 Sub PrepareAnalog Local Integer II Local Float i, y, xx, yy, r1, r2, tim cls Circle ana.x, ana.y, ana.r, 5, 1, c.Dial ', rgb(Black) 'Draw dial and 'erase inside it For II = 0 To 59 If II Mod 15 = 0 Then For y = -2 To +2 tim = (II + y/10) : xx = Cos(Rad(tim * 6)) : yy = Sin(Rad(tim * 6)) r1 = ana.r - 20 : r2 = ana.r - 5 Line ana.x + xx*r1, ana.y - yy*r1, ana.x + xx*r2, ana.y - yy*r2, 1, c.Dial Next y ElseIf II Mod 5 = 0 Then For y = -2 To +2 tim = (II + y/10) : xx = Cos(Rad(tim * 6)) : yy = Sin(Rad(tim * 6)) r1 = ana.r - 15 : r2 = ana.r - 5 Line ana.x + xx*r1, ana.y - yy*r1, ana.x + xx*r2, ana.y - yy*r2, 1, c.Dial Next y Else tim = II : xx = Cos(Rad(tim * 6)) : yy = Sin(Rad(tim * 6)) r1 = ana.r - 10 : r2 = ana.r - 5 Line ana.x + xx*r1, ana.y - yy*r1, ana.x + xx*r2, ana.y - yy*r2, 1, c.Dial EndIf Next II End Sub 'PrepareAnalog Sub Hour_Text text ana.x, ana.y-ana.r+30, "12", CM, 2,1, c.Dial text ana.x+ana.r-30, ana.y+2, "3", CM, 2,1, c.Dial text ana.x, ana.y+ana.r-30, "6", CM, 2,1, c.Dial text ana.x-ana.r+30, ana.y+2, "9", CM, 2,1, c.Dial End Sub 'Hour_Text Edited 2022-07-02 15:55 by Andrew_G |
||||
Andrew_G Guru ![]() Joined: 18/10/2016 Location: AustraliaPosts: 868 |
Here is how it looks. {Edit: I've just noticed that my LCD is not centered. A -3 reduction in the ana.X and ana.r fixes it. YMMV} Edited 2022-07-02 16:40 by Andrew_G |
||||
![]() |
![]() |
The Back Shed's forum code is written, and hosted, in Australia. | © JAQ Software 2025 |