# PassingColors
# Reference: https://wokwi.com/projects/384675870918916097
# by xfpd: https://wokwi.com/makers/xfpd
from machine import Pin
import time
import neopixel
ws_pin = 13
NUMPIXELS = 24 # NeoPixel ring size
pixDELAY = 100 # Delay for pixel persistence
GRN_Base = (NUMPIXELS // 4)
BLU_Base = (NUMPIXELS // 2)
YEL_Base = ((NUMPIXELS * 3) // 4)
def collide():
global i
# RED pixel follows the counter (at the bottom of this function)
RED = i
# GREEN pixel starts at 1/4 (.25) position on the ring
GRN = GRN_Base - i
if (GRN < 0) :
GRN = NUMPIXELS + GRN
# BLUE pixels starts at 1/2 (.5) position on the ring
BLU = BLU_Base + i
if (BLU > NUMPIXELS - 1):
BLU = BLU - NUMPIXELS
# YELLOW pixel starts at 3/4 (.75) position on the ring
YEL = YEL_Base - i
if (YEL < 0):
YEL = abs(NUMPIXELS + YEL)
print("RED %5d | GRN %5d | BLU %5d | YEL %5d" %(RED, GRN, BLU, YEL))
pixels[RED] =(255, 0, 0)
pixels[GRN] =(0, 255, 0)
pixels[BLU] =(0, 0, 255)
pixels[YEL] =(255, 255, 0)
pixels.write()
time.sleep_ms(pixDELAY)
# BLACK-out the trailing colored pixel
pixels.fill((0, 0, 0))
pixels.write() # show the black-out
if (i < (NUMPIXELS-1)):
i = i + 1
else:
i = 0
# Start Function
if __name__ == '__main__':
pixels = neopixel.NeoPixel(Pin(ws_pin), NUMPIXELS)
i=0 # counter
while True:
collide()