import time
from machine import Pin, PWM
import utime, math, tm1637
# pwm
pwm = PWM(Pin(2), freq=50, duty=0)
def Servo(servo, angle):
pwm.duty(int(((angle)/180 *2 + 0.5)/ 20 * 1023))
LED1 = Pin(19, Pin.OUT) #Red LED
LED2 = Pin(21, Pin.OUT) #Yellow LED
LED3 = Pin(22, Pin.OUT) #Green LED
tm = tm1637.TM1637(clk=Pin(12), dio=Pin(13))
def count_down(sec,color):
if color == 'red':
for y in range(sec,-1,-1):
tm.numbers(0,y)
time.sleep(1)
else:
for y in range(sec,-1,-1):
tm.numbers(0,y)
if y==3:
ylight(3)
time.sleep(1)
def rlight(sec):
LED1.value(1)
LED2.value(0)
LED3.value(0)
Servo(2,0)
count_down(sec,'red')
#time.sleep(sec)
def ylight(sec):
LED1.value(0)
LED2.value(1)
LED3.value(0)
def glight(sec):
LED1.value(0)
LED2.value(0)
LED3.value(1)
Servo(2,90)
count_down(sec,'green') # seconds of red light (stop!)
rtime = int(input('input red light lasts seconds:'))
gtime = int(input('input green light lasts seconds:'))
while 1:
rlight(rtime)
glight(gtime)
# ylight(2)