Python and Arduino serial connection- servo always gets back to same angle

Hello,

It’s a bit difficult for me to explain what is going on but i’ll do my best. If any info is missing let me know

I have a python code that works on a raspberry pi. this code is basicaly image process but in the end it is supposed to send serial information to the arduino which sends signals to a servo motor.

The problem is whenever I send an Integer, the servo goes where I tell him but just for a short time(milisecond or so) and than go back to the center.
for example I send and Int of 70, the servo actually goes to 70 deg but instead of just staying there it always go back to the center.

[code]
#include <Servo.h>

Servo steeringwheel;  // create servo object to control a servo
Servo myspeed;        // create servo object to define the vertical speed

int servo_angle;
int SpeedValue;
const int DCMotorPin = 6; //Assigned pin for DC motor
const int ServoPin = 5; //Assigned pin for Servo steeringwheel motor
int sum = 0;
const int ledPin =  LED_BUILTIN;// the number of the LED pin
void setup() {

  pinMode(ledPin, OUTPUT);
  steeringwheel.attach(ServoPin);  // attaches the servo on pin 10 to the servo object
  pinMode(ServoPin, OUTPUT); //Servo will receive data
  myspeed.attach(DCMotorPin); //DC motor assigned to pin
  pinMode(DCMotorPin, OUTPUT); //DC motor will receive data

  SpeedValue = map(SpeedValue, 1600, 1700, 0, 100); // 100 is set to max speed
  SpeedValue = constrain(SpeedValue, 0, 50); //Every value lower than 0 will be count as zero. in the same way, 50 is the maximum value

  //Good speed we can see at ~1620
  SpeedValue = 1618;

  Serial.begin(115200);   // Set the baudrate
  Serial.flush();
  Serial.setTimeout(500);
}
void loop() {

  if (Serial.available() > 0) {
    servo_angle = Serial.parseInt();

    servo_angle = servo_angle + sum;
    if (servo_angle != 0) {
      steeringwheel.write(servo_angle);
      SpeedValue = 1618;
      digitalWrite(ledPin, HIGH);
      Serial.print(servo_angle);
      // myspeed.write(SpeedValue);
    }
    else {
      steeringwheel.write(90);
      myspeed.write(1600);
    }
  }
}

[/code]

and I add some of my python code. It’s long, I try to copy here the relevant parts.

## CONNECTION WITH ARDUINO SETUP############################
port = "/dev/ttyACM0"
baudrate = 115200
serial.Serial(port, baudrate, timeout = 1)
############################################################

def sendInt(servo_angle):
    send_string=str(servo_angle)
    send_string += "\n"
    ser.write(send_string.encode('utf-8'))
    ser.flush()


sendInt(60) #send 60 deg to servo
time.sleep(2.5)
sendInt(0) #send 0 to servo to stop the DCmotor and set servo to 90 deg

please, help me out here Im stuck on this topic for a while without any success
Thank’s!

To determine where the error is i suggest you use the Serial monitor to send the data first (be sure to set the line ending to 'none')

if (Serial.available() > 0) {
    servo_angle = Serial.parseInt();

see one of the issues is here : parseint returns '0' in case of a 'non valid' value. send_string += "\n"and you send a newline right after.

This Simple Python - Arduino demo may help.

The Python code should work on Windows if you edit it to use the Windows style of COM ports.

...R