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