Skip to content

Commit e90d4d0

Browse files
authored
Merge pull request #1932 from makermelissa/main
Added Nunchuck Laser Code
2 parents cbf40ab + c4197e2 commit e90d4d0

1 file changed

Lines changed: 47 additions & 0 deletions

File tree

Nunchuck_Laser/code.py

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
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+
# Pre-calculate the angles
30+
min_yaw_angle = YAW_RANGE / 2
31+
max_yaw_angle = 180 - (YAW_RANGE / 2)
32+
min_pitch_angle = PITCH_OFFSET + (PITCH_RANGE / 2)
33+
max_pitch_angle = PITCH_OFFSET + 180 - (PITCH_RANGE / 2)
34+
35+
while True:
36+
x, y = nc.joystick
37+
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)
39+
ax = nc.acceleration[0]
40+
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
46+
laser.duty_cycle = 0
47+
time.sleep(0.01)

0 commit comments

Comments
 (0)