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