You are reading a single comment by @Alex and its replies. Click here to read the full conversation.
  • Hi All,

    I have two espruino's both with:

    http://www.espruino.com/Clocks

    The 32Khz Watch Crystal soldered onto it. I tried the clock example from that page as well as my own implementation and there is quite a big drift: depending on which espruino I use, 1 - 3 seconds on the minute.

    Did anyone else notice this?

    The program I want to use this for is a simple light switch. On in the morning, off in the afternoon.

    
    var hours = 21;
    var minutes = 58;
    var seconds = 0;
    
    var port = {pin:C6, brightness:0};
    var fadeInterval = 5;
    
    function timestep() {
      seconds += 1;
      if (seconds == 60) {
        seconds = 0;
        minutes += 1;
      }
      if (minutes == 60) {
        minutes = 0;
        hours += 1;
      }
      if (hours == 24) {
        hours = 0;
      }
      updateBehaviourFade();
    }
    
    /**
     * This fades in the lights on the port.pin
     * @param {Number} fadeInterval | time in seconds to fade in
     */
    function fadeIn(fadeInterval) {
      if (port.brightness == 0) {
        analogWrite(port.pin,1/255);
    
    
        var fadeInID = setInterval(function () {
            port.brightness += 1/255;
            if (port.brightness > 1) {
              analogWrite(port.pin,1);
              port.brightness = 1;
              clearInterval(fadeInID);
            }
            else {
              analogWrite(port.pin,port.brightness);
            }
          },
          fadeInterval*1000/255
        );
      }
    }
    
    /**
     * This fades out the lights on the port.pin
     * @param {Number} fadeInterval | time in seconds to fade out
     */
    function fadeOut(fadeInterval) {
      if (port.brightness == 1) {
        analogWrite(port.pin,1 - 1/255);
        var fadeOutID = setInterval(function () {
            port.brightness -= 1/255;
            if (port.brightness < 0) {
              port.brightness = 0;
              analogWrite(port.pin,0);
              clearInterval(fadeOutID);
            }
            else {
              analogWrite(port.pin,port.brightness);
            }
          },
          fadeInterval*1000/255
        );
      }
    }
    
    function updateBehaviourFade() {
      if (hours >= 8 && port.brightness == 0) {
        fadeIn(fadeInterval);
      }
      if (hours >= 23 && port.brightness == 1) {
        fadeOut(fadeInterval);
      }
     }
    
    function onInit() {
      analogWrite(C6,0);
      setInterval(timestep,1000);
    }
    

    Would the fading interfere with the clock?

    Regards,

    Alex

    PS. also the fading isn't linear.. it seems to be faster and slower during the fading cycle.

About

Avatar for Alex @Alex started