Servo turn slowly forward and reverse

Hi all,
Im trying to figure out how to make my servo turn slower both directions. As right now they turn way too fast. Wanted to check if anyone has a code, Thank you for your help.

Post your device code as you have it now.

Break the movement into smaller chunks, and move through those with a timer.

the reason i give pin 9 and 8 write(0) is after any movement is done the motors making noise, the min/max movement shouldve stop any noise, but it doesnt, so giving value 0 to both pins after command is done works.

` DEVICE

// Set Servo Degrees MINIMUM and MAXIMUM
const SERVO_MIN = 0.01;
const SERVO_MAX = 0.08;

imp.setpowersave(true)

// Set pin output, each pin represents servo PWM pin
hardware.pin9.configure(PWM_OUT, 0.02, SERVO_MIN);//servo 1
hardware.pin8.configure(PWM_OUT, 0.02, SERVO_MIN);// servo 2
// Start program with pins value 0 or LOW
hardware.pin9.write(0);
hardware.pin8.write(0);
// device will listen for “value” from the agent and write it to each of the servos
agent.on(“value”, function (value) {

imp.setpowersave(true)

local value = value * (SERVO_MAX-SERVO_MIN) + SERVO_MIN; //0.01 + (value * 0.06);
//server.log(format("servo position set to %.2f", value)); // write position in Device Logs Window
try {
    // Writes the value received from the agent/url
    hardware.pin9.write(value); 
    hardware.pin8.write(value);
    // wait for 0.5
    imp.sleep(0.8); 
    // write 0 or LOw to each of the servos PWM, to stop Tx
    hardware.pin9.write(0);
    hardware.pin8.write(0);
    imp.sleep(0.8);
    
}catch (err) {
        server.error("Error!");
    }

});

AGENT

//const OtherAgentUrl = “https://agent.electricimp.com/xxxxxxxxx"
local doorState = “open”;
local page = “” +
” " +
"" +
"" +

    "<form action='" + http.agenturl() +"' method='post'>" +
    "<h1><center style='font: bold 54px Arial'>Kitchen Blinds</center></h1>" +
    
      "<center><input type='password' autocomplete='on' name='pin' placeholder='pin'></input> <br></center>" +
	  "<center><button name='value' value='open' type='submit' style='font: bold 54px Arial;width:450px;height:500px'>Open</button>" +
	  "<button name='value' value='lock' type='submit' style='font: bold 54px Arial;width:450px;height:500px'>Lock</button></center>" +
	  //"<button style='font: bold 34px Arial'>Big text</button>" +
	"</form>" +
"</body>" +

“”;

function requestHandler(request, response) {

if("/state" == request.path){
response.header(“Content-Type”, “application/json”);
response.send(200, getState());
}else if(“POST” == request.method){

local data = http.urldecode(request.body);

if ("value" in data && data["pin"] == "") { //set code
  server.log(data["value"]);
  if(data["value"] == "open"){
      device.send("value", 0.9);                                 // Try to set this values from 0.1 to 0.9
  }else if(data["value"] == "lock"){
      device.send("value", 0);
  }
}

/*
if ("single" in data && data["pin"] == "") {
    server.log(data["single"]);
    if(data["single"] == "open"){
        device.send("single", 1);
    }else if(data["single"] == "lock"){
        device.send("single", 0);
    }
}
*/

//response.send(200, page);
response.send(200, page);

}else{
response.send(200, page);
}

};

// your agent code should only ever have ONE http.onrequest call.
http.onrequest(requestHandler);
`

no one?

You don’t appear to have any timing loop yourself in the device code, you just write out the position you want it to go to. The servos will go there as fast as they can. There is no setting on a servo or on the PWM output to say “go slow”, they pride themselves on going fast! It may be that somebody has written a class to set a speed along with a new position, I cannot help there.

If you want them so move slowly, with gradual acceleration, etc, then you need a timing loop in your device code, where you increment the servo set point a little at a time, perhaps every 0.1 seconds, until it reaches the required endpoint. You also need to remember what the last endpoint was, so you can generate a series of small steps taking you from the last endpoint to the new endpoint. (This is just an expansion of what @MikeyDJ posted)

Ditto what Dr Jack said!

We use a ladder function for filtering sensor readings and I think that the same technique could be employed here:

just roughly

  1. get the new position from the agent: Feed NP to function 2

2a) if math.fabs(NP-OP) <= Kdesired_accuracy then the servo is at the right position: nothing to do: wait for new value from agent
2b) else if NP > OP then OP = OP + Ksmall_increment
2c) else if NP < OP then OP = OP - Ksmall_increment
2d) write OP to servo
2e) wait Ksome_time and then call function 2 again

where
NP = new position
OP = old position
The size of Ksmall_increment and Ksome_time will dictate how fast OP moves
Kdesired_accuracy is the dead-band, or how much difference between OP and NP before the servo will be commanded to move

its not really elegant but I think it will do the job

thanks for the help gents, i guess i have a little learning to do hehe. Ill think on ways to do it from what you said.

I hope you are watching this thread, which has a non-blocking slow servo movement function by beardedinventor. Should help you a lot.

yes and thank you for letting me know, you don’t good people this days! Havent had a chance to play with them yet