1
$\begingroup$

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 < 50:
        print("Count " + str(count))
        count += 1
        if (ser.inWaiting() > 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.

$\endgroup$
1
  • $\begingroup$ I'm voting to close this question because it seems to be an Arduino question, and not a Blender one. I suggest migrating the question to Arduino.Stackexchange. $\endgroup$
    – brasshat
    Commented Jun 7, 2017 at 20:08

1 Answer 1

2
$\begingroup$

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.

enter image description here

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

enter image description here

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 < 50:
#        print("Count " + str(count))
#        count += 1
    if (ser.inWaiting() > 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 < 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()
$\endgroup$
1
  • 1
    $\begingroup$ You forgot to comment out ser.close() otherwise it will close the port after first run and will not work. btw Thank you, it worked for me as I wanted it to be. $\endgroup$ Commented Jun 8, 2017 at 2:08

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .