Welcome to Hackster!
Hackster is a community dedicated to learning hardware, from beginner to pro. Join us, it's free!
obniz developer team
Published

Change the number of LED’s that glow according to the slider

The number of LEDs on the tape LED will change according to the position of the slider on the web page.

BeginnerFull instructions provided1 hour200

Story

Read more

Code

source code

HTML
<!DOCTYPE html>
<html>
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <script
      src="https://unpkg.com/obniz@3.x/obniz.js"
      crossorigin="anonymous"
    ></script>
    <script
      src="https://code.jquery.com/jquery-3.4.1.js"
      integrity="sha256-WpOohJOqMqqyKL9FccASB9O0KwACQJpFTUBLTYOVvVU="
      crossorigin="anonymous"
    ></script>
    <link
      rel="stylesheet"
      href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.0/css/bootstrap.min.css"
      integrity="sha384-9gVQ4dYFwwWSjIDZnLEWnxCjeSWFphJiwGPXr1jddIhOegiu1FwO5qRGvFXOdJZ4"
      crossorigin="anonymous"
    />
    <title>Slider to Tape LED</title>
  </head>
  <body>
    <div class="container">
      <h2 class="text-center" style="margin:40px">Slider to Tape LED</h2>
      <div class="text-center">
        <input id="slider" type="range" value="0" min="0" max="8" step="1" />
      </div>
    </div>
    <script>
      $(() => {
        const WHITE = [255, 255, 255];
        const BLACK = [0, 0, 0];
        const NUM_OF_LEDS = 8;
        let obniz = new Obniz("OBNIZ_ID_HERE");
      
        obniz.onconnect = async () => {
          let leds = obniz.wired("WS2812", { gnd: 0, din: 1, vcc: 2 });
      
          $("#slider").change(async () => {
            let colorsArray = [];
            let numOfWhiteLeds = $("#slider").val();
      
            if (numOfWhiteLeds === "0") {
              for (let i = 0; i < NUM_OF_LEDS; i++) {
                await colorsArray.push(BLACK);
              }
            } else {
              for (let i = 0; i < numOfWhiteLeds; i++) {
                await colorsArray.push(WHITE);
              }
              for (let j = 0; j < NUM_OF_LEDS - numOfWhiteLeds; j++) {
                await colorsArray.push(BLACK);
              }
            }
            await leds.rgbs(colorsArray);
          });
        };
      });

    </script>
  </body>
</html>

Credits

obniz developer team
80 projects • 35 followers
Development board "obniz" is controlled via the internet.
Contact

Comments

Please log in or sign up to comment.