Strange servo-behavior

I have written a little visualBasic application that sends angle-values via UDP to an arduino that writes these angles to a servo.
The value is transfered with no problems and all, but the servo likes to screw up a lot.

The value can only increase/decrease in 2°-steps which is controlled by two buttons, this way the servo should always have enough time between upd-packets to get to its new position.
The lastest value it was set to should be 44° because the application always slowy goes back in 2°-steps until it reaches 44° if there is no more input.

The Problem:
Whenever i'm not constantly updating the servo-value it runs into it's clockwise limit, which can't be good for the gears/engine inside...
shouldn't the servo stay at its last know value until it gets a new one ?

#include <SPI.h>
#include <Ethernet.h>
#include <EthernetUdp.h> 
#include <Servo.h>


byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
byte ip[] = { 192, 168, 0, 178 };
byte gateway[] = { 192, 168, 0, 1 };
byte subnet[] = { 255, 255, 255, 0 };


char packetBuffer[UDP_TX_PACKET_MAX_SIZE]; //buffer to hold incoming packet
char  ReplyBuffer[] = "acknowledged";       // a string to send back
byte remoteIp[4]; // holds recvieved packet's originating IP
unsigned int remotePort; // holds received packet's originating port


//Pin 11,12 & 13 are used by the EthernetShield
const int lightBackPin = 8;  //Need to replace these somtime
const int lightFrontPin =9;  //
const int lightPin = 8;
const int irPin =9;

const int lenk = 10;
const int cam =3;

Servo lenkServo;  // Steering-Servo
Servo camServo;   //Use this later for moveable camera...



int posCam=44;
int posLenk=44;

int M1 = 4;
int E1 = 5;
int E2 = 6;
int M2 = 7;

EthernetUDP Udp;

void setup() {
  Ethernet.begin(mac, ip , gateway, subnet);
  Udp.begin(9100);
  Serial.begin(9600);
  pinMode(lightBackPin, OUTPUT);
  pinMode(lightFrontPin, OUTPUT);
  pinMode(M1,OUTPUT);
  pinMode(M2,OUTPUT);
  lenkServo.attach(lenk);  // attaches the servo on pin 10 to the servo object 
  camServo.attach(cam);
  lenkServo.write(44);delay(500); 
}



void loop() {
  int packetSize = Udp.parsePacket();  // holds received packet size
  int DriveSpeed = 0;
  
  if(packetSize)
    {
    Udp.read(packetBuffer,UDP_TX_PACKET_MAX_SIZE);
    //Serial.println(packetBuffer);
        
        switch(packetBuffer[5]){
        case '-': digitalWrite(lightPin,LOW);digitalWrite(irPin,LOW);break;
        case 'i': digitalWrite(lightPin,LOW);digitalWrite(irPin,HIGH);break;
        case 'l': digitalWrite(lightPin,HIGH);digitalWrite(irPin,LOW);break;
        case 'b': digitalWrite(lightPin,HIGH);digitalWrite(irPin,HIGH);break;
        //IR     = Pin 12
        //Light  = Pin 13
        }
      
    
    if(packetBuffer[4]!='-')
      {
        switch(packetBuffer[4]){
        case '0': DriveSpeed=30;break; 
        case '1': DriveSpeed=55;break;
        case '2': DriveSpeed=80;break;
        case '3': DriveSpeed=105;break;
        case '4': DriveSpeed=130;break;
        case '5': DriveSpeed=155;break;
        case '6': DriveSpeed=180;break;
        case '7': DriveSpeed=205;break;
        case '8': DriveSpeed=230;break;
        case '9': DriveSpeed=255;break;
        }
      } 
      
    if(packetBuffer[0]=='-' & packetBuffer[1]=='-') //Stillsttand
        {analogWrite(E1,0);}
    if(packetBuffer[0]=='x')  
        {digitalWrite(M1,HIGH);analogWrite(E1,DriveSpeed);} //Rückwärts ?    //
    if(packetBuffer[1]=='x')                       //
        {digitalWrite(M1,LOW);analogWrite(E1,DriveSpeed);}  //Vorwärts ?     //
        
        /*old code
    if(packetBuffer[2]=='-' & packetBuffer[3]=='-')//==========================Straight 
        {;}                       
    if(packetBuffer[2]=='x')//=================================================Left 
        {;}                          
    if(packetBuffer[3]=='x')//=================================================Right                    
        {;}     
        */             // complicated version of 'atoi()', but this works too ;P
        int lenkA,lenkB;
        switch(packetBuffer[6]){
        case '0': lenkA=0;break; 
        case '1': lenkA=1;break;
        case '2': lenkA=2;break;
        case '3': lenkA=3;break;
        case '4': lenkA=4;break;
        case '5': lenkA=5;break;
        case '6': lenkA=6;break;
        case '7': lenkA=7;break;
        case '8': lenkA=8;break;
        case '9': lenkA=9;break;
        }
        
        switch(packetBuffer[6]){
        case '0': lenkB=0;break; 
        case '1': lenkB=1;break;
        case '2': lenkB=2;break;
        case '3': lenkB=3;break;
        case '4': lenkB=4;break;
        case '5': lenkB=5;break;
        case '6': lenkB=6;break;
        case '7': lenkB=7;break;
        case '8': lenkB=8;break;
        case '9': lenkB=9;break;
        }
        
        posLenk=((lenkA*10)+lenkB);
        Serial.println("PosLenk: ");
        Serial.print(posLenk);
        if(posLenk<90 & posLenk>0)
          {
          lenkServo.write(posLenk);
          //delay(5);
          }    
    }

}

I am not an authority... But I think it might be in the way yu have LenkA & LenkB declared inside loop(). I would think that they are getting reset every time through the procedure so eventually they are both passed through as 0 which sends you servo back to the 0 degrees position.

While your Computer Science instructor might not like it... declare LenkA & LenkB as globals up at the start of your code and see if it behaves better.

Thanks, but that sadly did nothing.
when i serial-print posLenk which is written to the servo, it always shows the correct values.

It must be an error with how i write the value to the servo...

const int lenk = 10;
Servo lenkServo;  // Steering-Servo
  lenkServo.attach(lenk);  // attaches the servo on pin 10 to the servo object

Pin 10 is the Ethernet shield chip select pin. You can NOT use it as the servo pin.

Oh, thanks a lot PaulS, i always thought the ethernet-shield only uses 11,12 &13 ^^

Can i use pin 1 or 2 for a servo, i heard that they are the serial-connection (hopefully not the usb).

Pins 0 and 1 are the USB/serial pins, so I wouldn't recommend using them. Pin 2 should be OK.