setWatch() on analog input?

Posted on
  • Are there plans to extend setWatch() to allow reacting to an analog input value? Does the chip the Espruino uses even have a good way to do that? Or is it better to wire up something to convert that event into a digital pulse and set a watch on that?

    For example, to put a watch on the results of an analog sensor, so if it went over a certain value, it'd call the function? (This is easy enough to handle w/discrete components - a '339 and a pair of dividing resistors to make a reference can handle detecting if it's above or below a threshold)

    Or to use one of those ultra-cheap chinese "vibration sensors" to detect shocks? (the 20 cent ones that look kinda like blue resistors. When tapped or shaken, the resistance changes randomly). This I think could also be used with a 339 to get a digital output from it in the event of changes.

  • I'm afraid there is no way to do this right now - the chip may have some functionality for analog compare, but it isn't as easy as on digital io.

    If you don't care about power consumption then just use setInterval to check every so often - otherwise I'm afraid you will have to create some analog circuit that will be capable of tripping the analog input.

  • Here is a script I wrote to watch analog input at¬≠ster/analog_read_watch

    // constructor parameters (watchPin, eventFunction, timeout, difference, repeat)
    var obj = new AnalogReadWatch(C4, function(data) { print(data); }, 1000, 0.01, true );

  • Nice, thanks!

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

setWatch() on analog input?

Posted by Avatar for DrAzzy @DrAzzy