import serial
import RPi.GPIO as GPIO
import time
# Setup GPIO pins
MOTOR_PINS = {
1: {'step': 3, 'dir': 2},
2: {'step': 5, 'dir': 4},
3: {'step': 7, 'dir': 6},
4: {'step': 5, 'dir': 6}
}
GPIO.setmode(GPIO.BCM)
for motor in MOTOR_PINS.values():
GPIO.setup(motor['step'], GPIO.OUT)
GPIO.setup(motor['dir'], GPIO.OUT)
# Open serial connection to Nextion
ser = serial.Serial('/dev/ttyS0', 9600, timeout=1)
def set_motor(motor_id, speed, direction):
pins = MOTOR_PINS[motor_id]
GPIO.output(pins['dir'], GPIO.HIGH if direction else GPIO.LOW)
delay = 1.0 / (speed * 100) # Adjust scaling
for _ in range(200): # Number of steps
GPIO.output(pins['step'], GPIO.HIGH)
time.sleep(delay / 2)
GPIO.output(pins['step'], GPIO.LOW)
time.sleep(delay / 2)
# Main loop
while True:
if ser.in_waiting:
data = ser.readline().decode().strip()
print("Received:", data)
# Parse speed and direction values from Nextion
# (Use regex or split based on your message format)