Skip to content

Instantly share code, notes, and snippets.

@cefn
Last active August 9, 2016 23:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cefn/a9b6ca20bae1d2be85af67044ed6cc90 to your computer and use it in GitHub Desktop.
Save cefn/a9b6ca20bae1d2be85af67044ed6cc90 to your computer and use it in GitHub Desktop.
Reference code for driving the microbit neopixel library through a rainbow effect
from microbit import *
import neopixel
# Setup the Neopixel strip on pin0 with a length of 8 pixels
np = neopixel.NeoPixel(pin0, 8)
def wheel(wheelByte):
wheelByte = 255 - wheelByte
if wheelByte < 85:
return (255 - wheelByte * 3, 0, wheelByte * 3)
if wheelByte < 170:
wheelByte = wheelByte - 85
return (0, wheelByte * 3, 255 - wheelByte * 3)
wheelByte = wheelByte - 170
return (wheelByte * 3, 255 - wheelByte * 3, 0)
wheelPos = 0
while True:
#Iterate over each LED in the strip
for pixel_id in range(0, len(np)):
# Assign the current LED a color value from the rainbow sequence, with current offset
np[pixel_id] =wheel((wheelPos + pixel_id) % 255)
# Display the current pixel data on the Neopixel strip
np.show()
wheelPos = (wheelPos + 1) % 255
sleep(100)
@cefn
Copy link
Author

cefn commented Aug 9, 2016

sleep(100) is a mistake, as that's in seconds (by contrast with sleep_ms(100))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment