• Hi

    I am attempting to create an Object that encapsulates a servo motor and I seen to have come unstuck!

    The following the significant part of my code for the Servo object:

    Note ref:


    is where I picked up the pattern for setting up 'p' and the setInterval function.

    function Servo(pin, min, max) {
      self = this;
      this.min = min;
      this.max = max;
      this.onedeg = (max - min) / 180;
      this.pos = 0;
      this.pulse = min;
      this.pin = pin;
      self.p = function ()  {
    //    print(this.pin);
      this.interval = setInterval(function() { self.p(); }, 20);
    Servo.prototype.setPos = function (deg)  {
      this.pos = E.clip(deg, 0, 180);
      this.pulse = E.clip(this.min + (this.onedeg * this.pos), this.min, this.max);
    function onInit() {
    //  servo1 = new Servo(C5,0.63,2.34);
    //  servo1.setPos(180);
      servo2 = new Servo(C6,0.63,2.34);
      setWatch(function() { button(); }, BTN1,{"repeat":true,"edge":"rising","deb­ounce":10});

    Hope I got the formatting right this time !-)

    This runs fine for servo1 (servo2 commented out).
    It runs fine for servo2 (servo1 commented out).

    However if I instantiate both server1 and servo2, server1 does nothing and server2 judders and moves eratically.

    If I add the 'print(this.pin);' to the 'p' function:
    It runs fine for servo1 (servo2 commented out) I get C5 echoed to the console (this is correct).
    It runs fine for servo2 (servo1 commented out) I get C6 echoed to the console (this is correct).

    However if I instantiate both server1 and servo2, server1 does nothing and server2 judders and moves eratically I get C6 echoed to the console (I would expect C6 and C5 in roughly equal amounts).

    My conclusion is that I am instantiating two objects but the 'p' method is piching the values from the last one instantiated.

    I am also working on stepper motors and radio recivers and would like to define objects to encapsulate their behaviour as well.

    Could you please help me understand what is going on and how to fix this. Eventually I would like to operate several servos this way.



  • You're defining self as a global variable. I don't think you want to do that

  • As DrAzzy says, you need:

    var self = this;


    self = this;

    That's almost certainly your problem... Personally though I'd also skip defining the small second function, and do:

    var self = this;
      this.interval = setInterval(function ()  {
    //    print(self.pin);
      }, 20);

    That's the pattern we tend to use for modules, and while it involves writing self and not this, it is a bit more efficient both in speed and memory usage.

  • Wow...
    That worked brilliantly :-)

    Now I just need to understand why!

    I understand the logic for the 'var self = this;'. I read the article! but coming from a proper Java background it's all very different.

    I will keep reading.

    Thanks for the help.


  • Post a reply
    • Bold
    • Italics
    • Link
    • Image
    • List
    • Quote
    • code
    • Preview

Javascript objects, constructors, intervals , servos and erratic behaviour

Posted by Avatar for Stuart.d.d @Stuart.d.d