-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathled_test.py
More file actions
37 lines (29 loc) · 1.09 KB
/
Copy pathled_test.py
File metadata and controls
37 lines (29 loc) · 1.09 KB
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
29
30
31
32
33
34
35
36
37
import serial
import time
# CONFIGURATION
# Check your port using 'ls /dev/tty*' in terminal
SERIAL_PORT = '/dev/ttyUSB0'
BAUD_RATE = 115200
try:
# Initialize Serial connection
ser = serial.Serial(SERIAL_PORT, BAUD_RATE, timeout=1)
# IMPORTANT: Wait for Arduino to reboot after serial connection is established
print(f"Connecting to {SERIAL_PORT}...")
time.sleep(2)
print("Connection established. Sending data...")
while True:
# Loop through numbers 1, 2, 3
for value in [1, 2, 3]:
# Convert to string, add newline, and encode to bytes
command = f"{value}\n"
ser.write(command.encode('utf-8'))
print(f"Sent: {value}")
# Wait 1 second before sending the next number
time.sleep(1)
except serial.SerialException as e:
print(f"Error opening serial port: {e}")
print("Tip: Make sure you have permissions (sudo usermod -a -G dialout $USER)")
except KeyboardInterrupt:
print("\nExiting script...")
if 'ser' in locals() and ser.is_open:
ser.close()