Home
JAQForum Ver 20.06
Log In or Join  
Active Topics
Local Time 23:20 20 Apr 2024 Privacy Policy
Jump to

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 : PicoMite "Analogue Clock"

     Page 1 of 3    
Author Message
DrifterNL

Regular Member

Joined: 27/09/2018
Location: Netherlands
Posts: 56
Posted: 11:46pm 17 Aug 2022
Copy link to clipboard 
Print this post

While watching a youtube video about a guy coding an analogue clock on a led matrix I got the idea to try coding an analogue clock on the picomite for an SPI LCD panel.
My setup is a PicoMite with an I2C RTC and an ST7789 320 x 240 SPI panel.
After a few hours of trial and tweaking I came up with the following:




The clock is scalable and colours customizable.
It was a fun little project and a bit of practice for me.


Option explicit

Dim cr,cd,dd
Dim xc,yc,x1,y1,x2,y2
Dim xh,yh,xm,ym,xs,ys
Dim xho,yho,xmo,ymo,xso,yso
Dim hour,min,sec
Dim csl,hhl,mhl,shl
Dim chc,shc
Dim ot$

Const ccc = RGB(orange)  'clock circle colour'
Const csc = RGB(orange)  'clock spoke colour'
Const hmamc = RGB(green) 'clock hand colour for am'
Const hmpmc = RGB(blue)  'clock hand colour for pm'
Const shamc = RGB(red)   'second hand colour for am'
Const shpmc = RGB(red)   'second hand colour for pm'
Const clsc = RGB(black)  'clear screen colour'

xc = 160       'x center of spi display'
yc = 120       'y center of spi display'
cr = 110       'clock radius'
csl = cr * 0.05'clock spoke length'
hhl = cr * 0.5 'hour hand length'
mhl = cr * 0.9 'minute hand length'
shl = cr * 0.9 'second hand length'

xho = xc
yho = yc
xmo = xc
ymo = yc
xso = xc
yso = yc

CLS clsc

'----------clock circle----------'
Circle xc,yc,cr,1,1,ccc'
'For dd = 0 To 360 Step + 0.5
'x1 = Sin(Rad(dd)) * cr
'y1 = Cos(Rad(dd)) * cr
'Pixel x1+xc,y1+yc,ccc
'Next

'----------clock spokes----------'
For dd = 0 To 360 Step + 30
x1 = Sin(Rad(dd)) * (cr)
y1 = Cos(Rad(dd)) * (cr)
x2 = Sin(Rad(dd)) * (cr+csl)
y2 = Cos(Rad(dd)) * (cr+csl)
Line x1+xc,y1+yc,x2+xc,y2+yc,1,csc
Next

'----------get time from I2C RTC----------'
RTC gettime
waitloop:
If Val(Mid$(Time$,7,2)) = 0 Then GoTo waitloop

clockloop:

'----------get time values from time string----------'
ot$ = Time$
hour = Val(Mid$(ot$,1,2))
min = Val(Mid$(ot$,4,2))
sec = Val(Mid$(ot$,7,2))

'----------set clock hand colours for am pm----------'
If hour >= 12 Then
 chc = hmpmc
 shc = shpmc
Else
 chc = hmamc
 shc = shamc
EndIf

'----------hour hand----------'
If hour > 12 Then
 cd = (30 * (hour - 12)) + (0.5 * (Fix(min / 10) * 10))
 Else
 cd = (30 * hour) + (0.5 * (Fix(min / 10) * 10))
 EndIf

'clock degrees to display degrees'
'cd = clock degrees'
'dd = display degrees'
'changing the orientation from clock degrees to display degrees'
dd = cd + 180
If dd > 360 Then dd = dd - 360
'changing the direction from cw to ccw'
dd = 360 - dd

xh = Sin(Rad(dd)) * hhl
yh = Cos(Rad(dd)) * hhl


'----------minute hand----------'
cd = (6 * min) + (0.1 * (Fix(sec / 10) * 10))
dd = cd + 180
If dd > 360 Then dd = dd - 360
dd = 360 - dd
xm = Sin(Rad(dd)) * mhl
ym = Cos(Rad(dd)) * mhl


'----------second hand----------'
cd = 6 * sec
dd = cd + 180
If dd > 360 Then dd = dd - 360
dd = 360 - dd
xs = Sin(Rad(dd)) * shl
ys = Cos(Rad(dd)) * shl

'------clear old and display new clock hands on SPID------'
Line xc,yc,xho+xc,yho+yc,1,clsc
Line xc,yc,xmo+xc,ymo+yc,1,clsc
Line xc,yc,xso+xc,yso+yc,1,clsc

Line xc,yc,xh+xc,yh+yc,1,chc
Line xc,yc,xm+xc,ym+yc,1,chc
Line xc,yc,xs+xc,ys+yc,1,shc

'-------save old data for clearing clock hands on next round-------'
xho = xh
yho = yh
xmo = xm
ymo = ym
xso = xs
yso = ys

'-----wait for time to change to prevent screen flicker-----'
waittimechange:
If ot$ = Time$ Then GoTo waittimechange

GoTo clockloop

Floating Point Keeps Sinking Me!
Back To Integer So I Don't Get Injured.
 
Bleep
Guru

Joined: 09/01/2022
Location: United Kingdom
Posts: 409
Posted: 09:27am 18 Aug 2022
Copy link to clipboard 
Print this post

Hi,
Have a look at this if you like clocks. :-)
Link to post.
If you get to the end of the post, there is a video and sources, of various clocks.
Post with video.
Regards.
Edited 2022-08-18 19:30 by Bleep
 
Zett

Newbie

Joined: 18/06/2022
Location: Netherlands
Posts: 14
Posted: 05:32am 19 Aug 2022
Copy link to clipboard 
Print this post

Wha! great! nice! gona try your code to!
 
stanleyella

Guru

Joined: 25/06/2022
Location: United Kingdom
Posts: 1639
Posted: 06:06pm 19 Aug 2022
Copy link to clipboard 
Print this post

Nice!
 
homa

Senior Member

Joined: 05/11/2021
Location: Germany
Posts: 238
Posted: 02:35pm 23 Aug 2022
Copy link to clipboard 
Print this post

welcome to the club of analogue watch friends

look also here:
simple clock_22

regards
matthias
 
stanleyella

Guru

Joined: 25/06/2022
Location: United Kingdom
Posts: 1639
Posted: 07:46pm 23 Aug 2022
Copy link to clipboard 
Print this post

I had to comment

'----------get time from I2C RTC----------'
'RTC gettime
'waitloop:
'If Val(Mid$(Time$,7,2)) = 0 Then GoTo waitloop

Because I do not have it and gave an error. It now works.
clocks are a good way of learning mmbasic trigonometry.

worked picomite ili9341
 
stanleyella

Guru

Joined: 25/06/2022
Location: United Kingdom
Posts: 1639
Posted: 08:53pm 23 Aug 2022
Copy link to clipboard 
Print this post

@DrifterNL
Another bread board user , It is fast and you can fix mistakes easier than soldered vero board, You do not need a pcb to have fun.
 
DrifterNL

Regular Member

Joined: 27/09/2018
Location: Netherlands
Posts: 56
Posted: 10:44pm 23 Aug 2022
Copy link to clipboard 
Print this post

Thanks all!
Love the other clock projects as well!


  stanleyella said  @DrifterNL
Another bread board user , It is fast and you can fix mistakes easier than soldered vero board, You do not need a pcb to have fun.


True, but if I may make a suggestion, use solid core jumpers for critical devices like an SD card reader otherwise one wrong move and files / data can be corrupted.

Here is my test set-up:



Floating Point Keeps Sinking Me!
Back To Integer So I Don't Get Injured.
 
stanleyella

Guru

Joined: 25/06/2022
Location: United Kingdom
Posts: 1639
Posted: 01:24am 24 Aug 2022
Copy link to clipboard 
Print this post

I am going to make this on strip board. my picomite and lcd.
It looks like a birds nest.

 
stanleyella

Guru

Joined: 25/06/2022
Location: United Kingdom
Posts: 1639
Posted: 01:24am 24 Aug 2022
Copy link to clipboard 
Print this post

I am going to make this on strip board. my picomite and lcd.
It looks like a birds nest.

 
Volhout
Guru

Joined: 05/03/2018
Location: Netherlands
Posts: 3496
Posted: 04:27pm 28 Aug 2022
Copy link to clipboard 
Print this post

Simple clock

'Simple analog clock
'uses picomite OPTION RTC AUT ENABLE, so time$ is accurate
'expects 320x240 display (either VGA or ILI9341)

'dimensions
option default integer
x0=160
y0=120
r0=110:rc=r0+2
r1=90
r2=60

'clock face
cls
circle x0,y0,rc,1,1,rgb(white)
for s=0 to 55 step 5
 line x0+rc*sin(-pi*s/30),y0+rc*cos(pi*s/30),x0+(rc+5)*sin(-pi*s/30),y0+(rc+5)*cos(pi*s/30),1,rgb(white)
next s

'read time and show hands
do
 do:loop while t$=time$ ' wait for seconds to pass
 c1=rgb(black):c2=c1
 drawclock   'draw hands in black = erase
 t$=time$
 h=val(left$(t$,2))
 m=val(mid$(t$,4,2))
 s=val(right$(t$,2))
 c1=rgb(yellow):c2=rgb(green)
 drawclock   'draw new hands
 print time$
