You are reading a single comment by @MaBe and its replies. Click here to read the full conversation.
  • Here is one more I named LED. It is a very simple class the can handle three led states:

    on / off / blink( duration in ms for off and on)

    Some led are switched on with Pin.write(1) and some with Pin.write(0) thats why the constructor needs two parameter, one for the pin and one for the value used to switch the led on.

    class LED {
          constructor(led, on) {
            this.led = led;
            this.ON = on;
            this.id = 0;
          }
          on() {
            this.off();
            this.led.write(this.ON);
          }
          off(){
            if ( this.id ) {
              clearInterval(this.id);
              this.id = 0;
            }
            this.led.write(!this.ON);
         }
          blink(t){
            this.off();
            this.id = setInterval(function(led){led.toggle();}­,t, this.led);
          }
    }
    

    Code to test class LED on a Pico

    
    /* Pico 
    LED1  red
    LED2  green
    */
    
    led1 = new LED(LED1, true);
    led1.blink(500);
    
    led2 = new LED(LED2, true);
    led2.blink(250);
    
    // stop after 10 sec
    setTimeout(function(){led1.off();led2.of­f();},1E4);
    

    Hope it is useful ;-)

About

Avatar for MaBe @MaBe started