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 color of the full-color LED with a radio button

You can change the color of the full color LED connected to the obniz with the radio button.

BeginnerFull instructions provided1 hour256

Things used in this project

Hardware components

obniz
Cambrian Robotics obniz
×1
full color led
×1

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>Radio Button to Fullcolor LED</title>
  </head>

  <body>
    <div class="container">
      <h2 class="text-center" style="margin:40px">Control Fullcolor LED</h2>
      <form id="rgb-controls" class="text-center row">
        <div class="col-xs-4 col-md-3"></div>
        <div id="choices" class="text-left col-xs-4 col-md-6">
          <input type="radio" name="colors" value="red" />red<br />
          <input type="radio" name="colors" value="green" />green<br />
          <input type="radio" name="colors" value="blue" />blue<br />
          <input type="radio" name="colors" value="gradation-change" />gradation
          change<br />
          <div class="text-center">
            <button id="change" class="btn btn-warning" style="margin:20px">
              change!!
            </button>
          </div>
        </div>
        <div class="col-xs-4 col-md-3"></div>
      </form>
    </div>
    <script>
      $(() => {
        const COLORS = {
          red: [255, 0, 0],
          green: [0, 255, 0],
          blue: [0, 0, 255]
        };
        let obniz = new Obniz("OBNIZ_ID_HERE");
      
        obniz.onconnect = async () => {
          let led = obniz.wired("FullColorLED", {
            r: 2,
            g: 0,
            b: 1,
            common: 3,
            commonType: "cathode_common"
          });
      
          $("#rgb-controls").submit(async e => {
            await e.preventDefault();
            let checkedVal = $('input[name="colors"]:checked').val();
      
            if (checkedVal === null || checkedVal === undefined) {
              return;
            } else if (checkedVal === "gradation-change") {
              led.gradation(1000);
            } else {
              led.stopgradation();
              led.rgb(
                COLORS[checkedVal][0],
                COLORS[checkedVal][1],
                COLORS[checkedVal][2]
              );
            }
          });
        };
      });
    </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.