Created
February 7, 2025 03:39
-
-
Save TTZPlayz/e59a9de695e39c9f05582565d326b450 to your computer and use it in GitHub Desktop.
I completed this in class to swap through the colors of the rainbow when the switch is on.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import time | |
import board | |
import neopixel | |
from digitalio import DigitalInOut, Direction, Pull | |
switch = DigitalInOut(board.D3) | |
switch.direction = Direction.INPUT | |
switch.pull = Pull.UP | |
pixel_pin = board.D2 | |
num_pixels = 4 | |
pixels = neopixel.NeoPixel(pixel_pin, num_pixels, brightness=0.3, auto_write=False) | |
RED = (255, 0, 0) | |
ORANGE = (255, 100, 0) | |
YELLOW = (255, 150, 0) | |
GREEN = (0, 255, 0) | |
CYAN = (0, 255, 255) | |
BLUE = (0, 0, 255) | |
PURPLE = (180, 0, 255) | |
PINK = (255, 100, 100) | |
WHITE = (255, 255, 255) | |
OFF = (0, 0, 0) | |
colors = [RED, ORANGE, YELLOW, GREEN, CYAN, BLUE, PURPLE, PINK, WHITE] | |
now = 0 | |
while True: | |
print(switch.value) | |
if (switch.value == True): #detect the button press | |
now = now + 1 | |
if (now >= 9): #9 colors in the list | |
now = 0 | |
pixels.fill(colors[now]) | |
pixels.show() | |
time.sleep(0.12) # debounce delay |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment