fork download
  1. import serial
  2.  
  3. # Replace 'COM3' with your Arduino's serial port
  4. arduino_port = 'COM3'
  5. baud_rate = 9600
  6.  
  7. try:
  8. ser = serial.Serial(arduino_port, baud_rate, timeout=1)
  9. print(f"Connected to Arduino on port {arduino_port}")
  10. except serial.SerialException:
  11. print(f"Could not open port {arduino_port}")
  12. exit()
  13.  
  14. while True:
  15. try:
  16. line = ser.readline().decode('utf-8').strip()
  17. if line == "PRESSED":
  18. print("Foot pedal pressed! Triggering action...")
  19. # Add your game action or function call here
  20. except serial.SerialException:
  21. print("Lost connection to Arduino")
  22. break
  23. except UnicodeDecodeError:
  24. continue
  25.  
Success #stdin #stdout 0.1s 14248KB
stdin
Standard input is empty
stdout
Could not open port COM3