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
;simpleanalogueclockforgraphiclcdonuno#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_EXTENDEDFONTSET1GLCDfntDefaultsize=2GLCDRotate(landscape)GLCDCLSILI9341_BLACK;dimradius,xcentre,ycentre,xend,yendasbytedimangle,seconds,minutes,hours,handcolourasword;drawdialforangle=0to359step6xend=160+210*sin(angle)/255yend=120-210*cos(angle)/255pset(xend,yend,ILI9341_BLUE);secondmarksnextangle;forangle=0to359step30xend=160+218*sin(angle)/255yend=120-218*cos(angle)/255pset(xend,yend,ILI9341_YELLOW);5minutemarksnextangle;radius=200xcentre=160ycentre=120hours=2*30;setclocktoHours2:Minutes15:Seconds50minutes=15*6seconds=0*6hours=hours+(minutes/72)*6;sethoursto1/5hourieminuteforsmoothmovementCircle(160,120,90,ILI9341_RED);;startmaindoangle=secondshandcolour=ILI9341_BLACKradius=200handseconds=seconds+6ifseconds>=359thenseconds=0angle=minuteshandcolour=ILI9341_BLACKhandminutes=minutes+6endif;;ifminutes>=359thenminutes=0angle=hourshandcolour=ILI9341_BLACKhandhours=hours+6;nudgehourhandendif;ifhours>=359thenhours=0handcolour=ILI9341_BLACKhandendif;angle=secondshandcolour=ILI9341_GREENhandangle=minuteshandcolour=ILI9341_SILVERhandif((minutes=72)or(minutes=144)or(minutes=216)or(minutes=288)or(minutes=360))and(seconds=0)thenangle=hourshandcolour=ILI9341_BLACKhandhours=hours+6;nudgehourhandendifangle=hourshandcolour=ILI9341_FUCHSIAhandwait998msloop;subhandxend=xcentre+radius*sin(angle)/255yend=ycentre-radius*cos(angle)/255line(xcentre,ycentre,xend,yend,handcolour)endsub
edited for smoother hour hand
Last edit: stan cartwright 2017-07-09
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
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?
;yetanothersimpleanalogeclockforgraphiclcdonuno#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_EXTENDEDFONTSET1GLCDfntDefaultsize=2GLCDRotate(landscape)GLCDCLSILI9341_BLACK;dimradius,xcentre,ycentre,xend,yendasbytedimangle,seconds,minutes,oldseconds,oldminutes,minstmp,hours,oldhours,handcolourasword;drawdialCircle(160,120,90,ILI9341_green)forangle=0to359step6xend=160+210*sin(angle)/255yend=120-210*cos(angle)/255pset(xend,yend,ILI9341_yellow);secondmarksnextangle;forangle=0to359step30xend=160+218*sin(angle)/255yend=120-218*cos(angle)/255pset(xend,yend,ILI9341_white);5minutemarksnextangle;radius=200xcentre=160ycentre=120hours=11*30;setclocktoHours11:Minutes58:Seconds50minutes=58*6;hours*30:minutes*6:seconds*6seconds=50*6hours=hours+(minutes/72)*6;nudgehourhandtominutedivision;;startmaindooldseconds=secondsseconds=seconds+6ifseconds=360thenseconds=0oldminutes=minutesminutes=minutes+6minstmp=minstmp+6ifminutes=360thenminutes=0endififminstmp>=72Then;1/5thcircleminstmp=0oldhours=hourshours=hours+6;increasehourhandby1minutedivisionevery12minutesifhours=360Thenhours=0endifendifendif;eraseoldhandsangle=oldsecondshandcolour=ILI9341_blackradius=200hand;angle=oldminuteshandcolour=ILI9341_blackradius=180hand;angle=oldhourshandcolour=ILI9341_blackradius=150hand;drawupdatedhandsangle=secondshandcolour=ILI9341_GREENradius=200hand;angle=minuteshandcolour=ILI9341_SILVERradius=180hand;angle=hourshandcolour=ILI9341_FUCHSIAradius=150hand;wait998ms;adjustfortimekeepingaccuracyloop;subhandxend=xcentre+radius*sin(angle)/255yend=ycentre-radius*cos(angle)/255line(xcentre,ycentre,xend,yend,handcolour)endsub
Last edit: stan cartwright 2017-08-05
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
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
edited for smoother hour hand
Last edit: stan cartwright 2017-07-09
Code looks good.
Great to see the Trig library in use!
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.
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?
Last edit: stan cartwright 2017-08-05