• 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:

    http://stackoverflow.com/questions/79421­38/how-do-i-setinterval-to-call-a-functi­on-within-a-class 
    

    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;
      this.setPos(90);
    
      self.p = function ()  {
        digitalPulse(this.pin,1,this.pulse);
    //    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() {
      clearInterval();
      clearWatch();
    //  servo1 = new Servo(C5,0.63,2.34);
    //  servo1.setPos(180);
      servo2 = new Servo(C6,0.63,2.34);
      servo2.setPos(180);
      setWatch(function() { button(); }, BTN1,{"repeat":true,"edge":"rising","deb­ounce":10});
    }
    
    onInit();
    

    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.

    Thanks

    Stuart

About

Avatar for Stuart.d.d @Stuart.d.d started