• Howdy all,

    Just need a little help getting started with a simple application that would count down at a variable rate and display progress on the Micro:Bit V2 LED Display. It would be great if on every iteration I fired the speaker on the version 2 at each transition for say 500 MS.

    I was thinking that the async nature of Espruino would allow me to do something clever with the "eventing" system, and take advantage of Espruino's support for the LED matrix display.

    Eventually I would like the variables settable from a BLE interfaced on a Smartphone, but basic functionallity first...

    Thanks!

  • I think what you need is similar to the example on https://www.espruino.com/MicroBit#micro-­bit-functionality. Something like this should work

    g = Graphics.createArrayBuffer(5,5,1);
    g.flip = function(){show(this.buffer);};
    
    function countdown() {
      x--;
      g.clear();  
      if (x<=0) { // finished
        clearInterval(intr);
        intr = undefined;
        g.fillRect(0,0,5,5);
        // long beep
        analogWrite(Microbit.SPEAKER,0.5, {freq:2500});
        setTimeout(() => Microbit.SPEAKER.reset(), 500);
      } else { // count down
        g.drawString(x);
        // beep
        analogWrite(Microbit.SPEAKER,0.5, {freq:3000});
        setTimeout(() => Microbit.SPEAKER.reset(), 100);
      }
      g.flip();  
    }
    
    var x = 10;
    countdown();
    var intr = setInterval(countdown, 1000);
    
    
  • Thank you! This kind of timely support is exactly why I own ever single piece of hardware you make...

    Works perfectly, and sounds great as well. Espruino really has command of the Micro:Bit's speaker, and this example is a good into to Espruino's (JS) "eventing system". I have an entirely new respect of/for setInterval.

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

I should think well suited to Espruino ... Micro:Bit v2 countdown timer, with announcement

Posted by Avatar for RandallSY @RandallSY

Actions