🍞
Breadstick
  • Breadstick Innovations Website
  • Breadsticks
    • đŸĨ–Raspberry Breadstick
      • Code Examples
        • Demo Code
        • POV Wand
        • Pride Flags POV Wand
        • 6-Axis IMU
        • RGB Blink
        • AsyncIO RGB Blink
        • Digital Input
        • Digital Output
        • PWM
        • ADC
        • Servo Motor
    • đŸĨ–Raspberry Breadstick Lite
    • 🍞Support Boards
      • I2C Devices
        • 🚌I2C Bus Rail Adapter
        • đŸŒĻī¸Weather Crouton
        • 😎Brightness Crouton
        • 📏Distance Crouton
        • đŸĢ¨Motion Crouton
      • I2S Devices
        • đŸ“ĸBoombox
      • SPI Devices
      • đŸ•šī¸Buttons/Switches
    • Learning Resources
    • Troubleshooting
  • Nougat
    • Nougat C3
    • Nougat Quad
    • Installing WLED
  • Pico Slices
    • 🔴Slice 1 - LED Mixer
      • Assembly Guide
      • CircuitPython Code
        • 1 - Blink
        • 2 - Analog Read to Plotter
        • 3 - PWM Fade
        • 4 - Pot Controlled PWM
        • 5 - Gamma Correction
    • âąī¸Slice 2 - Stopwatch
      • Assembly Guide
      • Coding Lessons
        • 1 - 7-Segment Display Intro
        • 2 - Cycling Through All Segments
    • âŦœSlice 3 - 8x8 Dot Matrix
      • Assembly Guide
      • MicroPython Code
        • 1 - Moveable Pixel
        • 2 - Snake
    • Circuit Python Setup
    • Reset Bricked Pico
  • Christmas
    • Christmas Tree DIY kit
  • Protoboards
    • â¤ī¸Proto-Heart
    • đŸĨĒProto-Toast
  • SHOP
Powered by GitBook
On this page

Was this helpful?

  1. Breadsticks
  2. Raspberry Breadstick
  3. Code Examples

AsyncIO RGB Blink

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

Code

"""
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)

leds.fill(OFF)
leds.show()

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:
        leds.show()
        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
    
asyncio.run(main())
    

PreviousRGB BlinkNextDigital Input

Last updated 1 year ago

Was this helpful?

đŸĨ–