|
9 | 9 | import adafruit_pca9685 |
10 | 10 | import adafruit_motor.servo |
11 | 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 |
| 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 |
15 | 16 |
|
16 | 17 | # STEMMA QT 3V needs to be activated |
17 | 18 | i2c_power = digitalio.DigitalInOut(board.I2C_POWER) |
|
32 | 33 | min_pitch_angle = PITCH_OFFSET + (PITCH_RANGE / 2) |
33 | 34 | max_pitch_angle = PITCH_OFFSET + 180 - (PITCH_RANGE / 2) |
34 | 35 |
|
| 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 | + |
35 | 42 | while True: |
36 | 43 | x, y = nc.joystick |
37 | 44 | 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 | + ) |
39 | 48 | ax = nc.acceleration[0] |
40 | 49 |
|
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 |
46 | 56 | laser.duty_cycle = 0 |
47 | 57 | time.sleep(0.01) |
0 commit comments