Moving servomotor with Python problem

In Python I've made a script to read pointers of a game, it reads and output the data through the console. Now i'm trying to send that data to an Arduino and move a servomotor similar to a speedometer. My goal for now is to make an analog speedometer from values taken from a videogame (in this case, Outrun 2006 Coast 2 Coast).

The problem that i've got is the servomotor is not moving correctly when it's receive the data from python. Sometimes it's stuck, sometimes it's not responding. Kinda weird because i've tried to move manually a servomotor from python to arduino and it responds, but with 0.5 sec of delay, and even i tried with a LED, it responds instantly.

Here's the code of the Arduino

#include <Servo.h>

int angulo = 0;
Servo miServo;

void setup() {
miServo.attach(3);
Serial.begin(9600);
}

void loop() {
if(Serial.available() > 0)
{
angulo=Serial.parseInt();
angulo=constrain(angulo,0,180);
}
miServo.write(angulo);
}

And this is the code that i've execute from python

memreading it's from a module that i've created

import memreading, serial

PLANTILLA

###################################################
proceso = 'or2006c2c.exe'
velocidad = 0x07806A4

CODIGO COMUNICACION SERIAL

###################################################
ser = serial.Serial('COM3', 9600)

CODIGO LECTURA DE MEMORIA

###################################################
mem = memreading.MemReading()

processHandle = mem.openProcess(mem.attachToProcess(proceso))

def lecturaMemoria():
data = mem.readMemory(processHandle, velocidad)
data = int(float(data)/1.59)
return str(data)

while True:
try:
ser.open()
dato = lecturaMemoria()
print dato
ser.write(dato)
except KeyboardInterrupt:
ser.close()
break

Here is the code to read in memory coded by myself: https://gist.github.com/PPastene/f955a80abc55964733accc9d9133b488

I'm not gonna explain that code, but the data returns a string from mem.readMemory() method. It's parsed to integer because it need a calculation for then parsed again to string (I can't put a integer value in a loop, python gives an error because integer objects are not iterable). In reddit forums they told me that i need to send the value in bytes. Well, im working with Python 2.x, and IDK how to convert to bytes

I've got the real problem, but it's not solved yet. I'll explain:

I'm sending a data stream from python to arduino, because it's crucial to keep on track the values sended from the memory address (explained in the first post). Python always send a 3 values in a ascii array (bacause im newbie in arduino and python), i've got to convert that array to integer and from Python sending a number to move the servomotor. That works, but not with the data stream that i've coded.

IDK if it's failing the Python code or the arduino code. I'll update the new arduino code

//Code taken from: http://diymakers.es/usando-el-puerto-serie-del-arduino/

#include <Servo.h>

Servo servo;

char cadena[3];
byte posicion = 0;
int valor = 0;

void setup()
{
servo.attach(9);
Serial.begin(9600);
servo.write(valor);
}

void loop()
{
if(Serial.available())
{
memset(cadena, 0, sizeof(cadena));

while (Serial.available()>0)
{
delay(5);
cadena[posicion]=Serial.read();
posicion++;
}
valor=atoi(cadena);
Serial.println(valor);
servo.write(valor);
posicion=0;
}
}

Have a look at this Python - Arduino demo

It is very important that the Python program opens the serial port, allows time for the Arduino to reset, and keeps the serial port open until it is completely finished with the Arduino.

The examples in Serial Input Basics use a similar approach but were written more recently. There is also a parse example.

...R

Robin2:
Have a look at this Python - Arduino demo

It is very important that the Python program opens the serial port, allows time for the Arduino to reset, and keeps the serial port open until it is completely finished with the Arduino.

The examples in Serial Input Basics use a similar approach but were written more recently. There is also a parse example.

...R

I'll try your code. I hope that get to work that i'm doing, but i think that there's also a problem in my python code, because it's sending a streaming of data that IDK if my arduino UNO can handle it. I know that thjere are a lot of projects about analog speedometers for games made with arduino, but IDK how it's work.

P-Pablo:
because it's sending a streaming of data that IDK if my arduino UNO can handle it.

I don't know how-many-times-per-second you mean when you say "streaming of data". It would certainly be possible for a PC to send data too frequently.

Why not organize things so that the PC sends data when the Arduino requests it. That puts the slower Arduino in control.

...R

Why are you using 9600 baud and not 115200 or better?