AsyncIO RGB Blink

This time the leds are blinked without the busy delay of the time.sleep() method.


Raspberry Breadstick AsyncIO RGB Blink
Breadstick Innovations
March 26, 2024

This code uses the DotStar library to blink 3 of the SK9822 RGB LEDs, 
without time.sleep() based delays.

from board import *
#from time import sleep
from adafruit_dotstar import DotStar
import asyncio

leds = DotStar(DOTSTAR_CLOCK, DOTSTAR_DATA, 24, brightness=0.02, auto_write=False)

RED = (255,0,0)
GREEN = (0,255,0)
BLUE = (0,0,255)
OFF = (0,0,0)


async def blinker (led_num, color, frequency):
    period = 1/frequency
    global leds
    while True:
        leds[led_num] = color
        await asyncio.sleep(period)
        leds[led_num] = (0,0,0)
        await asyncio.sleep(period)

async def led_refresh (frequency):
    period = 1/frequency
    global leds
    while True:
        await asyncio.sleep(period)
async def main():
    red = asyncio.create_task(blinker(0,RED,1))
    green = asyncio.create_task(blinker(12,GREEN,5))
    blue = asyncio.create_task(blinker(23,BLUE,9))
    refresh = asyncio.create_task(led_refresh(30))
    await red
    await green
    await blue
    await refresh

Last updated