Simple digital clock

Posted on
  • My 7 years old daughter have made (with my help) big digital clock. We use 8x32 matrix of ws8212b LED and small ESP8266 board with 5-to-3.3 V LDO on board. LED matrix have separate 2-wire power connector and 3-wire control connector, so we can connect 5V power adaptor to first one and ESP8266 to second one only (using 3 jumper wires - to GND, МСС and GPIO2).

    Then very simple program convert the matrix to digital clock

    var neopixel = require("neopixel");
    E.setTimeZone(3);
    require("Wifi").setSNTP("0.pool.ntp.org"­,0);
    
    require("Font6x8").add(Graphics);
    var g = Graphics.createArrayBuffer(8,32,24,{zigz­ag:true,color_order:'grb'});
    g.setRotation(1,1);
    
    var att=6.9;
    var d=Math.pow(2,att);
    g.setColor(0,0,1/d);
    
    var oldTime, newTime;
    g.setFont6x8();
    
    function drawTime() {
      newTime=Date().toString().substr(16,5);
      if(oldTime != newTime) {
        oldTime=newTime;
        g.clear();
        g.drawString(newTime);
        neopixel.write(D2, g.buffer);
      }
    }
    
    setInterval(drawTime,1000);
    
    neopixel.write(D2, g.buffer);
    

    Some comments:
    Last line are to fix state of LED matrix. It is enough to write first LED (even first logical LED - 1 byte) only but it is harder - we need separate (very small) buffer to do that. So we write all matrix. The line may be removed but first ouput will be incorrect.
    We have found that LED matrix is in fact 8x32 zigzag (first line is right-to-left, second one is left-to-right etc.) So we have added g.setRotation() to use it as 32x8 display.
    I have added att and d vars to control LED brightness more intuitively - now brightness looks to be changed linearly with att change.
    It need a few seconds to get correct time from Internet, so time displayed is strange after power on.

  • Nice - thanks for posting it up! That first write for neopixels is a pain - it's caused by the pin having been floating before neopixel.write was called.

    You might like this function for use with setColor: http://www.espruino.com/Reference#t_l_E_­HSBtoRGB

    It'd be really neat to change the hue by a little bit every second :)

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

Simple digital clock

Posted by Avatar for SergeP @SergeP

Actions