• I'm using a string of LED lights and creating various 'modes' I wan't to change to when clicking button 1. All works fine, but when I attach a battery everything freezes. Then when I try and save, then reattach to power nothing happens.

    What I need is a way for the battery to be attached and functionality to remain. I'm looking at documentation but getting confused. I am a Noob with this! Thanks in advance!

    SPI2.setup({ baud: 3200000, mosi: B15 });
    
    /*
    * Config
    */
    
    const totalLEDs = 25;
    
    /*
    * Light Arrays
    */
    
    const offLights = (function () {
      var lights = new Array(totalLEDs * 3);
      lights.fill(0);
      return lights;
    }());
    
    const duskDawnLights = (function () {
      var lights = [];
      for (var i = 0; i < totalLEDs; i++) {
        lights.push(255, 10, 10);
      }
    
      return lights;
    }());
    
    const daylights = (function () {
      var lights = [];
      for (var i = 0; i < totalLEDs; i++) {
        lights.push(150, 255, 255);
      }
    
      return lights;
    }());
    
    const randomFireLights = (function () {
      var colours = [
        [255, 0, 0], [255, 10, 10], [200, 10, 0], [180, 5, 10], [150, 20, 0], [130, 20, 0],
      ];
      return () => {
        var lights = [];
        for (var i = 0; i < totalLEDs; i++) {
          lights = lights.concat(colours[Math.floor(Math.ra­ndom() * colours.length)]);
        }
    
        return lights;
      };
    }());
    
    /*
    * Light algorithms/functions
    */
    
    const playFireLights = () => {
      setInterval(() => {
          updateLEDLights(randomFireLights());
        }, 75);
    };
    
    const playOffLights = () => updateLEDLights(offLights);
    
    const playDuskDawnLights = () => updateLEDLights(duskDawnLights);
    
    const playDaylights = () => updateLEDLights(daylights);
    
    /*
    * LED light controls
    */
    
    function updateLEDLights(lights) {
      SPI2.send4bit(lights, 0b0001, 0b0011);
    }
    
    /*
    * Utils
    */
    
    function getLightTransitionValues(lightsFrom, lightsTo) {
      var transitionLights = [];
      for (var i = 0; i < lightsFrom.length; i++) {
        if (lightsFrom[i] < lightsTo[i]) {
          transitionLights[i] = lightsFrom[i] + 1;
        } else if (lightsFrom[i] > lightsTo[i]) {
          transitionLights[i] = lightsFrom[i] - 1;
        } else {
          transitionLights[i] = lightsTo[i];
        }
      }
    
      return transitionLights;
    }
    
    function lightsAreEqual(a, b) {
      for (var i = 0; i < a.length; i++) {
        if (a[i] !== b[i]) {
          return false;
        }
      }
    
      return true;
    }
    
    const transitionLightValues = (function () {
      let transitionInterval = null;
      return function (from, to) {
        if (transitionInterval) {
          return transitionInterval;
        }
    
        let tempLights = from;
        transitionInterval = setInterval(function () {
          tempLights = getLightTransitionValues(tempLights, to);
          updateLEDLights(tempLights);
          if (lightsAreEqual(tempLights, to)) {
            clearInterval();
            transitionInterval = null;
          }
        }, 100);
      };
    }());
    
    setWatch((function () {
      const states = [playDuskDawnLights, playDaylights, playFireLights,
        playDuskDawnLights, playOffLights,
      ];
      let currentStateIndex = 0;
    
      return () => {
        clearInterval();
        interval = states[currentStateIndex]();
        currentStateIndex = (currentStateIndex + 1 >= states.length) ? 0 : currentStateIndex + 1;
      };
    }()), BTN, { edge: 'rising', repeat: true, debounce: 10 });
    
    
  • Hi!

    Which board are you using? Do you save just by typing save() after an upload, or by some other means?

    What you're doing looks fine. Have you tried adding blinking some of the on-board LEDs on and off to see if Espruino itself thinks it is doing something? It might be an electrical issue rather than a software one.

    Also, a while back we changed to using neopixel.write for sending data to neopixels. What you're doing should still be fine, but if you use neopixel.write then it's a bit more readable, as well as more portable to different platforms (like Puck.js if you ever wanted to use that).

  • Hello!

    Cool, I have an original Espruino (Rev 11.3b). I tried save() in the terminal yes. I swear this wasn't working yesterday but I think the code is on the board because when I've plugged in the micro usb the lights came on automatically. However, when I plug in a battery (9v) nothing happens. I'll update to use neopixel, thanks! And will try the on-board LEDs too! Thanks!

  • No problem - how are you connecting the battery to Espruino?

    The neopixel lights need between about 3 and 5v - not 9v - so if you've powered the lights off 9v too that's more than likely what's causing your problems?

  • Ahhh! That is why! Tried it with 2 AA and works great! Thanks Gordon!

  • Great, and no problem! To be honest you're probably going to get better battery life & performance with 3xAA batteries - that'll be around 4.5v which should be spot on for the LED strip.

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

Cannot save and run code stops when connected to a battery

Posted by Avatar for roppa @roppa

Actions