Avatar for maze1980

maze1980

Member since Jun 2019 • Last active Jul 2019
  • 1 conversations
  • 31 comments

Most recent activity

  • in ESP32
    Avatar for maze1980

    I would be interesting to hear why you switched from Arduino/PlatformIO to Espruino.

    I'd say you could integrate the ESP32 in your PCB design.

  • in Puck.js, Pixl.js and MDBT42
    Avatar for maze1980

    My favorite logic level shifter is the BSS138. With two resistors it works bi-directional, if you use only one resistor it's unidirectional. It doesn't invert the signal.
    "bi-directional-logic-level-converter-ho­okup-guide" will find relevant documentation.

  • in Interfacing
    Avatar for maze1980

    @Robin: If it makes sense or not we don't know. Might be doing some multiplexing of pins, due to limited IO pins. The key message is to use setInterval/setTimeout instead of while/for.

  • in Interfacing
    Avatar for maze1980

    You don't need state machines for this simple case.

    @Robin: Refering to post #1 and #4, to replace WHILE and FOR with setInterval/setTimeout/clearInterval, use this code when the button is pressed (first setWatch function, line 1) instead of WHILE:

    yolo = setInterval(function() {  //the watch displays the hour
          display(1, hourFirstDigit);
          display(2, hourSecondDigit);    }, 50); //with 50ms if may flicker
    

    When the button is released (second setWatch function , line 11) call clearInterval(yolo) and allClear(). Then show the minutes again with yolo = setInterval(), and use setTimeout to clear that interval after 300ms with clearInterval(yolo) again insted of FOR.

    If the displays flickers at 50ms reduce the interval to maybe 25 or 20ms.

  • in Interfacing
    Avatar for maze1980

    Introducing setTimeout for the hour display makes the code hard to understand. Why do you want to keep the WHILE loop that is making things complicated?

  • in Interfacing
    Avatar for maze1980

    the problem is, whenever I press/release the button, the piezo speaker
    beeps until the LEDs are cleared

    That's what you program is supposed to do. There is no multitasking in Javascript, just a single thread. As such, the timer to stop the beep expires while your WHILE loop is running, but the code to stop the beep only gets executed when the CPU is idle. That's the case when the function containing the WHILE loop is finished. (There might be better and technical more accurate explanations.)

    In case you need to write

    display(1, hourFirstDigit);
        display(2, hourSecondDigit);
    

    repeatedly then do this using a function. Start the function with var yolo=setInterval(...) and stop it with clearInterval(yolo). Don't use WHILE or FOR in such cases.

    Regarding

    pretty simple, right?

    No, it's not simple. You need two function while one function with 3 lines would do.

  • in Interfacing
    Avatar for maze1980

    Actually the prepare function isn't used and needed, the array is initialized with 0 values. I should have removed the function. If any other value but zero was needed .fill would be faster and shorter than using a loop, indeed. Thanks for the info.

  • in Interfacing
    Avatar for maze1980

    Thanks Gorden. It's a nice function and really fast. Just tried it with a pixel runner left/right.

    //runner (left/right)
    
    var pin = NodeMCU.D1;
    pinMode(pin, "output");
    var neopixel = require("neopixel");
    const leds = 64;
    
    const ledsx3 = 3*leds;
    //array with rgb values as Uint8ClampedArray
    const base = new Uint8ClampedArray(ledsx3);
    var show = new Uint8ClampedArray(ledsx3);
    var dot1 = new Uint8ClampedArray(3);
    dot1[0] = 128;
    dot1[1] = 128;
    dot1[2] = 128;
    var dot2 = new Uint8ClampedArray(3);
    dot2[0] = 128;
    dot2[1] = 0;
    dot2[2] = 0;
    var dot3 = new Uint8ClampedArray(3);
    dot3[0] = 0;
    dot3[1] = 0;
    dot3[2] = 128;
    
    //
    //prepare
    //
    var prepare = function() {
      for (var i=0; i<ledsx3; i++) {
        base[i] = 0;
      }
    };
    
    //
    //animate
    //
    var dir1 = 3;
    var pos1 = 0;
    var dir2 = -6;
    var pos2 = leds;
    var dly3 = 0;
    var pos3 = 3*Math.round(Math.random()*leds);
    var start, duration;
    var animate = function() {
      start = getTime();
      show.set(base);
      show.set(dot1, pos1);
      show.set(dot2, pos2);
      show.set(dot3, pos3);
      neopixel.write(pin, show);
      pos1 = pos1 + dir1;
      if (pos1 >= ledsx3) dir1 = -3;
      else if (pos1 <= 0) dir1 = 3;
      //duration of all code above: 0.006s
      pos2 = pos2 + dir2;
      if (pos2 >= ledsx3) dir2 = -6;
      else if (pos2 <= 0) dir2 = 6;
      //duration of all code above:  0.007s
      dly3++;
      if (dly3 === 7) {
        dly3 = 0;
        pos3 = 3*Math.round(Math.random()*leds);
      }
      duration = getTime()-start;
      //duration of all code above:  0.008s
    };
    
    
    var onInit = function() {
      setInterval(function() { animate(); }, 10);
    };
    
    onInit(); //don't save() this line
    

    Execute at your on risk, epilepsy warning.

  • in Puck.js, Pixl.js and MDBT42
    Avatar for maze1980

    The diodes are a simple hack.
    The proper way would be a hot swap controller, e.g. LTC4227 (check the datasheet for allowed voltages and and current consumption). It senses the input voltages of multiples sources and controls a transistor to connect the source with the highest input voltage to the output.

  • in Interfacing
    Avatar for maze1980

    I know, it's a copynpaste of the "else if.." line for about 10 times to do compare the speeds (mainloop with rgb color calculations vs mainloop with merging arrays).

Actions