Help needed for properly servo coding

hi
im a total beginner with the coding stuff and im trying to create a simple code with agent to control a servo from certain position to another (min to max) using http req

i tried modifying the agent code for LED (from the examples page)
but im clearly doing it wrong cause i dont know what to add or take out of the code

below is the code i used (seems ok until i get Internal Server Error: the index ‘ServoState’ does not exist (line 15))

agent:


server.log("Turn servo On: " + http.agenturl() + “?servo=1”);
server.log("Turn servo Off: " + http.agenturl() + “?servo=0”);

function requestHandler(request, response) {
try {

if (“servo” in request.query) {

if (request.query.servo == “1” || request.query.servo == “0”) {

local servoState = request.query.servo.tointeger();

device.send(“servo”, ServoState);
}
}

response.send(200, “OK”);
} catch (ex) {
response.send(500, "Internal Server Error: " + ex);
}
}

http.onrequest(requestHandler);


device


const SERVO_MIN = 0.03;
const SERVO_MAX = 0.1;

servo <- hardware.pin7;
servo.configure(PWM_OUT, 0.02, SERVO_MIN);

function SetServo(ServoState) {
local scaledValue = ServoState * (SERVO_MAX-SERVO_MIN) + SERVO_MIN;
servo.write(scaledValue);
}

function SetServoDegrees(ServoState) {
local scaledValue = (ServoState + 81) / 161.0 * (SERVO_MAX-SERVO_MIN) + SERVO_MIN;
servo.write(scaledValue);
}

position <- 0;

function setServo(ServoState) {
server.log("Set servo: " + ServoState);
led.write(ServoState);
}
agent.on(“servo”, setServo);

this is the servo im using

I need some directions here…

thanks!!

Here is some code with a slider interface that I wrote a while ago, and I would recommend reading through this documentation.

Try changing …
device.send("servo", ServoState);
to …
device.send("servo", servoState);
in your agent code. I.e. change the case of the variable to match the local you previously declared. (Variables are case sensitive).

(You’re also going to need to define your ‘led’ pin in the device code).

thank you both for your help :}

i got it working with MakeDeck’s code

ill keep trying to code myself and see where im going with this

thanks again!

If you use an iOS device, take a look at the Pitchfork app. You can easily set it up to control servo with the buttons, switches and sliders.