Skip to content

Commit 2a7a154

Browse files
authored
Merge pull request #1934 from makermelissa/main
Nunchuck Laser code improvements
2 parents 49be81d + 3867149 commit 2a7a154

File tree

1 file changed

+19
-9
lines changed

1 file changed

+19
-9
lines changed

Nunchuck_Laser/code.py

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,10 @@
99
import adafruit_pca9685
1010
import adafruit_motor.servo
1111

12-
PITCH_OFFSET = 45 # The offset for the pitch
13-
PITCH_RANGE = 90 # The range the servo can rotate up and down in degrees
14-
YAW_RANGE = 90 # The range the servo can rotate side to side in degrees
12+
PITCH_OFFSET = 45 # The offset for the pitch
13+
PITCH_RANGE = 90 # The range the servo can rotate up and down in degrees
14+
YAW_RANGE = 90 # The range the servo can rotate side to side in degrees
15+
INVERT_PITCH = False
1516

1617
# STEMMA QT 3V needs to be activated
1718
i2c_power = digitalio.DigitalInOut(board.I2C_POWER)
@@ -32,16 +33,25 @@
3233
min_pitch_angle = PITCH_OFFSET + (PITCH_RANGE / 2)
3334
max_pitch_angle = PITCH_OFFSET + 180 - (PITCH_RANGE / 2)
3435

36+
pitch_inputs = [0, 255]
37+
if INVERT_PITCH: # Swap the Min and Max Values
38+
pitch_inputs[0], pitch_inputs[1] = pitch_inputs[1], pitch_inputs[0]
39+
40+
brightness = 0xFFFF # Initial brightness value
41+
3542
while True:
3643
x, y = nc.joystick
3744
servo_yaw.angle = simpleio.map_range(255 - x, 0, 255, min_yaw_angle, max_yaw_angle)
38-
servo_pitch.angle = simpleio.map_range(y, 0, 255, min_pitch_angle, max_pitch_angle)
45+
servo_pitch.angle = simpleio.map_range(
46+
y, pitch_inputs[0], pitch_inputs[1], min_pitch_angle, max_pitch_angle
47+
)
3948
ax = nc.acceleration[0]
4049

41-
if nc.buttons.Z: # Z-Button sets laser to full brightness
42-
laser.duty_cycle = 0xFFFF
43-
elif nc.buttons.C: # C-Button sets laser brightness depending on the roll position of your hand
44-
laser.duty_cycle = int(simpleio.map_range(ax, 240, 750, 0, 0xFFFF))
45-
else: # No button pressed sets laser to off
50+
if nc.buttons.Z: # Z-Button sets laser PWM to current brightness
51+
laser.duty_cycle = brightness
52+
elif nc.buttons.C: # C-Button sets laser brightness to value of the nunchuck roll position
53+
brightness = int(simpleio.map_range(ax, 250, 750, 0, 0xFFFF))
54+
laser.duty_cycle = brightness
55+
else: # No button pressed sets laser to off
4656
laser.duty_cycle = 0
4757
time.sleep(0.01)

0 commit comments

Comments
 (0)