You are reading a single comment by @Gordon and its replies. Click here to read the full conversation.
  • LCD is actually built-in on the HY STM32 boards.

    I'm not sure I understand what you're doing - for each LED you're completely wiping the screen with fillRect for most patterns - so it's going to be insanely slow and almost certainly not what you want.

    If you want to see the lights on the screen, remove all the LCD calls you have, and change doLights to:

    function doLights() {
      getPattern();  
      for (var i=0;i<rgb.length;i+=3) {
        LCD.setColor(rgb[i+0]/256,rgb[i+1]/256,r­gb[i+2]/256);
        LCD.fillRect(i,0,i+2,10);
      }
      SPI2.send4bit(rgb, 0b0001, 0b0011);
    }
    

    Your actual problem is changePattern(). It actually works fine until you press the button?

    For some reason you've actually commented out the code that made it work - so unsurprisingly it's now broken.

    function changePattern() {
      patternNumber = (patternNumber+1);
      //% patterns.length;
      getPattern = patterns[patternNumber];
    }
    

    should be:

    function changePattern() {
      patternNumber = (patternNumber+1) % patterns.length;
      getPattern = patterns[patternNumber];
    }
    

    The percent is a 'modulo' - basically if patternNumber gets bigger than the number of patterns then it rolls over back to 0.

    Without it, patternNumber gets bigger, and you try and access something in the patterns array that doesn't exist, which gives you undefined.

    Finally, you probably also want to debounce the switch, so use:

    setWatch(changePattern, BTN, { repeat: true, edge:'rising', debounce: 50 });
    

    instead of just setWatch(changePattern, BTN, { repeat: true, edge:'rising' });

About

Avatar for Gordon @Gordon started