Blender and Arduino

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 = Serial.read();
if (‘a’==ch) {
val = analogRead(potPin)/4;
Serial.write(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 = serialport.read(size=1)
y = ord(x)
print “y=”, y

else:
serialport.close()

doesn’t work, what do you recomend?— what i’m doing wrong?

Sorry, no direct help from me here. However, if you feel more comfortable with Python compared to the Arduino-coding style, you can try switching to the pyduino-library for the Arduino-side: http://code.google.com/p/pyduino/.

(I remember myself using pyduino when playing with Arduino+Blender (over a year ago; alas, no arduino or .blend files kept))

i was trying to use pyduino, firmata and blender, but i dunno what i’m doing wrong, i was following this site http://www.3d-disco.com/arduino_blender.htmbut doesn’t work…

anybody knows how to fix it?

Just to check: this is done in 2.49 and not 2.5, right? (Python v2.6 code is used in your first post’s example, and also in the code and .blend-file on the page you linked to.

(I have no access to my Arduino anymore, can’t check. Some obvious comment follows : ). You might want to control a LED-light on your Arduino using pyduino - to check that the pyduino+firmata protocol is working correctly. (pyduino is python v2.6). If that side works, get into blender 2.49 and see how that goes. Break the problem into pieces.

Do you get console output or errors?

Hi,
I was wondering which version of pyduino and firmata you used … ?
I was trying also these days to acces data from an arduino with python, and got also some difficutlies…

I finaly realised there is some incompatibilities here and there and that actualy the pyduino code is not completely up to date to the last firmata version (2.1)

this blogpost is giving some updated code but I still had some problem with it, the author (using also blender with openCV) is maybe working on it now : http://www.f24h.netsons.org/2010/05/pyduino-firmata-2-1/

sorry I missread, in the first post I see anlemo you use direct serial communication,
I’ve use similar code to talk to arduino from puredata, and I don’t see the problem with your code (but I can be blind)
I had no problem to extract data using readline() method, I’ll try to find back the exact code but I remember it to be very similar to your’s… If you would like to tell us the error you get it could be easier to debug also.

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

http://en.myinventions.pl/index.php?page=ArduinoToBlender

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()
{
Serial.begin(9600);
}

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

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 = serialport.read(size=1)
print x
dane.write(x)
y = serialport.read(size=1)
print y
dane.write(y)
dane.close()

serialport.close()

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

import GameLogic

dane=open(‘dane.txt’, ‘rb’)
x=dane.read(1)
y=dane.read(1)
dane.close()

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

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.