• The issue you're hitting is that setWatch timestamps the events and put them in a queue to execute with JS - but as you notice it's won't execute JavaScript in the interrupt itself.

    One way around this is to actually watch the data pin for changes as well:

    var currentDat = 0;
    setWatch(function (e) { currentDat = e.state; }, dat, {repeat:true});
    setWatch(function () {
            if (i < 52) {
              a.push(currentDat);
              i++;
            }...
    

    However reading clocked data is a really common thing to want to do, so in version 1v96 I added the ability to also record a separate data source using setWatch:

    setWatch(function (e) {
            if (i < 52) {
              a.push(e.data);
              i++;
            }
    }, clk, {
        edge: "rising",
        debounce: 0,
        repeat: true,
       data : dat
    });
    

    So that should do exactly what you want.

    More info at: http://www.espruino.com/Reference#l__glo­bal_setWatch

    As @Wilberforce says, depending on what you're trying to connect to there might also be a way to set up the underlying hardware to help you out.

About

Avatar for Gordon @Gordon started