-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRpiToArduino.py
More file actions
28 lines (24 loc) · 901 Bytes
/
RpiToArduino.py
File metadata and controls
28 lines (24 loc) · 901 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
import serial
import time
# Open serial connection to Arduino
ser = serial.Serial('/dev/serial0', 115200, timeout=1) # Use /dev/ttyUSB0 if using USB-Serial adapter
time.sleep(2) # Wait for connection to establish
def set_motor_speed(duty_cycle):
"""Send the duty cycle command to Arduino over UART."""
command = f"{duty_cycle}\n" # Ensure newline for proper parsing on Arduino
ser.write(command.encode('utf-8'))
print(f"Sent: {command.strip()}")
try:
while True:
speed = input("Enter motor speed (0-100%): ")
if speed.isdigit():
duty_cycle = int(speed)
if 0 <= duty_cycle <= 100:
set_motor_speed(duty_cycle)
else:
print("Enter a value between 0 and 100.")
else:
print("Invalid input, enter a number.")
except KeyboardInterrupt:
print("\nExiting...")
ser.close()