|
| 1 | +import time |
| 2 | +import board |
| 3 | +import adafruit_nunchuk |
| 4 | +import neopixel |
| 5 | +import simpleio |
| 6 | + |
| 7 | +nc = adafruit_nunchuk.Nunchuk(board.I2C()) |
| 8 | +# create neopixel object |
| 9 | +NEOPIN = board.D6 |
| 10 | +NEOLENGTH = 60 |
| 11 | +NEOORDER = neopixel.GRBW # set to GRB for 'regular' RGB NeoPixels |
| 12 | +pixels = neopixel.NeoPixel( |
| 13 | + NEOPIN, NEOLENGTH, brightness=0.1, auto_write=False, pixel_order=NEOORDER |
| 14 | +) |
| 15 | + |
| 16 | +RED = (220, 0, 0) |
| 17 | +PURPLE = (80, 0, 160) |
| 18 | +PINK = (100, 0, 80) |
| 19 | +GREEN = (0, 180, 0) |
| 20 | +CYAN = (0, 80, 100) |
| 21 | +BLUE = (0, 0, 255) |
| 22 | +BLACK = (0, 0, 0) |
| 23 | + |
| 24 | +COLORS = [RED, PURPLE, PINK, GREEN, CYAN, BLUE] |
| 25 | +pix = 0 # selected pixel |
| 26 | +color_pick = 0 # current color index |
| 27 | +pixels.fill(BLACK) |
| 28 | +pixels.show() |
| 29 | + |
| 30 | +while True: |
| 31 | + x, y = nc.joystick # get joystick values |
| 32 | + ax, ay, az = nc.acceleration # get accelerometer values |
| 33 | + |
| 34 | + tilt_x = simpleio.map_range(ax, 300.0, 800.0, 0.0, 1.0) # remap tilt to brightness |
| 35 | + # remap y to current pixel |
| 36 | + pix = int( |
| 37 | + simpleio.map_range(y, 0, 255, 0, NEOLENGTH - 1) |
| 38 | + ) |
| 39 | + |
| 40 | + if nc.button_C: # hold C button to use tilt for brightness |
| 41 | + pixels.brightness = tilt_x |
| 42 | + |
| 43 | + if nc.button_Z: |
| 44 | + color_pick = (color_pick + 1) % 4 # cycle through colors |
| 45 | + time.sleep(0.02) # debounce |
| 46 | + |
| 47 | + pixels.fill(BLACK) # turn off pixels |
| 48 | + for i in range(0, pix + 1): # light up all pixels up to the current one |
| 49 | + pixels[i] = COLORS[color_pick] |
| 50 | + |
| 51 | + pixels.show() |
0 commit comments