Skip to main content
3 of 11
deleted 1059 characters in body
ZanQdo
  • 31
  • 1
  • 1
  • 5

Send multiple int values from Python to Arduino using pySerial

I'm trying to send 5 ints in the range of 0-180 from Blender to the Arduino Uno device using pySerial (py3K). I have managed to send 1 int (not sure if it's the best way but it works). However I'm failing to send more than 1 and every example online seems to stop at 1.

Here's the simplified code. The task is to send servo0-servo4 to the arduino and apply those values to the corresponding servos.

Runs once at startup in Blender

import sys
sys.path.append("/usr/lib/python3.3/site-packages/")
import serial
import bge

bge.arduino = serial.Serial('/dev/ttyACM0', 9600, timeout=.01)

Runs in a loop in Blender

import bge

servo0 = 0
servo1 = 45
servo2 = 90
servo3 = 135
servo4 = 180

bge.arduino.write(str(servo0).encode())

print (bge.arduino.readline())

The Arduino code

#include <Servo.h>

Servo servo0;
Servo servo1;
Servo servo2;
Servo servo3;
Servo servo4;

void setup(){
  Serial.begin(9600);
  Serial.setTimeout(10);
  servo0.attach(3);
  servo1.attach(5);
  servo2.attach(6);
  servo3.attach(9);
  servo4.attach(10);
}

void loop(){
  
  if(Serial.available()){
    
    int message = Serial.parseInt();
    
    // let's be safe
    if (message > 179){
      message = 179;
    }
    else if (message < 0){
      message = 0;
    }
    
    // control the servos
    servo0.write(message);
    Serial.println(message);

  }
  
}
ZanQdo
  • 31
  • 1
  • 1
  • 5