Display Second Hand
In this lesson we will use a bit of trigonometry to display a second hand
that ticks every second.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41 | from machine import Pin, SPI
from utime import sleep, localtime
import math
import gc9a01
# this uses the standard Dupont ribbon cable spanning rows 4-9 on our breadboard
SCK_PIN = 2 # row 4
SDA_PIN = 3
DC_PIN = 4
CS_PIN = 5
# GND is row 8
RST_PIN = 6
# define the SPI intrface
spi = SPI(0, baudrate=60000000, sck=Pin(SCK_PIN), mosi=Pin(SDA_PIN))
tft = gc9a01.GC9A01(spi, 240, 240, reset=Pin(RST_PIN, Pin.OUT),
cs=Pin(CS_PIN, Pin.OUT), dc=Pin(DC_PIN, Pin.OUT), rotation=0
)
tft.init()
CENTER = 120
HAND_LENGTH = 100
# our counter will range from 0 to 59
# A full circle is 2*Pi radians
TWO_PI = 3.145175*2
counter = 0
white = gc9a01.color565(255, 255, 255)
while True:
radians = (counter/60)*TWO_PI
x = int(math.sin(radians)*HAND_LENGTH)
y = -int(math.cos(radians)*HAND_LENGTH)
print(radians, x, y)
tft.fill(0)
tft.line(CENTER, CENTER, CENTER+x,CENTER+y, white)
sleep(1)
counter += 1
# if we are at 60 we start over
if counter > 59:
counter = 0
|