import time
import keyboard
import serial
import io

spacePressed = False

ser = serial.Serial(port="COM4", baudrate=9600, timeout=1)

print("Script is turned on")
while True:
    line = ser.read().decode().strip()
    if (line == "1" and spacePressed == False):
        keyboard.press("space")
        spacePressed = True
    elif (line == "0" and spacePressed == True):
        keyboard.release("space")
        spacePressed = False
    ser.flushInput()
    ser.flushOutput()

print("Script is turned off")
