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 = Serial.read(); 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 = 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?

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 = serialport.read(size=1) 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
while(1):
if i < 100:
serial.write(‘a’)
etc…
else:
serial.close()
break
i++

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

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

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):
serialport.write(‘a’)
x = serialport.read(size=1)
y = ord(x)
print “y=”, y

serialport.close()

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:

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)

http://i214.photobucket.com/albums/cc27/anlemo/cube.jpg

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 the 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

http://i214.photobucket.com/albums/cc27/anlemo/cubegoingup.jpg

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