Erratic behavior from servo?


#1

I am trying to get a servo to move to 2 positions. Simple enough. I made a function, specify the angle, servo is to move there.
Simple enough right?

Well, not quite. Have a look at this video and see what is happening:

The code:

    Servo myServoName;
    int pos = 0;

    void setup() {
        myServoName.attach( A5 );
        Spark.function("setpos", setPosition);
        Spark.variable("getpos", &pos, INT);
    }

    void loop() {
        /*
        myServoName.write(0);
        delay(1000);
        myServoName.write(180);
        delay(1000);
        */
    }

    int setPosition(String posValue) {
        pos = posValue.toInt();
        myServoName.write(pos);
        return 0;
    }

The other servo works just fine.

Any ideas?


#2

What type servo is that?
And what is “the other servo”?
Have you tried myServoName.writeMicroseconds() instead?

BTW, you are still using the deprecated Spark object and also the deprecated three three parameter version for Particle.variable()
Just for the sake of it, I’d also return pos; instead of 0 to get some useful feedback for the function call.


#3

Oh shucks, copied some old code over, hence the deprecated objects.
Let me see what writeMicroseconds does and if it helps.


#4

Yup, that did it!