Skip to content

Commit a9db0be

Browse files
committed
Added Nunchuck Laser Code
1 parent cbf40ab commit a9db0be

1 file changed

Lines changed: 41 additions & 0 deletions

File tree

Nunchuck_Laser/code.py

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries
2+
# SPDX-License-Identifier: MIT
3+
4+
import time
5+
import board
6+
import digitalio
7+
import simpleio
8+
import adafruit_nunchuk
9+
import adafruit_pca9685
10+
import adafruit_motor.servo
11+
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+
16+
# STEMMA QT 3V needs to be activated
17+
i2c_power = digitalio.DigitalInOut(board.I2C_POWER)
18+
i2c_power.switch_to_output(value=False)
19+
i2c = board.I2C()
20+
21+
wing = adafruit_pca9685.PCA9685(i2c)
22+
wing.frequency = 50
23+
servo_yaw = adafruit_motor.servo.Servo(wing.channels[0])
24+
servo_pitch = adafruit_motor.servo.Servo(wing.channels[1])
25+
laser = wing.channels[2]
26+
27+
nc = adafruit_nunchuk.Nunchuk(i2c)
28+
29+
while True:
30+
x, y = nc.joystick
31+
servo_yaw.angle = simpleio.map_range(255 - x, 0, 255, (YAW_RANGE / 2), 180 - (YAW_RANGE / 2))
32+
servo_pitch.angle = simpleio.map_range(y, 0, 255, PITCH_OFFSET + (PITCH_RANGE / 2), PITCH_OFFSET + 180 - (PITCH_RANGE / 2))
33+
ax = nc.acceleration[0]
34+
35+
if nc.buttons.Z: # Z-Button sets laser to full brightness
36+
laser.duty_cycle = 0xFFFF
37+
elif nc.buttons.C: # C-Button sets laser to brightness depending on the roll position of your hand
38+
laser.duty_cycle = int(simpleio.map_range(ax, 240, 750, 0, 0xFFFF))
39+
else: # No button pressed sets laser to off
40+
laser.duty_cycle = 0
41+
time.sleep(0.01)

0 commit comments

Comments
 (0)