Skip to content

first commit nunchuck ring code #1399

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 6, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 51 additions & 0 deletions Nunchuck_Ring/nunchuck_ring.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import time
import board
import adafruit_nunchuk
import neopixel
import simpleio

nc = adafruit_nunchuk.Nunchuk(board.I2C())
# create neopixel object
NEOPIN = board.D6
NEOLENGTH = 60
NEOORDER = neopixel.GRBW # set to GRB for 'regular' RGB NeoPixels
pixels = neopixel.NeoPixel(
NEOPIN, NEOLENGTH, brightness=0.1, auto_write=False, pixel_order=NEOORDER
)

RED = (220, 0, 0)
PURPLE = (80, 0, 160)
PINK = (100, 0, 80)
GREEN = (0, 180, 0)
CYAN = (0, 80, 100)
BLUE = (0, 0, 255)
BLACK = (0, 0, 0)

COLORS = [RED, PURPLE, PINK, GREEN, CYAN, BLUE]
pix = 0 # selected pixel
color_pick = 0 # current color index
pixels.fill(BLACK)
pixels.show()

while True:
x, y = nc.joystick # get joystick values
ax, ay, az = nc.acceleration # get accelerometer values

tilt_x = simpleio.map_range(ax, 300.0, 800.0, 0.0, 1.0) # remap tilt to brightness
# remap y to current pixel
pix = int(
simpleio.map_range(y, 0, 255, 0, NEOLENGTH - 1)
)

if nc.button_C: # hold C button to use tilt for brightness
pixels.brightness = tilt_x

if nc.button_Z:
color_pick = (color_pick + 1) % 4 # cycle through colors
time.sleep(0.02) # debounce

pixels.fill(BLACK) # turn off pixels
for i in range(0, pix + 1): # light up all pixels up to the current one
pixels[i] = COLORS[color_pick]

pixels.show()