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!
Hello! It looks like you're interested in this conversation, but you don't have an account yet.
Getting fed up of having to scroll through the same posts each visit? When you register for an account, you'll always come back to exactly where you were before, and choose to be notified of new replies (either via email, or push notification). You'll also be able to save bookmarks and upvote posts to show your appreciation to other community members.
With your input, this post could be even better 💗
Register Login