servo problem not reacting properly to encoder

hey Guys,
i have a weird issue, my setup is like this. ( i use a servo's and a encoder) my servo is on a other power supply by using a micro usb from pc, then i connect the wiring. the ground to ground arduino, and 5V to servo the signal wire from servo to arduino.

so whats the problem , the servo turns the right way and works perfectly, but then when i get to the max or min angel/number on the encoder, the servo flips out starts vibrate and loses its work position, then when i go back in the work range with the encoder the servo takes a long time to react again. and even after a while does the same thing over and over again ( even when your still in the working part with the encoder)

i hope you can help me out.

i will post the complete code ( making a active spoiler system on a car NOT COMPLETED)
i don't know what the problem is maybe my arduino is to slow because of al the interrupt, please let me know because i'm lost.

int teller = 0;
int test = LOW;
float ext = 2.0;
const int interrupt = 2;
const int interrupt2 = 3;
const int interrupt3 = 21;
const int interrupt4 = 20;
bool membit1 = LOW;
int pos = 90;
int switchstate = 0;
const int nachtlichtpin = 4;
bool nachtlicht = LOW;
int valRead = 0;
int val = 0;
int kM = 0;
int snelheid = 0;
int rem = 0;
int RemRead = 0;
int servo = 90;
int stuur = 0;
#include <LiquidCrystal.h>
const int rs = 12, en = 11, d4 = 8, d5 = 7, d6 = 6, d7 = 5;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);
#include <TimerOne.h>

#include <Servo.h>

Servo myservo; 

void setup() {
 Serial.begin(9600);
 myservo.attach(9);
pinMode(10,OUTPUT);// NACHT LICHTEN
pinMode(nachtlichtpin, INPUT); // NACHT LICHTEN AAN/UIT
pinMode(13, OUTPUT); // REM LICHTEN
digitalWrite(4,LOW);
digitalWrite(10,LOW);
digitalWrite(2,LOW);
digitalWrite(3,LOW);


attachInterrupt(digitalPinToInterrupt(interrupt), Interrupttask, RISING);
attachInterrupt(digitalPinToInterrupt(interrupt2), Interrupttask2, RISING);// eerst pin number , void regegister , dan meet moment
attachInterrupt(digitalPinToInterrupt(interrupt3), Interrupttask3, CHANGE);//encoder
attachInterrupt(digitalPinToInterrupt(interrupt4), Interrupttask4, CHANGE);//encoder

Timer1.initialize(10000);//time interupt
Timer1.attachInterrupt(interruptroutine);
}

void loop(){
Serial.print("servo");
 Serial.println(stuur);
  
  digitalWrite(nachtlichtpin, nachtlicht);
 
   if (nachtlicht == HIGH){
    digitalWrite(10,HIGH);
    }    else{
      digitalWrite(10,LOW);
   }
  
  
   if (membit1 == HIGH){//night light 
   switchstate = digitalRead(nachtlichtpin);
   if ((switchstate == HIGH) && (test == LOW)){
   nachtlicht = !nachtlicht;
    test = HIGH; 
}
   
   if (( switchstate == digitalRead(nachtlichtpin)) && (switchstate == LOW)){
    test = LOW;
   }

   lcd.begin(16,2);
   kM = snelheid/100;
   if (kM > 240){
    kM = 240;}
  
      lcd.print(kM);
      lcd.setCursor(3,0);
    lcd.print("km/h");
    delay(20);

    //if ((servo >= 0) && (servo <= 10000))
     stuur = map(servo, -10000, 10000, 0, 180);
     if ( stuur>180){
      stuur = 180;
     }
     if ( stuur<0){
      stuur = 0;
     }
      myservo.write(stuur);  
      
      
   }
  else{
    lcd.begin(16, 2);
    lcd.setCursor(0,0);
    lcd.print("PRESS START"); 
    delay(20); 
   
 } 
 }
  void Interrupttask() {// start program bit
     membit1 = HIGH;}
  void Interrupttask2() {
    membit1 = LOW;
  }
    void interruptroutine(){// lcd screen speed calcutator
    rem = analogRead(A2);
    delay(2);
    val = analogRead(A1); 
    valRead = map(val, 0, 1023, 0, 240);
    RemRead = map(rem, 0, 1023, 0 , 50);
 
     snelheid = snelheid + valRead/10 - snelheid/2000 - RemRead;
    if(snelheid <0){ snelheid = 0;
    }
    if (snelheid > 24000){
      snelheid = 24000;
    }}

void Interrupttask3(){//servo counter 
  if (digitalRead(interrupt3) == HIGH ) {
    if ( digitalRead(interrupt4) == HIGH){
      servo--;
    }
    else {
      servo++;
    }
  }else{
    if ( digitalRead(interrupt4) == HIGH){
      servo++;
    }
    else {
      servo--;
    }
    
  }
}
void Interrupttask4(){//servo counter
  if (digitalRead(interrupt3) == HIGH ) {
    if ( digitalRead(interrupt4) == HIGH){
      servo++;
    }
    else {
      servo--;
    }
  }else{
    if ( digitalRead(interrupt4) == HIGH){
      servo--;
    }
    else {
      servo++;
    }
    
  }
}

kinder Regards Gunter

The Servo library uses Timer1 so you separately using Timer1 is almost certainly causing the problems. Change your timer code or perhaps try using ServoTimer2.h instead of the standard Servo.h. Note that will mean using write() with microsecond pulse lengths rather than write(angle).

Steve

hey Steve,

Thanks for the info i will give it a try ( so if i'm following i just need to remove the time code from the servo and that will do the trick?)

kind regards Gunter

The 5V Arduino output cannot be used to power motors or servos. Use a 4xAA battery pack (or other independent power supply capable of more than 1 Ampere) instead, and connect the grounds.

     stuur = map(servo, -10000, 10000, 0, 180);
     if ( stuur>180){
      stuur = 180;
     }
     if ( stuur<0){
      stuur = 0;
     }
      myservo.write(stuur);

Can be much simpler, using the constrain() function:

      myservo.write (constrain (map (servo, -10000, 10000, 0, 180), 0, 180));

jremington:
The 5V Arduino output cannot be used to power motors or servos. Use a 4xAA battery pack (or other independent power supply capable of more than 1 Ampere) instead, and connect the grounds.

i use a external power supply , i use a micro usb to pc like i already expand.
so i don't think this is the issue.

The USB port cannot supply enough current for many servos.

jremington:
The USB port cannot supply enough current for many servos.

i see , its only for 5 servo's but i can make a battery pack just to be sure thanks.
and the problem was the time function THANKS GUYS FOR THE REALLY HELPFUL INFO!