Arduino and Blender

hi everybody, i'm trying to communicate the arduino with blender, but i have a lot of problems..

This is the arduino code, the arduino take the value of a potentiometer and send de data when receives an 'a'

int potPin = 0; int val = 0;

void setup() { Serial.begin(9600); }

void loop() { if ( Serial.available()) { char ch =; if ('a'==ch) { val = analogRead(potPin)/4; Serial.print(val); } } }

this is the phyton code in blender

import serial

serialport = serial.Serial('COM5', 9600)

for i in range(1, 100): serialport.write('a') x = y = ord(x) print "y=", y

else: serialport.close()

doesn't work, what do you recomend?--- what i'm doing wrong?

Try this (Arduino side)...

[glow]byte[/glow] val = 0;

srry,, doesn't work

when i test it in "serial monitor" of arduino,, when i put an 'a' the serial gives me the data ...

i think the problem is in phyton

in the arduino code instead of serial.print() use serial.write()
I don’t see anything wrong with your python code.

also it’s probably the copy paste but the x = is not aligned with the rest of the code in the for loop. the code won’t run if the code isn’t indented right.

srry, doesn't work

when i run the python script the program gets stuck, it seems that the program doesn't send anything to the port

I think I noticed a mistake.I think the else can’t be used without an if statement so instead of using the for loop try this:
i = 0
if i < 100:

In your Arduino code I think there is a mistake in the header of your if statement:

 if ( Serial.available()) {
   char ch =;
   if ('a'==ch) {  
       val = analogRead(potPin)/4;

if ‘a’ == ch
should be
if ch == 'a’

Adrenalina is right, Else only works with If. Just take the Else out and the serial.close() will automatically execute when the for loop is finished. I think that’s what you’re trying to do:

for i in range(1, 100):
x =
y = ord(x)
print “y=”, y


Looks like a cool project.

if 'a' == ch should be if ch == 'a'

Makes no difference which order the operands appear in. They are being tested for equality.

hi everybody, finally i could communicate the arduino with the blender game engine. i get the information from here:

i'm controlling a cube with 2 potentiometers (2 externals signs) one is for the direccion and the other one is for the speed, as you can see this ambient, cube(blue) , floor (green), ramp(gray)

Arduino code

void setup()

void loop()
Serial.print(analogRead(0)/4, BYTE);   //direction
Serial.print(analogRead(1)/4, BYTE);    // speed

this is the python script which receives the 2 inputs from the serial port and write them on the text file.

import serial

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

while 1:
    dane=open('dane.txt', 'r+b')
    x =
    print x
    y =
    print y


this is the python script of the blender game engine which controls the direction and the speed of the cube.

import GameLogic

dane=open('dane.txt', 'rb')

contr = GameLogic.getCurrentController()
z = 0.001*(ord(x)-128)
location.dRot=[0,0,z]          // direction
w = 0.001*(ord(y))
location.dLoc=[w,0,0]     // speed

i need to send a character to the serial port when the cube is climbing the ramp

i already have the code that identifies when the cube is going up with a variable, the variable is "a" and when the cube is going up a==1 and a==0 when not.

the reason that the first code doesn't have end is because i need to communicate the 2 external signs at every moment, and both scripts are running at the same time.

the second code is where i have the variable and in the first code is where i get the data from the serial port, so i dunno how to communicate the 2 scripts.

i would like to say if the variable a==1 send a carachter but as in this one i haven't the serial communication i dunno how to do it