loop while inkey$=""

'math for the hands
sub drawclock
 line x0,y0,x0+r0*sin(-pi*s/30),y0+r0*cos(pi*s/30),1,c1
 line x0,y0,x0+r1*sin(-pi*m/30),y0+r1*cos(pi*m/30),1,c2
 line x0,y0,x0+r2*sin(-pi*h/6),y0+r2*cos(pi*h/6),1,c2
end sub

PicomiteVGA PETSCII ROBOTS
 
stanleyella

Guru

Joined: 25/06/2022
Location: United Kingdom
Posts: 1639
Posted: 05:53pm 28 Aug 2022
Copy link to clipboard 
Print this post

  Volhout said  Simple clock

'Simple analog clock
'uses picomite OPTION RTC AUT ENABLE, so time$ is accurate
'expects 320x240 display (either VGA or ILI9341)

'dimensions
option default integer
x0=160
y0=120
r0=110:rc=r0+2
r1=90
r2=60

'clock face
cls
circle x0,y0,rc,1,1,rgb(white)
for s=0 to 55 step 5
 line x0+rc*sin(-pi*s/30),y0+rc*cos(pi*s/30),x0+(rc+5)*sin(-pi*s/30),y0+(rc+5)*cos(pi*s/30),1,rgb(white)
next s

'read time and show hands
do
 do:loop while t$=time$ ' wait for seconds to pass
 c1=rgb(black):c2=c1
 drawclock   'draw hands in black = erase
 t$=time$
 h=val(left$(t$,2))
 m=val(mid$(t$,4,2))
 s=val(right$(t$,2))
 c1=rgb(yellow):c2=rgb(green)
 drawclock   'draw new hands
 print time$
loop while inkey$=""

'math for the hands
sub drawclock
 line x0,y0,x0+r0*sin(-pi*s/30),y0+r0*cos(pi*s/30),1,c1
 line x0,y0,x0+r1*sin(-pi*m/30),y0+r1*cos(pi*m/30),1,c2
 line x0,y0,x0+r2*sin(-pi*h/6),y0+r2*cos(pi*h/6),1,c2
end sub


works but screen or is wrong, that is the screen setup- no problem.. Displays time in mmedit chat box??
first program I have seen that uses pi.

Edited 2022-08-29 03:59 by stanleyella
 
matherp
Guru

Joined: 11/12/2012
Location: United Kingdom
Posts: 8570
Posted: 05:56pm 28 Aug 2022
Copy link to clipboard 
Print this post

  Quote  expects 320x240 display


not 240x320 display
 
stanleyella

Guru

Joined: 25/06/2022
Location: United Kingdom
Posts: 1639
Posted: 06:14pm 28 Aug 2022
Copy link to clipboard 
Print this post

  matherp said  
  Quote  expects 320x240 display


not 240x320 display


I got portrait and landscape boards. cheers

 
stanleyella

Guru

Joined: 25/06/2022
Location: United Kingdom
Posts: 1639
Posted: 08:03pm 28 Aug 2022
Copy link to clipboard 
Print this post

  stanleyella said  
  Volhout said  Simple clock

'Simple analog clock
'uses picomite OPTION RTC AUT ENABLE, so time$ is accurate
'expects 320x240 display (either VGA or ILI9341)

'dimensions
option default integer
x0=160
y0=120
r0=110:rc=r0+2
r1=90
r2=60

'clock face
cls
circle x0,y0,rc,1,1,rgb(white)
for s=0 to 55 step 5
 line x0+rc*sin(-pi*s/30),y0+rc*cos(pi*s/30),x0+(rc+5)*sin(-pi*s/30),y0+(rc+5)*cos(pi*s/30),1,rgb(white)
next s

'read time and show hands
do
 do:loop while t$=time$ ' wait for seconds to pass
 c1=rgb(black):c2=c1
 drawclock   'draw hands in black = erase
 t$=time$
 h=val(left$(t$,2))
 m=val(mid$(t$,4,2))
 s=val(right$(t$,2))
 c1=rgb(yellow):c2=rgb(green)
 drawclock   'draw new hands
 print time$
loop while inkey$=""

'math for the hands
sub drawclock
 line x0,y0,x0+r0*sin(-pi*s/30),y0+r0*cos(pi*s/30),1,c1
 line x0,y0,x0+r1*sin(-pi*m/30),y0+r1*cos(pi*m/30),1,c2
 line x0,y0,x0+r2*sin(-pi*h/6),y0+r2*cos(pi*h/6),1,c2
end sub


option default integer
for s=0 to 55 step 5
 line x0+rc*sin(-pi*s/30),y0+rc*cos(pi*s/30),x0+(rc+5)*sin(-pi*s/30),y0+(rc+5)*cos(pi*s/30),1,rgb(white)
