Created
September 29, 2024 01:32
-
-
Save oliverswitzer/3195a15e450512c8e220ff3fe5718a45 to your computer and use it in GitHub Desktop.
Pimoroni Servo 2040 Serial Testing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import gc | |
import time | |
import math | |
from pimoroni import Button | |
from plasma import WS2812 | |
from servo import ServoCluster, servo2040 | |
from machine import UART, Pin | |
""" | |
An example of applying a wave pattern to a group of servos and the LEDs. | |
Press "Boot" to toggle the wave pattern on and off. | |
NOTE: ServoCluster and Plasma WS2812 use the RP2040's PIO system, | |
and as such may have problems when running code multiple times. | |
If you encounter issues, try resetting your board. | |
""" | |
SPEED = 5 # The speed that the LEDs will cycle at | |
BRIGHTNESS = 0.4 # The brightness of the LEDs | |
UPDATES = 50 # How many times to update LEDs and Servos per second | |
SERVO_EXTENT = 75 # How far from zero to move the servos | |
# Free up hardware resources ahead of creating a new ServoCluster | |
gc.collect() | |
# Create a servo cluster for pins 0 to 7, using PIO 0 and State Machine 0 | |
START_PIN = servo2040.SERVO_3 | |
END_PIN = servo2040.SERVO_8 | |
servos = ServoCluster(pio=0, sm=0, pins=list(range(START_PIN, END_PIN + 1))) | |
# Create the LED bar, using PIO 1 and State Machine 0 | |
led_bar = WS2812(servo2040.NUM_LEDS, 1, 0, servo2040.LED_DATA) | |
# Create the user button | |
user_sw = Button(servo2040.USER_SW) | |
# Start updating the LED bar | |
led_bar.start() | |
# Initialize UART on pins that would normally be used for servos | |
# Using SERVO_1 (GP0) as TX and SERVO_2 (GP1) as RX | |
uart = UART(0, baudrate=9600, tx=Pin(servo2040.SERVO_1), rx=Pin(servo2040.SERVO_2)) | |
offset = 0.0 | |
wave_active = True | |
last_button_state = False | |
def check_uart_message(): | |
if uart.any(): | |
msg = uart.read(5) # Read up to 5 bytes | |
print("Received message:", msg) | |
return msg == b'hello' | |
return False | |
while True: | |
if check_uart_message(): | |
# Animate LEDs when 'hello' is received | |
# Create a simple, obvious blinking animation | |
for _ in range(3): # Blink 3 times | |
# Turn all LEDs on with bright red color | |
for i in range(servo2040.NUM_LEDS): | |
led_bar.set_hsv(i, 0.0, 1.0, 1.0) # Full brightness red | |
led_bar.update() | |
time.sleep(0.5) # Stay on for half a second | |
# Turn all LEDs off | |
led_bar.clear() | |
led_bar.update() | |
time.sleep(0.5) # Stay off for half a second | |
# Return to the original state (all LEDs off) | |
led_bar.clear() | |
led_bar.update() | |
current_button_state = user_sw.raw() | |
# Check for button press (transition from not pressed to pressed) | |
if current_button_state and not last_button_state: | |
wave_active = not wave_active | |
if not wave_active: | |
# Turn off LEDs and disable servos when wave is inactive | |
led_bar.clear() | |
servos.disable_all() | |
if wave_active: | |
offset += SPEED / 1000.0 | |
# Update all the LEDs | |
for i in range(servo2040.NUM_LEDS): | |
hue = i / (servo2040.NUM_LEDS * 4) | |
led_bar.set_hsv(i, hue + offset, 1.0, BRIGHTNESS) | |
# Update all the Servos | |
for i in range(servos.count()): | |
angle = ((i / servos.count()) + offset) * math.pi * 2 | |
servos.value(i, math.sin(angle) * SERVO_EXTENT, load=False) | |
servos.load() # We have now set all the servo values, so load them | |
time.sleep(1.0 / UPDATES) | |
last_button_state = current_button_state |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import time | |
from machine import UART, Pin | |
from servo import servo2040 | |
uart = UART(0, baudrate=9600, tx=Pin(servo2040.SERVO_1), rx=Pin(servo2040.SERVO_2)) | |
while True: | |
if uart.any(): | |
data = uart.read().decode('utf-8').strip() | |
print(f"Received: {data}") | |
if data.lower() == 'hello': | |
print("Moving servo left and right") | |
time.sleep(0.1) # Small delay to prevent CPU overuse |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment