/ NeoPixie_Dust_Bag / code.py
code.py
1 # SPDX-FileCopyrightText: 2017 Mikey Sklar for Adafruit Industries 2 # 3 # SPDX-License-Identifier: MIT 4 5 # NeoPixie Dust Bag 6 # learn.adafruit.com/neopixel-pixie-dust-bag 7 8 import time 9 10 import board 11 import digitalio 12 import neopixel 13 14 try: 15 import urandom as random # for v1.0 API support 16 except ImportError: 17 import random 18 19 neo_pin = board.D0 # DIGITAL IO pin for NeoPixel OUTPUT from GEMMA 20 touch_pin = board.D2 # DIGITAL IO pin for momentary touch sensor to GEMMA 21 pixel_count = 30 # Number of NeoPixels connected to GEMMA 22 delay_sec = .010 # delay between blinks, smaller numbers are faster 23 delay_mult = 8 # Randomization multiplier, delay speed of the effect 24 25 # initialize neopixels 26 pixels = neopixel.NeoPixel( 27 neo_pin, pixel_count, brightness=.4, auto_write=False) 28 29 oldstate = False # counting touch sensor button pushes 30 showcolor = 0 # color mode for cycling 31 32 # initialize external capacitive touch pad (active high) 33 button = digitalio.DigitalInOut(touch_pin) 34 button.direction = digitalio.Direction.INPUT 35 button.pull = digitalio.Pull.UP 36 37 while True: 38 39 rcolor = 100 # swtich cycles colors, initially GOLD 40 gcolor = 0 41 bcolor = 0 42 43 if showcolor == 0: # Garden PINK 44 rcolor = 242 45 gcolor = 90 46 bcolor = 255 47 48 elif showcolor == 1: # Pixie GOLD 49 rcolor = 255 50 gcolor = 222 51 bcolor = 30 52 53 elif showcolor == 2: # Alchemy BLUE 54 rcolor = 50 55 gcolor = 255 56 bcolor = 255 57 58 elif showcolor == 3: # Animal ORANGE 59 rcolor = 255 60 gcolor = 100 61 bcolor = 0 62 63 elif showcolor == 4: # Tinker GREEN 64 rcolor = 0 65 gcolor = 255 66 bcolor = 40 67 68 # sparkling 69 # select a random pixel 70 p = random.randint(0, (pixel_count - 2)) 71 # color value from momentary switch 72 pixels[p] = (rcolor, gcolor, bcolor) 73 pixels.write() 74 75 # delay value randomized to up to delay_mult times longer 76 time.sleep(delay_sec * random.randint(0, delay_mult)) 77 78 # set to a dimmed version of the state color 79 pixels[p] = (int(rcolor / 10), int(gcolor / 10), int(bcolor / 10)) 80 pixels.write() 81 82 # set a neighbor pixel to an even dimmer value 83 pixels[p + 1] = (int(rcolor / 15), int(gcolor / 15), int(bcolor / 15)) 84 pixels.write() 85 86 # button check to cycle through color value sets 87 # get the current button state 88 newstate = button.value 89 90 # Check if state changed from low to high (button/touchpad press). 91 if newstate and not oldstate: 92 # cycle to next color 93 showcolor += 1 94 95 # limit the cycle to the 5 colors 96 if showcolor > 4: 97 showcolor = 0 98 99 # give feedback to the REPL to debug the touch pad 100 # print("Color:", showcolor) 101 102 # Set the last button state to the old state. 103 oldstate = newstate