next s

pi is what, integer? sin,cos integer? I thought from other clocks they were floats.
Is pi, sin, cos read from tables?
My other basic does not have rad... and certainly not pi
 
Volhout
Guru

Joined: 05/03/2018
Location: Netherlands
Posts: 3496
Posted: 08:09pm 28 Aug 2022
Copy link to clipboard 
Print this post

Hi,

option default integer means that if you declare a new variable, it will become an integer.
PI (3.1415926535...)is not an integer, it is a pre-declared FLOAT.
SIN and COS are pre-declared FLOAT functions.

The result of multiplying "r0*SIN(...)" is in essence a FLOAT and will be converted to an integer by the LINE command (that only understands integers).

Yip, this is BASIC at it's utmost strength.... You need not worry about this kind of stuff, Basic will solve it for you.
Try this in C and you will do a lot of casting before the sun sets ..... (or ... if you get lazy, you simply declare everything a FLOAT, and do the casting in the LINE statement.....but before you try this on an arduino, know that FLOAT is really slow on the ATMEG328)
Edited 2022-08-29 06:13 by Volhout
PicomiteVGA PETSCII ROBOTS
 
stanleyella

Guru

Joined: 25/06/2022
Location: United Kingdom
Posts: 1639
Posted: 09:10pm 28 Aug 2022
Copy link to clipboard 
Print this post

  Volhout said  Hi,

option default integer means that if you declare a new variable, it will become an integer.
PI (3.1415926535...)is not an integer, it is a pre-declared FLOAT.
SIN and COS are pre-declared FLOAT functions.

The result of multiplying "r0*SIN(...)" is in essence a FLOAT and will be converted to an integer by the LINE command (that only understands integers).

Yip, this is BASIC at it's utmost strength.... You need not worry about this kind of stuff, Basic will solve it for you.
Try this in C and you will do a lot of casting before the sun sets ..... (or ... if you get lazy, you simply declare everything a FLOAT, and do the casting in the LINE statement.....but before you try this on an arduino, know that FLOAT is really slow on the ATMEG328)

Thanks, a newbee. other gcbasic .. no floats :(
I never used arduino c+/arduino ide
in mmbasic,  are trig values read from data or calculated when needed?
I have yet to try trig but it is on my list of things to do with mmbasic and lcd.
I do not know the speed difference between mmbasic floats and integers.
v%=0-v% makes it minus... where does it say that?
I am not new to basic just mmbasic which is not 8 bit pics.
 
Mixtel90

Guru

Joined: 05/10/2019
Location: United Kingdom
Posts: 5707
Posted: 09:48pm 28 Aug 2022
Copy link to clipboard 
Print this post

v%=0-v% isn't just BASIC it's ordinary arithmetic.
What is 0-5?
Mick

Zilog Inside! nascom.info for Nascom & Gemini
Preliminary MMBasic docs & my PCB designs
 
stanleyella

Guru

Joined: 25/06/2022
Location: United Kingdom
Posts: 1639
Posted: 10:17pm 28 Aug 2022
Copy link to clipboard 
Print this post

  Mixtel90 said  v%=0-v% isn't just BASIC it's ordinary arithmetic.
What is 0-5?

try that in another basic with words and bytes:) it's 225-var or 65535-var.
seems easy using mmbasic even if they are 4 byte integers.
From a coding view it is a nice way of using negative numbers and useful for graphics.
x=x+d where d can be 4 or -4
d=0-d if d=4 then d=-4
if d=-4 then d=0-(-4)=4
print 0-(-4)=4 in teraterm
nice way to toggle a vars sign
 
Volhout
Guru

Joined: 05/03/2018
Location: Netherlands
Posts: 3496
Posted: 11:59am 29 Aug 2022
Copy link to clipboard 
Print this post

  stanleyella said  
  Mixtel90 said  v%=0-v% isn't just BASIC it's ordinary arithmetic.
What is 0-5?

try that in another basic with words and bytes:) it's 225-var or 65535-var.
seems easy using mmbasic even if they are 4 byte integers.


You are confused by the fact that MMBasic uses 64bit signed integers and calculates with signed integers.
The other basics you refer to are using signed algebra on a "7 bit number + sign".
The 255 is an unsigned value, and as long as the value you subtract is less that 7 bits (i.e. decimal 32), the result is a signed 7 bit value. Similar for the 65535.

In some compilers and languages (i.e. the  official RT2040 PIO assembler) you can use values "0" and "-0",  second stands for &hfffffff...
PicomiteVGA PETSCII ROBOTS
 
     Page 1 of 3    
Print this page
© JAQ Software 2024