Timer switch with Espruino board?

Posted on
  • Good afternoon,

    I'm a COMPLETE newbie in both Espruino and Electonics in general.
    My purpose is quite simple: I'd like to use the Espruino as a timer switch.

    More precisely, I have:

    1. a (JSON-formatted) list of timestamps
    2. a (completely independant, self-powered) circuit I'd like to close (I'd like to "put a wire" between two of its pins) for 1 second at those timestamps
    3. a battery holder (http://www.aliexpress.com/item/F04639-4-AA-Battery-Box-Holder-of-Receiver-4-8v-W-JST-connector-As-Ek2-0914/900092076.html) with 4 AA batteries

    Is this feasible with Espruino?

    Many thanks for your help, or directions for help

  • Hi,

    I reckon what you'd probably want to short out the two pins in the second circuit is a Relay - which really does physically connect two contacts together when you want it to. There are some relay modules that you can get really cheap now that are really easy to drive from Espruino.

    Just connect one of those up to a pin on Espruino - then it's basically the software... Something like this should work:

    var RELAY_PIN = A0; // where you connected the relay
    var timeStamps = [
      // Ordered list of timestamps (as seconds since 1970)
    ];
    
    function connectCircuit() {
      digitalWrite(RELAY_PIN, 0); // turn relay on - generally the logic is inverted
      setTimeout(function() {
        // relay off after 1 sec
        digitalRead(RELAY_PIN); // some relays are 5v, so you need to make the pin an input
        queueTimer(); // and now see if there are other timer tasks
      }, 1000); 
    }
    
    function queueTimer() {
      var nextTime = timeStamps.shift(); /// remove first element from the array
      if (nextTime===undefined) return; // no more timeStamps?
      var delaySecs = getTime()-nextTime;
      if (delaySecs<=0) // maybe it's already happened?  Do it now
        connectCircuit();
      else // otherwise do it after a delay
        setTimeout(connectCircuit, delaySecs*1000);
    }
    

    You'd want to turn on the 'set time' option in the Web IDE's settings though, to ensure that when you write code to Espruino it sets the time on it up correctly.

    Sometimes having a 'proper' timestamp is overkill though, and you might just want to have 'seconds since a button was pressed'. That's not a huge tweak though.

    Hope that helps!

  • Thanks a lot for this very complete answer Gordon.
    I'll share my results.
    J.

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

Timer switch with Espruino board?

Posted by Avatar for user57476 @user57476

Actions