import time
import machine as board
#import busio
#from adafruit_ht16k33.segments import Seg7x4
from digitalio import DigitalInOut, Direction, Pull
import tm1637
from adafruit_motor import servo
# create bug button press
btn = DigitalInOut(board.GP20)
btn.direction = Direction.INPUT
btn.pull = Pull.UP
prev_state = btn.value
#display = Seg7x4(busio.I2C(board.GP1, board.GP0))
display = tm1637.TM1637(clk=board.Pin(16), dio=board.Pin(17))
display.brightness = 0.1
num = 0
# create a PWMOut object on Pin A2.
pwm = pwmio.PWMOut(board.A2, duty_cycle=2 ** 15, frequency=50)
my_servo = servo.Servo(pwm)
angle = 0
goingup = 1
increment = 2
wait = 0.8
def do_button():
cur_state = btn.value
if cur_state != prev_state:
if not cur_state:
print("BTN is down")
num+=1
else:
print("BTN is up")
prev_state = cur_state
#time.sleep(0.01) # sleep for debounce
while True:
#display.print(f"000{num}")
do_button()
if angle < 180 and goingup:
angle+=increment
elif angle > 0 and not goingup:
angle-=increment
elif angle == 180:
goingup = 0
time.sleep(wait)
elif angle == 0:
goingup = 1
time.sleep(wait)
my_servo.angle = angle
display.print(f"000{angle}")
#print(angle)