code.py
  1  # SPDX-FileCopyrightText: 2018 Kattni Rembor for Adafruit Industries
  2  #
  3  # SPDX-License-Identifier: MIT
  4  
  5  """CircuitPython Essentials DotStar example"""
  6  import time
  7  from rainbowio import colorwheel
  8  import adafruit_dotstar
  9  import board
 10  
 11  num_pixels = 30
 12  pixels = adafruit_dotstar.DotStar(board.A1, board.A2, num_pixels, brightness=0.1, auto_write=False)
 13  
 14  
 15  def color_fill(color, wait):
 16      pixels.fill(color)
 17      pixels.show()
 18      time.sleep(wait)
 19  
 20  
 21  def slice_alternating(wait):
 22      pixels[::2] = [RED] * (num_pixels // 2)
 23      pixels.show()
 24      time.sleep(wait)
 25      pixels[1::2] = [ORANGE] * (num_pixels // 2)
 26      pixels.show()
 27      time.sleep(wait)
 28      pixels[::2] = [YELLOW] * (num_pixels // 2)
 29      pixels.show()
 30      time.sleep(wait)
 31      pixels[1::2] = [GREEN] * (num_pixels // 2)
 32      pixels.show()
 33      time.sleep(wait)
 34      pixels[::2] = [TEAL] * (num_pixels // 2)
 35      pixels.show()
 36      time.sleep(wait)
 37      pixels[1::2] = [CYAN] * (num_pixels // 2)
 38      pixels.show()
 39      time.sleep(wait)
 40      pixels[::2] = [BLUE] * (num_pixels // 2)
 41      pixels.show()
 42      time.sleep(wait)
 43      pixels[1::2] = [PURPLE] * (num_pixels // 2)
 44      pixels.show()
 45      time.sleep(wait)
 46      pixels[::2] = [MAGENTA] * (num_pixels // 2)
 47      pixels.show()
 48      time.sleep(wait)
 49      pixels[1::2] = [WHITE] * (num_pixels // 2)
 50      pixels.show()
 51      time.sleep(wait)
 52  
 53  
 54  def slice_rainbow(wait):
 55      pixels[::6] = [RED] * (num_pixels // 6)
 56      pixels.show()
 57      time.sleep(wait)
 58      pixels[1::6] = [ORANGE] * (num_pixels // 6)
 59      pixels.show()
 60      time.sleep(wait)
 61      pixels[2::6] = [YELLOW] * (num_pixels // 6)
 62      pixels.show()
 63      time.sleep(wait)
 64      pixels[3::6] = [GREEN] * (num_pixels // 6)
 65      pixels.show()
 66      time.sleep(wait)
 67      pixels[4::6] = [BLUE] * (num_pixels // 6)
 68      pixels.show()
 69      time.sleep(wait)
 70      pixels[5::6] = [PURPLE] * (num_pixels // 6)
 71      pixels.show()
 72      time.sleep(wait)
 73  
 74  
 75  def rainbow_cycle(wait):
 76      for j in range(255):
 77          for i in range(num_pixels):
 78              rc_index = (i * 256 // num_pixels) + j
 79              pixels[i] = colorwheel(rc_index & 255)
 80          pixels.show()
 81          time.sleep(wait)
 82  
 83  
 84  RED = (255, 0, 0)
 85  YELLOW = (255, 150, 0)
 86  ORANGE = (255, 40, 0)
 87  GREEN = (0, 255, 0)
 88  TEAL = (0, 255, 120)
 89  CYAN = (0, 255, 255)
 90  BLUE = (0, 0, 255)
 91  PURPLE = (180, 0, 255)
 92  MAGENTA = (255, 0, 20)
 93  WHITE = (255, 255, 255)
 94  
 95  while True:
 96      # Change this number to change how long it stays on each solid color.
 97      color_fill(RED, 0.5)
 98      color_fill(YELLOW, 0.5)
 99      color_fill(ORANGE, 0.5)
100      color_fill(GREEN, 0.5)
101      color_fill(TEAL, 0.5)
102      color_fill(CYAN, 0.5)
103      color_fill(BLUE, 0.5)
104      color_fill(PURPLE, 0.5)
105      color_fill(MAGENTA, 0.5)
106      color_fill(WHITE, 0.5)
107  
108      # Increase or decrease this to speed up or slow down the animation.
109      slice_alternating(0.1)
110  
111      color_fill(WHITE, 0.5)
112  
113      # Increase or decrease this to speed up or slow down the animation.
114      slice_rainbow(0.1)
115  
116      time.sleep(0.5)
117  
118      # Increase this number to slow down the rainbow animation.
119      rainbow_cycle(0)