Servo speed



  • Hi guys

    I am in the process of automating my central heating system (network gas boiler for heaters and tap water)

    A part of this project is to use a RC servo to control the thermostat knob

    Rotation of the knob has to be done much slower than the average servo speed

    Using delay() in sketches in order to slow down servo travel is a non-sense

    I tried VarSpeedServo library : but servo movement wasn't neat and it looks like it's using delay()...

    So i wrote that little bit of sketch:

    #include <Servo.h>
    Servo myservo;  // create servo object to control a servo
    #define servoMin 10
    #define servoMax 170
    int servoTarget = 90;               // incoming serial data - servo target position
    int servoPos    = 90;               // actual servo position
    unsigned long servoDelay = 1000;    // time delay between 2 servo position increments
    unsigned long servoPrevMillis;
    
    void setup() {
      myservo.attach(9);    // attaches the servo on pin 9 to the servo object
      Serial.begin(9600);   // opens serial port, sets data rate to 9600 bps
      Serial.print("Servo at ");
      Serial.println(servoTarget);
      myservo.write(servoTarget);
    }
    
    void loop() {
      if (Serial.available() > 0)  {
        servoTarget = Serial.parseInt();
        Serial.print("I received: ");
        Serial.println(servoTarget);
      }
      if ( (servoTarget != servoPos)
           && (servoTarget < servoMax) && (servoTarget > servoMin)
           && ( (millis() - servoPrevMillis) >= servoDelay) ) {
        if ( servoTarget > servoPos )
          servoPos++;
        else
          servoPos--;
        myservo.write(servoPos);
        delay(10);                     // wait for the servo to get there
        Serial.println(servoPos);
        servoPrevMillis = millis();
      }
    }
    

    As i am quite happy with the result i though i would share it!


Log in to reply
 

Suggested Topics

  • 1
  • 2
  • 2
  • 2
  • 5
  • 10

49
Online

11.2k
Users

11.1k
Topics

112.5k
Posts