Nunchuck Laser code improvements
@@ -12,6 +12,7 @@ import adafruit_motor.servo
PITCH_OFFSET = 45 # The offset for the pitch
PITCH_RANGE = 90 # The range the servo can rotate up and down in degrees
YAW_RANGE = 90 # The range the servo can rotate side to side in degrees
+INVERT_PITCH = False
# STEMMA QT 3V needs to be activated
i2c_power = digitalio.DigitalInOut(board.I2C_POWER)
@@ -32,16 +33,25 @@ max_yaw_angle = 180 - (YAW_RANGE / 2)
min_pitch_angle = PITCH_OFFSET + (PITCH_RANGE / 2)
max_pitch_angle = PITCH_OFFSET + 180 - (PITCH_RANGE / 2)
+pitch_inputs = [0, 255]
+if INVERT_PITCH: # Swap the Min and Max Values
+ pitch_inputs[0], pitch_inputs[1] = pitch_inputs[1], pitch_inputs[0]
+
+brightness = 0xFFFF # Initial brightness value
+
while True:
x, y = nc.joystick
servo_yaw.angle = simpleio.map_range(255 - x, 0, 255, min_yaw_angle, max_yaw_angle)
- servo_pitch.angle = simpleio.map_range(y, 0, 255, min_pitch_angle, max_pitch_angle)
+ servo_pitch.angle = simpleio.map_range(
+ y, pitch_inputs[0], pitch_inputs[1], min_pitch_angle, max_pitch_angle
+ )
ax = nc.acceleration[0]
- if nc.buttons.Z: # Z-Button sets laser to full brightness
- laser.duty_cycle = 0xFFFF
- elif nc.buttons.C: # C-Button sets laser brightness depending on the roll position of your hand
- laser.duty_cycle = int(simpleio.map_range(ax, 240, 750, 0, 0xFFFF))
+ if nc.buttons.Z: # Z-Button sets laser PWM to current brightness
+ laser.duty_cycle = brightness
+ elif nc.buttons.C: # C-Button sets laser brightness to value of the nunchuck roll position
+ brightness = int(simpleio.map_range(ax, 250, 750, 0, 0xFFFF))
+ laser.duty_cycle = brightness
else: # No button pressed sets laser to off
laser.duty_cycle = 0
time.sleep(0.01)
