import time import board import neopixel from adafruit_circuitplayground.express import cpx SERVO_PIN = board.A2 BUTTON_A = board.BUTTON_A BUTTON_B = board.BUTTON_B NUM_LEDS = 10 FULL_CIRCLE = 20 pixels = neopixel.NeoPixel(board.NEOPIXEL, NUM_LEDS, brightness=0.2) servo = cpx.servo(SERVO_PIN) fullCircleMode = False currentStop = 0 def setLEDs(): for i in range(NUM_LEDS): if i == currentStop: pixels[i] = (0, 0, 255) # Current stop LED (blue) elif i < currentStop: pixels[i] = (255, 0, 0) # Visited stop LED (red) else: pixels[i] = (0, 0, 0) # Unvisited stop LED (off) pixels.show() def flashCurrentStop(): for _ in range(5): pixels[currentStop] = (0, 255, 0) # Flash current stop LED (green) pixels.show() time.sleep(0.2) pixels[currentStop] = (0, 0, 0) pixels.show() time.sleep(0.2) def rotateTurntable(angle): servo.angle = angle def checkButtons(): if cpx.button_a: if cpx.button_b: # Both buttons pressed - enter full circle mode fullCircleMode = True else: # Button A pressed - move to next stop currentStop = (currentStop + 1) % (FULL_CIRCLE if fullCircleMode else NUM_LEDS) setLEDs() flashCurrentStop() elif cpx.button_b: # Button B pressed - move to previous stop currentStop = (currentStop - 1 + (FULL_CIRCLE if fullCircleMode else NUM_LEDS)) % (FULL_CIRCLE if fullCircleMode else NUM_LEDS) setLEDs() flashCurrentStop() # Setup currentStop = 0 fullCircleMode = False setLEDs() while True: checkButtons() if fullCircleMode: # Full circle mode targetAngle = int(map(currentStop, 0, FULL_CIRCLE, 0, 180)) rotateTurntable(targetAngle) else: # Half circle mode targetAngle = int(map(currentStop, 0, NUM_LEDS, 0, 180)) rotateTurntable(targetAngle)
Can someone help to figure out why This code won't work? I can get the servo to turn fine using the following code but I want to get the code above to work and be able to stop/ start and change from half (180) to full circle (360) modes and have the EDS change accordingly. Any help would be greatly appreciated. Jason
# SPDX-FileCopyrightText: 2019 Anne Barela for Adafruit Industries # # SPDX-License-Identifier: MIT """CircuitPython Essentials Servo continuous rotation servo example""" import time import board import pwmio from adafruit_motor import servo # create a PWMOut object on Pin A2. pwm = pwmio.PWMOut(board.A2, frequency=50) # Create a servo object, my_servo. my_servo = servo.ContinuousServo(pwm) while True: print("forward") my_servo.throttle = 0.0 time.sleep(1.0) print("stop") my_servo.throttle = 0.0 time.sleep(2.0) print("reverse") my_servo.throttle = 0.0 time.sleep(1.0) print("stop") my_servo.throttle = 0.0 time.sleep(4.0)