Menu

Simple analogue glcd clock

2017-07-08
2017-07-08
  • stan cartwright

    stan cartwright - 2017-07-08

    I wanted to draw some analogue dials and couldn't remember trig. I tried a clock first. The video is flat out-no delay.in code. Please point out any errors. I managed not to use Pi :) https://youtu.be/axFpz4W6CzE

    ;simple analogue clock for graphic lcd on uno
    #chip mega328p, 16
    #option explicit
    #include <glcd.h>
    #INCLUDE <TRIG2PLACES.H>
    ;
    #define GLCD_TYPE GLCD_TYPE_ILI9341
    #define GLCD_DC   portb.2
    #define GLCD_CS   portd.7
    #define GLCD_RESET   portd.4 ; Reset line Tie high..not needed
    #define GLCD_DO   portb.3 ;  MOSI SDI
    #define GLCD_SCK   portb.5 ;    SCK
    #define ILI9341_HardwareSPI    ' remove/comment out if you want to use software SPI.
    #define GLCD_EXTENDEDFONTSET1
    GLCDfntDefaultsize = 2
    GLCDRotate (landscape)
    GLCDCLS ILI9341_BLACK
    ;
    dim radius,xcentre,ycentre,xend,yend as byte
    dim angle,seconds,minutes,hours,handcolour as word
    ;draw dial
    for angle= 0 to 359 step 6
      xend = 160 + 210  * sin (angle)/255
      yend = 120 - 210  * cos (angle)/255
      pset (xend,yend,ILI9341_BLUE) ;second marks
    next angle
    ;
    for angle= 0 to 359 step 30
      xend = 160 + 218  * sin (angle)/255
      yend = 120 - 218  * cos (angle)/255
      pset (xend,yend,ILI9341_YELLOW) ;5 minute marks
    next angle
    ;
    radius=200
    xcentre=160
    ycentre=120
    hours=2*30 ;set clock  to Hours 2:Minutes 15:Seconds 50
    minutes=15*6
    seconds=0*6
    hours=hours+(minutes/72)*6 ;set hours to 1/5 hour ie minute for smooth movement
    Circle (160,120,90,ILI9341_RED)
    ;
    ;start main
    do
      angle=seconds
      handcolour=ILI9341_BLACK
      radius=200
      hand
      seconds=seconds+6
      if seconds>=359 then
        seconds=0
        angle=minutes
        handcolour=ILI9341_BLACK
        hand
        minutes=minutes+6
      end if
      ;
    
      ;
      if minutes>=359 then
        minutes=0
        angle=hours
        handcolour=ILI9341_BLACK
        hand
        hours=hours+6; nudge hour hand
      end if
      ;
      if hours>=359 then
        hours=0
        handcolour=ILI9341_BLACK
        hand
      end if
      ;
      angle=seconds
      handcolour=ILI9341_GREEN
      hand
      angle=minutes
      handcolour=ILI9341_SILVER
      hand
      if ((minutes=72) or (minutes=144) or (minutes=216) or (minutes=288) or (minutes=360)) and (seconds=0) then
        angle=hours
        handcolour=ILI9341_BLACK
        hand
        hours=hours+6 ; nudge hour hand
      end if
      angle=hours
      handcolour=ILI9341_FUCHSIA
      hand
      wait 998 ms
    loop
    ;
    sub hand
      xend = xcentre + radius  * sin (angle)/255
      yend = ycentre - radius  * cos (angle)/255
      line (xcentre,ycentre,xend,yend,handcolour)
    end sub
    

    edited for smoother hour hand

     

    Last edit: stan cartwright 2017-07-09
  • Anobium

    Anobium - 2017-07-09

    Code looks good.

    Great to see the Trig library in use!

     
  • Anobium

    Anobium - 2017-07-09

    It would be cool is to be able to port to other GLCDs.

    Could you use constants like GLCD_WIDTH to set xcentre? So, use xcentre = GLCD_WIDTH/2 then we can simply port.

     
  • stan cartwright

    stan cartwright - 2017-08-05

    I couldn't figure out making the hour hand move 6 degrees ie minute divisions with my original clock so I re-wrote it. There's enough info for it to be used on a small mono-chrome display. Speeded up it looks like it works. There's loads of clocks on youtube. Dunno why. Here's mine https://youtu.be/Uxxo2zDLM-o
    edit I was thinking... if %minutes/72=0 then hours=hours+6 ie minutes is on a 1/5th circle ?? is that better way?

    ;yet another simple analoge clock for graphic lcd on uno
    #chip mega328p, 16
    #option explicit
    #include <glcd.h>
    #INCLUDE <TRIG2PLACES.H>
    ;
    #define GLCD_TYPE GLCD_TYPE_ILI9341
    #define GLCD_DC   portb.2
    #define GLCD_CS   portd.7
    #define GLCD_RESET   portd.4 ; Reset line Tie high..not needed
    #define GLCD_DO   portb.3 ;  MOSI SDI
    #define GLCD_SCK   portb.5 ;    SCK
    #define ILI9341_HardwareSPI    ' remove/comment out if you want to use software SPI.
    #define GLCD_EXTENDEDFONTSET1
    GLCDfntDefaultsize = 2
    GLCDRotate (landscape)
    GLCDCLS ILI9341_BLACK
    ;
    dim radius,xcentre,ycentre,xend,yend as byte
    dim angle,seconds,minutes,oldseconds,oldminutes,minstmp,hours,oldhours,handcolour as word
    ;draw dial
    Circle (160,120,90,ILI9341_green)
    for angle= 0 to 359 step 6
      xend = 160 + 210  * sin (angle)/255
      yend = 120 - 210  * cos (angle)/255
      pset (xend,yend,ILI9341_yellow) ;second marks
    next angle
    ;
    for angle= 0 to 359 step 30
      xend = 160 + 218  * sin (angle)/255
      yend = 120 - 218  * cos (angle)/255
      pset (xend,yend,ILI9341_white) ;5 minute marks
    next angle
    ;
    radius=200
    xcentre=160
    ycentre=120
    hours=11*30 ;  set clock  to Hours 11:Minutes 58:Seconds 50
    minutes=58*6 ; hours*30:minutes*6:seconds*6
    seconds=50*6
    hours=hours+(minutes/72)*6 ; nudge hour hand to minute division
    ;
    ;start main
    do
      oldseconds=seconds
      seconds=seconds+6
      if seconds=360 then
        seconds=0
        oldminutes=minutes
        minutes=minutes+6
        minstmp=minstmp+6
        if minutes=360 then
          minutes=0
        end if
        if minstmp>=72 Then ;1/5th circle
          minstmp=0
          oldhours=hours
          hours=hours+6 ;increase hour hand by 1 minute division every 12 minutes
          if hours=360 Then
            hours=0
          end if
        end if
      end if
    ;erase old hands
      angle=oldseconds
      handcolour=ILI9341_black
      radius=200
      hand
    ;
      angle=oldminutes
      handcolour=ILI9341_black
      radius=180
      hand
    ;
      angle=oldhours
      handcolour=ILI9341_black
      radius=150
      hand
    ;draw updated hands
      angle=seconds
      handcolour=ILI9341_GREEN
      radius=200
      hand
    ;
      angle=minutes
      handcolour=ILI9341_SILVER
      radius=180
      hand
    ;
      angle=hours
      handcolour=ILI9341_FUCHSIA
      radius=150
      hand
    ;  wait 998 ms ;adjust for time keeping accuracy
    loop
    ;
    sub hand
      xend = xcentre + radius  * sin (angle)/255
      yend = ycentre - radius  * cos (angle)/255
      line (xcentre,ycentre,xend,yend,handcolour)
    end sub
    
     

    Last edit: stan cartwright 2017-08-05

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.