Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python3
- # Filename: pico_dutycycle_pulse.py
- # Author: Jeoi Reqi
- # Pulse and Fade the LED on a Raspberry Pi Pico Microcontroller running Adafruit Circuit Python & Asyncio using Duty Cycle.
- from time import sleep
- from board import *
- import pwmio
- import asyncio
- import microcontroller
- led = pwmio.PWMOut(LED, frequency=5000, duty_cycle=0)
- def led_pwm_up(led):
- for i in range(100):
- # PWM LED up and down
- if i < 50:
- led.duty_cycle = int(i * 2 * 65535 / 100) # Up
- sleep(0.001)
- def led_pwm_down(led):
- for i in range(101):
- # PWM LED up and down
- if i >= 50:
- led.duty_cycle = 65535 - int((i - 50) * 2 * 65535 / 100) # Down
- sleep(0.002)
- async def pulse_pico_led(led):
- print("\n\t~Starting Duty Cycle Test With LED Pulse~\n")
- led_state = True
- counter = 0
- result_count = 0
- while True:
- if led_state:
- print("\n\n\tFading LED On:\n\n")
- for i in range(101):
- brightness_pct = i
- duty_cycle = int(i * 65535 / 100)
- led.duty_cycle = duty_cycle
- await asyncio.sleep(0.025)
- print("{:6d}".format(led.duty_cycle), end="")
- result_count += 1
- if result_count >= 12:
- print("\n")
- result_count = 0
- led_state = False
- else:
- print("\n\n\n\tFading LED Off:\n\n")
- for i in range(100, -1, -1):
- brightness_pct = i
- duty_cycle = int(i * 65535 / 100)
- led.duty_cycle = duty_cycle
- await asyncio.sleep(0.025)
- print("{:6d}".format(led.duty_cycle), end="")
- result_count += 1
- if result_count >= 12:
- print("\n")
- result_count = 0
- led_state = True
- counter += 1
- if counter >= 2:
- print("\n\n\t\t\t\t\t\t\t\t Done!")
- led.duty_cycle = 0
- break
- await asyncio.sleep(0)
- asyncio.run(pulse_pico_led(led))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement