Hello All,
I am trying out a simple sketch to make a servo move back and forth between it's extents whilst controlling its speed with a pot. The sketch mostly works, however I am having a few problems with the "wait" function in the write command. If you are not familiar the library's API (GitHub - thorikawa/VarSpeedServoSam: Another Arduino servo library with the ability to change the speed for boards with SAM processors.) describes it so:
write(value, speed, wait) - wait is a boolean that, if true, causes the function call to block until move is complete
Here is my sketch:
#include <VarSpeedServo.h>
VarSpeedServo myServo1;
const int potPin = A0;
int potVal;
int servoSpeed;
void setup() {
myServo1.attach(9);
Serial.begin(9600);
}
void loop() {
potVal = analogRead(potPin);
servoSpeed = map(potVal, 0, 1023, 20, 255);
//value of 0 on the PWM freezes the servo & sketch - something to do with the library
myServo1.write(1, servoSpeed, true);
//true command only allows full 180° rotation at low speeds <200
potVal = analogRead(potPin);
servoSpeed = map(potVal, 0, 1023, 20, 255);
//find a way of not having to repeat this...
myServo1.write(180, servoSpeed, true);
//true command only allows full 180° rotation at low speeds <200
Serial.print("potVal: ");
Serial.print(potVal);
Serial.print(", servoSpeed: ");
Serial.println(servoSpeed);
}
My issue is with the boolean "wait" function:
A) I don't understand how this works at different speeds as there is no feedback loop from the servo (3pin). Does it take the speed and guess how long 180° is going to take and then move onto the next command?
B) At high speeds, anything above 200, it does not allow for the servo to complete the 180° before changing direction - why is this?
The boolean function also stalls the sketch like a delay would. I was hoping to incorporate a schedule function like this Operate servo without using delay - Programming Questions - Arduino Forum (similar to the blink without delay sketch). But I am not sure if I am able to use this with the 'wait' function - I'm guessing not.
Any ideas, thoughts and solutions are welcome - I am still a bit of a beginner and tried to research this particular problem but was unable to find anything.
Cheers
edit: I see here that at full speed the wait command goes to the next one before the operation is complete - note the servos returning at full speed to 10°.