Odd timing behavior

Posted on
  • So I decided to make a fruitmachine(but not according to the tutorial) and I'm struggling with some weird timing behavior.

    Basically, when I save my code, disconnect, unplug the power, plug the power back in OR when I re-upload my code, the timing is incorrect.
    I can fix it by resetting the Pico(Not just reset(), two seconds of button pressing), and then upload the code again.
    It also doesn't respond to my watches.

    Watch it here
    The first gif is the normal way, 50% on, 50% off like in the code. The second gif the timing division is weird but consistent(when the camera shakes that's when the gif jumps to its end, so skip that)

    How can I fix this?

    EDIT: woops forgot the code (m
    This is all the code, so it may contain irrelevant code, but I'm sure all relevant is there since it's all so that's why I paste it all.

    EDIT 2: If I connect it again and I want to upload the code, I get these errors:

    I practically can't do anything.

    Here's the code:

    var N_LEDS = 3;
    var BRIGHTNESS_FACTOR = 20;
    var arr = new Uint8ClampedArray(N_LEDS * 3/*3 leds * 3 channels*/);
    SPI2.setup({baud:3200000, mosi:B15});
    
    // Setup external button
    pinMode(B1, "input_pulldown");
    analogWrite(A7, 1);
    
    var preflashstate = false;
    
    function preFlash()
    {
      preflashstate = !preflashstate;
      if (preflashstate === true)
      {
        setAll(new Color(255, 255, 0));
      }
      else 
      {
        setAll(new Color(0, 0, 0));
      }
      updateLeds();
    }
    function LED2Flash()
    {
      LED2.write(!LED2.read());
    }
    
    function updateLeds()
    {
      var col = new Uint8ClampedArray(arr.length);
      for (var i = 0; i < arr.length; i++)
      {
        col[i] = arr[i] / BRIGHTNESS_FACTOR;
      }
      SPI2.send4bit(col, 0b0001, 0b0011);
    }
    
    function clearAll()
    {
      for (var i = 0; i < arr.length; i++)
      {
        arr[i] = 0;
      }
    }
    function setAll(color)
    {
      for (var i = 0; i < N_LEDS; i++)
      {
        color.setToLed(i);
      }
    }
    
    var Color = function(r,g,b)
    {
      this.r = r;
      this.g = g;
      this.b = b;
    };
    Color.prototype.setToLed = function(led)
    {
      led *= 3;
      //arr.splice(led, 3, this.r, this.g, this.b);
      arr[led    ] = this.r;
      arr[led + 1] = this.g;
      arr[led + 2] = this.b;
    };
    
    var SlotType = function(name, prize, color)
    {
      this.name = name;
      this.prize = prize;
      this.color = color;
    };
    var Wheel = function(index)
    {
      this.i = index;
    };
    
    var SYMBOLS = [
      new SlotType("Cherry", 20, new Color(255, 0, 0)),
      new SlotType("Lemon", 50, new Color(255, 255, 0)),
      new SlotType("Melon", 100, new Color(0, 153, 51)),
      new SlotType("7", 200, new Color(255, 2, 120)),
      new SlotType("Bar", 500, new Color(0, 255, 255)),
    ];
    
    function reset()
    {
      clearInterval();
      clearWatch();
      init();
    }
      
    function init()
    {
      setInterval(preFlash, 700, {repeat:true});
      setInterval(LED2Flash, 250, {repeat:true});
      
      setWatch(function(){
        clearInterval();
        clearWatch();
        clearAll();
        preflashstate = false;
        LED2.write(0);
        updateLeds();
      
        setWatch(reset, BTN, {repeat:true});
      
      }, B1, {repeat:true,edge:"rising"});
    }
    init();
    
  • You need an onInit() in which you stat the timers... save() is also saving the state of the machine, but only the 'static' (code and settings) part, but not the timer things. in the onInit() you put the init(). I did not fully analyze and understand your code, but with timings/timers that's usually the issue.

  • Interesting, but I don't think that's the issue. I changed onInit() to init(), but that didn't help.

  • When you save, timers should be stopped. Usually on upload, stuff starts to run and timers are on the go and state of variables are saved as well. I see that you have some reset() and other things going on. To have reset() redefined in your code may be an issue. Reset is a built-in function... Quick+Start. I do not know what redefining it in your code does to the system: override, not override,...

  • It works! It really works! Too bad I accidentally override reset(). Thanks!
    EDIT: Maybe a warning in the Web IDE when you override reset() or any of the core functions?

  • np. urvw :}

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

Odd timing behavior

Posted by Avatar for joppiesaus @joppiesaus

Actions