1

I need help from communicating serial data from blender game engine to arduino.

I'm making keyboard input from blender and trying to communicate to arduino, but it's not working.

This is blender code

import serial

ser = serial.Serial("COM6", 9600)
x=ser.write(1)   
print(x)
ser.close()

Logic

Key "a" --> Python script

https://i.stack.imgur.com/fAUfI.png

and this is the arduino code i trying to communicate from blender.

int led = 2;

void setup() {
    Serial.begin(9600);
    pinMode(led, OUTPUT);
}

void loop() {
    if ( Serial.available())
    {
        char ch = Serial.read();
        if(ch >= '0' && ch <= '9')
        {
        digitalWrite(led, HIGH);
        }
    }
}

Actually, when Blender Game Engine (BGE) running, and I press Key 'a' blender communicate with arduino and LED turn on.

Am I doing wrong?

Can anybody help me to solve this?

dsolimano
  • 8,870
  • 3
  • 48
  • 63
Fahmiii
  • 21
  • 1
  • 4

1 Answers1

2

in your blender python code, you're sending an integer:

x=ser.write(1)   

whereas in your arduino code you're checking for an ASCII digit between '0' and '9', i.e. numbers between 48 and 57

if(ch >= '0' && ch <= '9')

either try to change your python code to ser.write('1') or change your arduino code to ch >= 0 && ch <= 9, and it should work.

Also, before binding your code as a script inside blender, you should first test your python script outside of blender. simply by running using the commandline: python script.py, in the directory where the script is.

zmo
  • 24,463
  • 4
  • 54
  • 90