servo speed problem

hey guys

i'm working on a active spoiler system , so the speed on the servo( spoilers) need to be changing.

so you have a encoder that's your steering wheel, if you move you encoder the servo that needs the move the wheels move's all the time , but then you have your servo for your spoilers that one will only move if the speed of the car is above 70kM/H. i can see that it works fine. ( still servo making a weird sound)
and yes i use a external power supply with micro usb from pc, the man problem is that i want to let them move on a slow speed , when the car is driving slow because there not much of use , but when the car is moving faster so will the spoiler( servo) do aswel (spoeilerstand = spoeilerstand +((kM - servo )*10000):wink: i know this is wrong but i don't know the right combination to make it so it can count up slowly an count down slowly.

sorry for your trouble and thanks in advance

bool membit2 = LOW;
int spoeilerstand = 70;
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 = 90;
#include <LiquidCrystal.h>
const int rs = 12, en = 11, d4 = 7, d5 = 8, d6 = 6, d7 = 5;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);
bool display_beschreven = LOW;

#include <Servo.h>

Servo stuur1; 
Servo spoiler;
void setup() {

 stuur1.attach(9);
 spoiler.attach(14);
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);
cli();
TCCR2A = 0;
TCCR2B = 5;
TCNT2 = 0;
OCR2A = 250;
TCCR2A |= (1<<WGM21);
TCCR2B |= (1<<CS21);
TIMSK2 |= (1<<OCIE2A);
sei();


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

}

void loop(){
  
  digitalWrite(nachtlichtpin, nachtlicht);
 
   if (nachtlicht == HIGH){
    digitalWrite(10,HIGH);
    }    else{
      digitalWrite(10,LOW);
   }
  if (RemRead > 3){
    digitalWrite(13,HIGH);
  }
  else{
    digitalWrite(13,LOW);
  }
   if (membit1 == HIGH){
   switchstate = digitalRead(nachtlichtpin);
   if ((switchstate == HIGH) && (test == LOW)){
   nachtlicht = !nachtlicht;
    test = HIGH; 
}
   
   if (( switchstate == digitalRead(nachtlichtpin)) && (switchstate == LOW)){
    test = LOW;
   }

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

    Serial.print(spoeilerstand);       // prints a label
  Serial.print("ervo"); 



if (kM > 70){
 spoeilerstand = spoeilerstand +((kM - servo )*10000);
} else {
  spoeilerstand = 90;
}
   spoiler.write (constrain (map (spoeilerstand, -10000, 10000, 0, 180), 0, 180)); 
   stuur1.write (constrain (map (servo, -10000, 10000, 0, 180), 0, 180)); 

if (membit2 = 1){
    rem = analogRead(A2);
    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;
    }
  
   }
   membit2 = LOW;
   display_beschreven = LOW;
   }
  else{
    if (display_beschreven == LOW) {
    lcd.begin(20, 4);
    lcd.setCursor(0,0);
    lcd.print("PRESS START"); 
    delay(20); 
    display_beschreven = HIGH;
    }
 } 
 }
  void Interrupttask() {
     membit1 = HIGH;}
  void Interrupttask2() {
    membit1 = LOW;
  }
    ISR(TIMER2_COMPA_vect){
     // membit2 = HIGH;
  rem = analogRead(A2);
  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(){
  if (digitalRead(interrupt3) == HIGH ) {
    if ( digitalRead(interrupt4) == HIGH){
      servo--;
    }
    else {
      servo++;
    }
  }else{
    if ( digitalRead(interrupt4) == HIGH){
      servo++;
    }
    else {
      servo--;
    }
    
  }
}
void Interrupttask4(){
  if (digitalRead(interrupt3) == HIGH ) {
    if ( digitalRead(interrupt4) == HIGH){
      servo++;
    }
    else {
      servo--;
    }
  }else{
    if ( digitalRead(interrupt4) == HIGH){
      servo--;
    }
    else {
      servo++;
    }
    
  }
}

It is not clear why you are using interrupts

i use a external power supply with micro usb from pc,

It does not matter how the Arduino is powered (within reason) but if the servos are powered from the Arduino rather than from an external power supply (with a common GND to the Arduino) then the Arduino may be damaged because it cannot safely supply enough current.

UKHeliBob:
It is not clear why you are using interrupts
It does not matter how the Arduino is powered (within reason) but if the servos are powered from the Arduino rather than from an external power supply (with a common GND to the Arduino) then the Arduino may be damaged because it cannot safely supply enough current.

hey, i know

my grounds are connect and and my 5V power supply is only going to the servo's ( again the ground form the power supply is connect to arduino. i use the interrupts for my encoder i will post you my full code if you want but i use a interrupt call with change. because of the encoder, but where i have a problem with is that my servo's for the spoilers need to move on different speed , and the speeds need to change depending on de speed the car is driving. when the car is driving fast the servo's need to be fast , if the car is driving slow the servo's need to be slow.

You need to post the whole program so we can see how these variables are defined

if (kM > 70){
 spoeilerstand = spoeilerstand +((kM - servo )*10000);
} else {
  spoeilerstand = 90;
}

My guess is that the value is overflowing in the calculation because the variable type is too small.

It would also be a good idea to insert a Serial.print() so you can see the result of the calculation.

...R

Robin2:
You need to post the whole program so we can see how these variables are defined

if (kM > 70){

spoeilerstand = spoeilerstand +((kM - servo )*10000);
} else {
  spoeilerstand = 90;
}



My guess is that the value is overflowing in the calculation because the variable type is too small.

It would also be a good idea to insert a Serial.print() so you can see the result of the calculation.

...R

i updated the code , and yes that's the case , but i don't know how to let is count down and up at a changing speed :confused:

justG:
i updated the code , and yes that's the case , but i don't know how to let is count down and up at a changing speed :confused:

All the variables in that calculation are ints so if, during the calculation, the value exceeds 32767 things will go wrong.

I can't figure from your code what are the max an min values likely to arise in the different variables but it might be a wise precaution to change from int to long

...R

Robin2:
All the variables in that calculation are ints so if, during the calculation, the value exceeds 32767 things will go wrong.

I can't figure from your code what are the max an min values likely to arise in the different variables but it might be a wise precaution to change from int to long

...R

hi Robin

so the max value is de value of the encoder like you can see in the map function, of spoiler.write.
then the speed of how the servo move's is changing depeding de kM , well that what i try to do , i can't use a delay because it affects all the servo's , and i cant use a for loop because it will be to slow, i need to find a way to do this in a way it wont low my loop like a counting function. that happens again every time the programe finished 1 full loop .

justG:
so the max value is de value of the encoder like you can see in the map function, of spoiler.write

You tell us the numbers to save me having to go looking for them and maybe finding the wrong thing.

...R

Have you looked at using VarSpeedServo.h instead of Servo.h. That has an additional speed parameter to the write() command. I can't really understand what exactly you're calculating but if you manage to get a value that corresponds to the speed you want that will give you somewhere to use it.

Steve

Robin2:
You tell us the numbers to save me having to go looking for them and maybe finding the wrong thing.

...R

its between -10.000 and 10.000 its a encoder so i never know the exact number, but it its fixt now i tried a other way.

thanks for the help tho.

slipstick:
Have you looked at using VarSpeedServo.h instead of Servo.h. That has an additional speed parameter to the write() command. I can't really understand what exactly you're calculating but if you manage to get a value that corresponds to the speed you want that will give you somewhere to use it.

Steve

hey Steve,

its already fixt , i did not do it this way because for some reason if i use the varspeedservo libery , he seems to lose my LCD and other libery's its weird but its oke now thanks for the help man!

kind regards G