Kết nối phần cứng
Viết code
# Example using PWM to fade an LED. import utime, _thread from machine import Pin, PWM led_red = PWM(Pin(14)) pwm_red_width = 65536 pwm_red_dir = True led_green = PWM(Pin(13)) pwm_green_width = 65536 pwm_green_dir = True led_yellow = PWM(Pin(12)) pwm_yellow_width = 65536 pwm_yellow_dir = True led_red.freq(50) led_green.freq(50) led_yellow.freq(50) is_change = True button = Pin(17, Pin.IN) last_time_stamp = utime.ticks_us() def button_callback(pin): global is_change, last_time_stamp if utime.ticks_us() - last_time_stamp > 300000: last_time_stamp = utime.ticks_us() is_change = not is_change _thread.start_new_thread(task, (50, 0.05)) button.irq(trigger = Pin.IRQ_FALLING, handler = button_callback) led_25 = PWM(Pin(25)) led_25.freq(50) def task(n, delay): global led_25 for duty in range(0, 65536, +80): led_25.duty_u16(duty) utime.sleep(0.001) for duty in range(65536, 0, -80): led_25.duty_u16(duty) utime.sleep(0.001) led_25.duty_u16(0) while True: if is_change != True: utime.sleep(0.01) continue if pwm_red_dir: pwm_red_width += 20 else: pwm_red_width -= 20 if pwm_green_dir: pwm_green_width += 50 else: pwm_green_width -= 50 if pwm_yellow_dir: pwm_yellow_width += 100 else: pwm_yellow_width -= 100 if pwm_red_width < 0: pwm_red_dir = True pwm_red_width = 0 if pwm_red_width > 65536: pwm_red_dir = False pwm_red_width = 65536 if pwm_green_width < 0: pwm_green_dir = True pwm_green_width = 0 if pwm_green_width > 65536: pwm_green_dir = False pwm_green_width = 65536 if pwm_yellow_width < 0: pwm_yellow_dir = True pwm_yellow_width = 0 if pwm_yellow_width > 65536: pwm_yellow_dir = False pwm_yellow_width = 65536 led_red.duty_u16(pwm_red_width) led_green.duty_u16(pwm_green_width) led_yellow.duty_u16(pwm_yellow_width) utime.sleep(0.001)
Còn đây là kết quả
Comments
Post a Comment