Avatar for Stuart.d.d

Stuart.d.d

Member since Jul 2014 • Last active Aug 2014
  • 2 conversations
  • 7 comments

Most recent activity

  • in General
    Avatar for Stuart.d.d

    The other issue here is one of Isolation. A relay gives you two electrically separate circuits. The relay coil, the low voltage (safe) side of the circuit and the contacts which can switch hi voltage hi current loads (depending on the relay spec).

    An SCR can switch hi voltage hi current loads but the input (gate) would need to be connected to the low voltage side of the circuit. If this was switching mains or hi voltages it would mean that the Espruino itself would be at dangerous voltages.

    The solution is to use an optical isolator. The Espruino drives a small LED and a Photo Transister provides the gate voltage to drice the SCR.

    There are several packages that contain both optical isolator and SCR that can operate as Solid State Relays.

    Search articals on SCR, Triac, Thyristors and Solid State Relays.

    A big advantage of a relay is the configuration of the contacts, often multiple sets of isolated contacts with normally open and normally closed switches. An SCR is a single normally open switch.

    The advantages of an SCR are the speed at which it can operate and that there so it can be uses for power control such as light dimmers and large motors.

    Hope this helps:)

    Stuart

  • in JavaScript
    Avatar for Stuart.d.d

    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.

    Stuart

  • in JavaScript
    Avatar for Stuart.d.d

    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

  • in General
    Avatar for Stuart.d.d

    OK - Thanks for sorting it out.

    This is really useful if when the code does a save() all state is saved, it means that after a power down and reboot the state is restored.

    I assume the local variables in onInit() are not saved, just the globals.

    Thanks for the response. Very encouraging.

    All I need now is for Espruino to run bare metal on a RaspberryPi...... Put it on my wish list!

  • in General
    Avatar for Stuart.d.d

    Thanks for the reply.
    The formatting went away when I returned to the forum later. Seemed to sort itself out!

    Thanks for the reformatted code I will give it a try. I can see the logic now so it makes sense. Coming from a Java background you may see where my confusion stems from.

    My process was to upload the code, then press the button, then save(), then do the dump().

    Am I right is saying that the current value of the var's is wirtten to flash with the source when a save() is run and dump inlines the values with the source.
    A load(), reset... then reads the state, including the saved values, back to ram.
    You would normally expect a reset to ZERO all state and start running the source from the top. It's a bit mind blowing especially if the app has many states.

    So onInit() is run on reset, reset() and power-up. Is it run on load() as well.

    I do think the coding examples need to show this feature of the language.

  • in General
    Avatar for Stuart.d.d

    Sorry about the mess but the formatting seems to be messed up. If yo uneed separate copies of the files I can provide.

    Stuart

  • in General
    Avatar for Stuart.d.d

    Hi
    Love the Espruino. Having loads of fun seeing what it can do. However have issue that is confusing me. Can somone help.

    I wrote the following code...

    var list = [
          {"b":0,"g":0,"r":0,"s":0},
          {"b":0,"g":0,"r":1,"s":45},
          {"b":0,"g":1,"r":0,"s":90},
          {"b":0,"g":1,"r":1,"s":135},
          {"b":1,"g":0,"r":0,"s":180},
          {"b":1,"g":0,"r":1,"s":120},
          {"b":1,"g":1,"r":0,"s":90},
          {"b":1,"g":1,"r":1,"s":60}
        ];
    var MIN_SERVO = 0.63;
    var MAX_SERVO = 2.34;
    var ONE_DEG = (MAX_SERVO - MIN_SERVO) / 180;
    var ZERO_DEG = MIN_SERVO;
    var step = 0;
    
    function posServo(deg) {
      return E.clip(MIN_SERVO + (ONE_DEG * deg), MIN_SERVO, MAX_SERVO);
    }
    
    var servoPos = posServo(0);
    
    function stopTimer() {
      if (timer) {
        clearInterval(timer);
        timer = undefined;
      }
    }
    
    function nextLights() {
      digitalWrite(LED1, list[step].r);
      digitalWrite(LED2, list[step].g);
      digitalWrite(LED3, list[step].b);
      servoPos = posServo(list[step].s);
      step = (step +1) % list.length;
    }
    
    var timer = setInterval(nextLights, 500);
    
    setWatch(function() {
      stopTimer();
      nextLights();
    }, BTN1, {"repeat":true,"edge":"rising","debounce­":10});
    
    var sp = setInterval("digitalPulse(C5,1,servoPos)­", 20);
    

    This works fine, my lights blink, my servo moves...
    When I save() and reset the code does not start! However if I press the button it does get to the setWatch function and the device works from there.
    If I do a dump() however there are some confusing changes to the code.

    var list = [
          {"b":0,"g":0,"r":0,"s":0},
          {"b":0,"g":0,"r":1,"s":45},
          {"b":0,"g":1,"r":0,"s":90},
          {"b":0,"g":1,"r":1,"s":135},
          {"b":1,"g":0,"r":0,"s":180},
          {"b":1,"g":0,"r":1,"s":120},
          {"b":1,"g":1,"r":0,"s":90},
          {"b":1,"g":1,"r":1,"s":60}
        ];
    var MIN_SERVO = 0.63;
    var MAX_SERVO = 2.34;
    var ONE_DEG = 0.0095;
    var ZERO_DEG = MIN_SERVO;
    var step = 5;
    function posServo(deg) {
      return E.clip(MIN_SERVO + (ONE_DEG * deg), MIN_SERVO, MAX_SERVO);
    }
    var servoPos = 2.34;
    function stopTimer() {
      if (timer) {
        clearInterval(timer);
        timer = undefined;
      }
    }
    function nextLights() {
      digitalWrite(LED1, list[step].r);
      digitalWrite(LED2, list[step].g);
      digitalWrite(LED3, list[step].b);
      servoPos = posServo(list[step].s);
      step = (step +1) % list.length;
    }
    var timervar sp = 2;
    setInterval("digitalPulse(C5,1,servoPos)­", 19.99950408935546875);
    setWatch(function () {
      stopTimer();
      nextLights();
    }, "B12", { repeat:true, edge:'rising' });
    digitalWrite(A15,1);
    digitalWrite(C5,0);
    

    var step is set to 5, somtimes 7... never 0;
    var servoPos = 2.34. However if I run from console I get the following:

    posServo(0)
    =0.63
    

    also the line

    var timer = setInterval(nextLights, 500);
    

    is re-written as:

    var timervar sp = 2;
    setInterval("digitalPulse(C5,1,servoPos)­", 19.99950408935546875);
    

    and the var timer is not set and timervar is introduced. This is a mix of

    var timer = setInterval(nextLights, 500);
    

    and

    var sp = setInterval("digitalPulse(C5,1,servoPos)­", 20);
    

    Finally the setWatch parameters are missing the "debounce":10

    I updated to 1v67.. Read some entries in the forum and downgraded to 1v66 but it still does not work.

    When I received Espruino I updated straight away and did not see and evidence of this. that was about a week ago. however in my excitement I did not note which version it upgraded to.

    Currently it works from ram not from flash. If I reset() in the console(), press the reset button, disconnect battery and re-connect or load() in the console it does NOT run.

    It tried minification on and off. It still does not work...

    Can you please help...

    Stuart

Actions