RGB Led Project - Help

Posted on
Page
of 2
Prev
/ 2
  • This is what this code is doing, it isn't with the modifications you have showed me, i will send that later on. I want these bars wider but i want to make it cover the full screen. I can change the var rgb = new Uint8Array(50*3); 3 to 9 and it make full screen but i dont know if that will change my leds.


    1 Attachment

    • IMG_0351.JPG
  • @GarretL, your pic helps a lot.

    You may easily achieve using 300 of the 320 horizontal pixels with the following changes to your original code:

    Make line 12 of code in post #25 look like this:

    var xi = 6, xd = xi - 1;
    

    ...and line 204 like this:

      getPattern();  var x = 0;
    

    ...and line 204 like this:

      LCD.fillRect(x,0,x+xd,240);  x += xi;
    

    Using all of the 320 pixels is a bit a mess and may slow down the execution to the point where you cannot achieve 20 cycles per second anymore. But you may give it a try:

    Make line 12 of code in post #25 look like this:

    var xi = 320 / rgb.length, xd = xi - 1;
    

    ...and line 204 like this:

      getPattern();  var x = 0;
    

    ...and line 204 like this:

      x = i * xi; LCD.fillRect(Math.floor(x),0,Math.floor(­x+xd),240);
    

    Just for my curiosity - and help you with the generic code that maximizes dynamically the display size of the LEDs.

    A) Do you know the number pixels your display has horizontally? ...I assume it is 320.
    B) Do you know the number pixels your display has vertically? ...I assume it is 240.
    C) Do you know in which corner (x,y) = (0,0) is?

    The answer for C) is easy to find out. It should be sufficient to enter in the console pane of the Espruino Web IDE (left hand pane) the following line:

    LCD.setColor(1,0,0); LCD.fillRect(0,0,200,100);
    

    This draws and filled rectangle that is double as wide as high in the corner where 0,0 is.

    Looking forward to hear from you about - hopefully - success!

  • /*var sensor = require("HC-SR04").connect(C6,C7,functio­n(dist) {
      console.log(dist+" cm away");
    });
    setInterval(function() {
      sensor.trigger(); // send pulse
    }, 500);
    */
    LCD.clear();
    
    SPI2.setup({baud:3200000, mosi:B15});
    var rgb = new Uint8Array(50*3);
    //
    var xi = 6, xd = xi - 1;
    //
    var pos=0;
    
    function lightsOff() {
      clearInterval(cycle);
      pos++;
      for (var i=0;i<rgb.length;i+=3) {
        rgb[i  ] = 0;
        rgb[i+1] = 0;
        rgb[i+2] = 0;
      }
      SPI2.send4bit(rgb, 0b0001, 0b0011);
    }
    
    var patterns = [];
    //patterns.push(lightsOff);
    patterns.push(function() {
      // Fading white lights
      pos++;
      for (var i=0;i<rgb.length;i+=3) {
         var col = (Math.sin(i+pos*0.2)+1) * 127;
         rgb[i  ] = col;
         rgb[i+1] = col;
         rgb[i+2] = col;
      }
    });
    patterns.push(function() {
      // Fading Red lights
      pos++;
      for (var i=0;i<rgb.length;i+=3) {
         var col = (Math.sin(i+pos*0.2)+1) * 127;
         rgb[i  ] = col;
         rgb[i+1] = 0;
         rgb[i+2] = 0;
      }
    });
    patterns.push(function() {
      // Fading Green lights
      pos++;
      for (var i=0;i<rgb.length;i+=3) {
         var col = (Math.sin(i+pos*0.2)+1) * 127;
         rgb[i  ] = 0;
         rgb[i+1] = col;
         rgb[i+2] = 0;
      }
    });
    patterns.push(function() {
      // Fading Blue lights
      pos++;
      for (var i=0;i<rgb.length;i+=3) {
         var col = (Math.sin(i+pos*0.2)+1) * 127;
         rgb[i  ] = 0;
         rgb[i+1] = 0;
         rgb[i+2] = col;
      }
    });
    patterns.push(function() {
      // Low red light
      pos++;
      for (var i=0;i<rgb.length;i+=3) {
         var col = 85;
         rgb[i  ] = col;
         rgb[i+1] = 0;
         rgb[i+2] = 0;
      }
    });
    patterns.push(function() {
      // Medium red light
      pos++;
      for (var i=0;i<rgb.length;i+=3) {
         var col = 170;
         rgb[i  ] = col;
         rgb[i+1] = 0;
         rgb[i+2] = 0;
      }
    });
    patterns.push(function() {
      // Full red light 2
      pos++;
      for (var i=0;i<rgb.length;i+=3) {
         var col = 255;
         rgb[i  ] = col;
         rgb[i+1] = 0;
         rgb[i+2] = 0;
      }
    });
    patterns.push(function() {
      //  low blue light
      pos++;
      for (var i=0;i<rgb.length;i+=3) {
         var col = 85;
         rgb[i  ] = 0;
         rgb[i+1] = 0;
         rgb[i+2] = col;
      }
    });
    patterns.push(function() {
      // Med blue light
      pos++;
      for (var i=0;i<rgb.length;i+=3) {
         var col = 170;
         rgb[i  ] = 0;
         rgb[i+1] = 0;
         rgb[i+2] = col;
      }
    });
    patterns.push(function() {
      // Full blue light
      pos++;
      for (var i=0;i<rgb.length;i+=3) {
         var col = 255;
         rgb[i  ] = 0;
         rgb[i+1] = 0;
         rgb[i+2] = col;
      }
    });
    patterns.push(function() {
      //  low Green light
      pos++;
      for (var i=0;i<rgb.length;i+=3) {
         var col = 85;
         rgb[i  ] = 0;
         rgb[i+1] = col;
         rgb[i+2] = 0;
      }
    });
    patterns.push(function() {
      // Med Green light
      pos++;
      for (var i=0;i<rgb.length;i+=3) {
         var col = 170;
         rgb[i  ] = 0;
         rgb[i+1] = col;
         rgb[i+2] = 0;
      }
    });
    patterns.push(function() {
      // Full Green light
      pos++;
      for (var i=0;i<rgb.length;i+=3) {
         var col = 255;
         rgb[i  ] = 0;
         rgb[i+1] = col;
         rgb[i+2] = 0;
      }
    });
    patterns.push(function() {
      // Fading colours
      pos++;
      for (var i=0;i<rgb.length;i+=3) {
         rgb[i  ] = (1 + Math.sin((i+pos)*0.1324)) * 127;
         rgb[i+1] = (1 + Math.sin((i+pos)*0.1654)) * 127;
         rgb[i+2] = (1 + Math.sin((i+pos)*0.1)) * 127;
      }
    });
    patterns.push(function() {
      // Random White lights
      for (var i=0;i<rgb.length;i+=3) {
         rgb[i  ] = Math.random()*255;
         rgb[i+1] = Math.random()*255;
         rgb[i+2] = Math.random()*255;
      }
    });
    patterns.push(function() {
      // Random Red lights
      for (var i=0;i<rgb.length;i+=3) {
         rgb[i  ] = Math.random()*255;
         rgb[i+1] = 0;
         rgb[i+2] = 0;
      }
    });
    patterns.push(function() {
      // Random Green lights
      for (var i=0;i<rgb.length;i+=3) {
         rgb[i  ] = 0;
         rgb[i+1] = Math.random()*255;
         rgb[i+2] = 0;
      }
    });
    patterns.push(function() {
      // Random Blue lights
      for (var i=0;i<rgb.length;i+=3) {
         rgb[i  ] = 0;
         rgb[i+1] = 0;
         rgb[i+2] = Math.random()*255;
      }
    });
    var getPattern = patterns[0];
    function doLights() {
      //getPattern();
      //
      var xi = 6, xd = xi - 1;
      LCD.fillRect(x,0,x+xd,240);  x += xi;
      //
      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,240);
      }
      SPI2.send4bit(rgb, 0b0001, 0b0011);
    }
    
    var patternNumber = 0;
    function changePattern() {
      patternNumber = (patternNumber+1) % patterns.length;
      getPattern = patterns[patternNumber];
    }
    
    setWatch(changePattern, BTN, { repeat: true, edge:'rising', debounce: 50 });
    
    cycle = setInterval(doLights,50);
    

    The first set of changes showed nothing on the screen.


    1 Attachment

    • IMG_0357.JPG
  • /*var sensor = require("HC-SR04").connect(C6,C7,functio­n(dist) {
      console.log(dist+" cm away");
    });
    setInterval(function() {
      sensor.trigger(); // send pulse
    }, 500);
    */
    LCD.clear();
    
    SPI2.setup({baud:3200000, mosi:B15});
    var rgb = new Uint8Array(50*3);
    //
    var xi = 320 / rgb.length, xd = xi - 1;
    //
    var pos=0;
    
    function lightsOff() {
      clearInterval(cycle);
      pos++;
      for (var i=0;i<rgb.length;i+=3) {
        rgb[i  ] = 0;
        rgb[i+1] = 0;
        rgb[i+2] = 0;
      }
      SPI2.send4bit(rgb, 0b0001, 0b0011);
    }
    
    var patterns = [];
    //patterns.push(lightsOff);
    patterns.push(function() {
      // Fading white lights
      pos++;
      for (var i=0;i<rgb.length;i+=3) {
         var col = (Math.sin(i+pos*0.2)+1) * 127;
         rgb[i  ] = col;
         rgb[i+1] = col;
         rgb[i+2] = col;
      }
    });
    patterns.push(function() {
      // Fading Red lights
      pos++;
      for (var i=0;i<rgb.length;i+=3) {
         var col = (Math.sin(i+pos*0.2)+1) * 127;
         rgb[i  ] = col;
         rgb[i+1] = 0;
         rgb[i+2] = 0;
      }
    });
    patterns.push(function() {
      // Fading Green lights
      pos++;
      for (var i=0;i<rgb.length;i+=3) {
         var col = (Math.sin(i+pos*0.2)+1) * 127;
         rgb[i  ] = 0;
         rgb[i+1] = col;
         rgb[i+2] = 0;
      }
    });
    patterns.push(function() {
      // Fading Blue lights
      pos++;
      for (var i=0;i<rgb.length;i+=3) {
         var col = (Math.sin(i+pos*0.2)+1) * 127;
         rgb[i  ] = 0;
         rgb[i+1] = 0;
         rgb[i+2] = col;
      }
    });
    patterns.push(function() {
      // Low red light
      pos++;
      for (var i=0;i<rgb.length;i+=3) {
         var col = 85;
         rgb[i  ] = col;
         rgb[i+1] = 0;
         rgb[i+2] = 0;
      }
    });
    patterns.push(function() {
      // Medium red light
      pos++;
      for (var i=0;i<rgb.length;i+=3) {
         var col = 170;
         rgb[i  ] = col;
         rgb[i+1] = 0;
         rgb[i+2] = 0;
      }
    });
    patterns.push(function() {
      // Full red light 2
      pos++;
      for (var i=0;i<rgb.length;i+=3) {
         var col = 255;
         rgb[i  ] = col;
         rgb[i+1] = 0;
         rgb[i+2] = 0;
      }
    });
    patterns.push(function() {
      //  low blue light
      pos++;
      for (var i=0;i<rgb.length;i+=3) {
         var col = 85;
         rgb[i  ] = 0;
         rgb[i+1] = 0;
         rgb[i+2] = col;
      }
    });
    patterns.push(function() {
      // Med blue light
      pos++;
      for (var i=0;i<rgb.length;i+=3) {
         var col = 170;
         rgb[i  ] = 0;
         rgb[i+1] = 0;
         rgb[i+2] = col;
      }
    });
    patterns.push(function() {
      // Full blue light
      pos++;
      for (var i=0;i<rgb.length;i+=3) {
         var col = 255;
         rgb[i  ] = 0;
         rgb[i+1] = 0;
         rgb[i+2] = col;
      }
    });
    patterns.push(function() {
      //  low Green light
      pos++;
      for (var i=0;i<rgb.length;i+=3) {
         var col = 85;
         rgb[i  ] = 0;
         rgb[i+1] = col;
         rgb[i+2] = 0;
      }
    });
    patterns.push(function() {
      // Med Green light
      pos++;
      for (var i=0;i<rgb.length;i+=3) {
         var col = 170;
         rgb[i  ] = 0;
         rgb[i+1] = col;
         rgb[i+2] = 0;
      }
    });
    patterns.push(function() {
      // Full Green light
      pos++;
      for (var i=0;i<rgb.length;i+=3) {
         var col = 255;
         rgb[i  ] = 0;
         rgb[i+1] = col;
         rgb[i+2] = 0;
      }
    });
    patterns.push(function() {
      // Fading colours
      pos++;
      for (var i=0;i<rgb.length;i+=3) {
         rgb[i  ] = (1 + Math.sin((i+pos)*0.1324)) * 127;
         rgb[i+1] = (1 + Math.sin((i+pos)*0.1654)) * 127;
         rgb[i+2] = (1 + Math.sin((i+pos)*0.1)) * 127;
      }
    });
    patterns.push(function() {
      // Random White lights
      for (var i=0;i<rgb.length;i+=3) {
         rgb[i  ] = Math.random()*255;
         rgb[i+1] = Math.random()*255;
         rgb[i+2] = Math.random()*255;
      }
    });
    patterns.push(function() {
      // Random Red lights
      for (var i=0;i<rgb.length;i+=3) {
         rgb[i  ] = Math.random()*255;
         rgb[i+1] = 0;
         rgb[i+2] = 0;
      }
    });
    patterns.push(function() {
      // Random Green lights
      for (var i=0;i<rgb.length;i+=3) {
         rgb[i  ] = 0;
         rgb[i+1] = Math.random()*255;
         rgb[i+2] = 0;
      }
    });
    patterns.push(function() {
      // Random Blue lights
      for (var i=0;i<rgb.length;i+=3) {
         rgb[i  ] = 0;
         rgb[i+1] = 0;
         rgb[i+2] = Math.random()*255;
      }
    });
    var getPattern = patterns[0];
    function doLights() {
      //getPattern();
      getPattern();  var x = 0;
       x = i * xi; LCD.fillRect(Math.floor(x),0,Math.floor(­x+xd),240);
      
      // 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,240);
      
      SPI2.send4bit(rgb, 0b0001, 0b0011);
    }
    
    var patternNumber = 0;
    function changePattern() {
      patternNumber = (patternNumber+1) % patterns.length;
      getPattern = patterns[patternNumber];
    }
    
    setWatch(changePattern, BTN, { repeat: true, edge:'rising', debounce: 50 });
    
    cycle = setInterval(doLights,50);
    

    The second shows the same thing without the white thing in the corner. Yes you have the correct dimensions and i believe 0,0 is top left corner!

  • @GarrettL, thanks for showing the coded... because you inserted lines, it got a bit mangled because the line numbers changed... The reason that you do not see effects is that both 3rd changes ended up outside the loop and before the color is set instead of replacing your fillRect() in the loop after the color is set. Furthermore, because your code comes after the changes, it just overwrites what was before... and in the second example you commented oh¥-so-required the loop that's why you get just a white dot in the top left corner... ;-)

    Return to your code in post #25, and apply literally proposed changes.... and you will see it work. There is nothing wrong with your code in post #25, because it produced a big tall, fat horizontal bar from top to bottom - 240 pixels - consisting of 50 slim vertical bars - 3 pixels wide - for each LED. The proposed changes will just double that...

  • in post #27 you have two codes of line that you want me to sub for the same line and also i tried it and causes some errors!

  • If you could just copy the code and change them yourself it might be easier than explaining! Thank you

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

RGB Led Project - Help

Posted by Avatar for GarrettL @GarrettL

Actions