Blender game controller using arduino not working properly I have started learning blender game engine and arduino. Now I am trying to implement whatever I have learned in this simple game where I press a push button connected to arduino and Cube in blender game should move to left.
It is working but it is not smooth. 
I want it like this: when I press the push button cube should start moving, when I release it should stop
What is happening: when I press the push button, cube doesn't move until move function finishes and then it moves suddenly my a large amount (amount depends upon for how much time I held down the button).

Python code is as follows:

import serial
import bge
from time import sleep

ser = serial.Serial('COM3', 9600)

movement = 0.1

def left():
    print("Function Started")
    cont = bge.logic.getCurrentController()
    player = cont.owner
    count = 1
    while count &lt; 50:
        print("Count " + str(count))
        count += 1
        if (ser.inWaiting() &gt; 0):
            var = ser.readline()
            print(var)
            var = str(var)
            var = var[2:-5]
            var = int(var)
            print('Int var : ' + str(var))
            print()
            movements = [var * movement * -1, 0, 0]
            local = False
            player.applyMovement(movements, local)
        sleep(0.1)
    ser.close()
    print("Serial port Closed.")

#left()


How should I write this to get desired output?



Arduino Code:


const int inputPin = 13;

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  pinMode(inputPin, INPUT);

}

void loop() {
  // put your main code here, to run repeatedly:
  int var = digitalRead(inputPin);
  Serial.println(var);

}


It sends 0 if button is not pressed and 1 if it is pressed.
i don't have arduino but this should work, the problem its that you have an always sensor true level triggering = false (see the img), this means that your script will runs once.



Make it TRUE and now your script will run every frame.



And now your script should be:

import serial
import bge
from time import sleep

ser = serial.Serial('COM3', 9600)

movement = 0.1

def left():
    print("Function Started")
    cont = bge.logic.getCurrentController()
    player = cont.owner
#    count = 1
#    while count &lt; 50:
#        print("Count " + str(count))
#        count += 1
    if (ser.inWaiting() &gt; 0):
        var = ser.readline()
        print(var)
        var = str(var)
        var = var[2:-5]
        var = int(var)
        print('Int var : ' + str(var))
        print()
        movements = [var * movement * -1, 0, 0]
        local = False
        player.applyMovement(movements, local)
#        sleep(0.1)
#    ser.close()
    print("Serial port Closed.")


Since i dont have arduino i tested with this script.

#import serial
import bge
#from time import sleep

#ser = serial.Serial('COM3', 9600)

movement = 0.001

def left():
    print("Function Started")
    cont = bge.logic.getCurrentController()
    player = cont.owner
#    count = 1
#    while count &lt; 50:
#        print("Count " + str(count))
#        count += 1
    if True:
        var = '0.005564256'
        print(var)
        var = str(var)
        var = var[2:-5]
        var = int(var)
        print('Int var : ' + str(var))
        print()
        movements = [var * movement * -1, 0, 0]
        local = False
        player.applyMovement(movements, local)
#        sleep(0.1)
#    ser.close()
    print("Serial port Closed.")

#left()