edge lab LIVE! starts Tuesday in Munich. Grab a workshop seat and take home the board you build on. Code FRIEND-HACKSTER saves 20%. () Learn more!
obniz developer team
Published

Move servo motor by voice recognition

Control a flag waver using speech recognition. If you say “I'm home!”, the flag with the word “Welcome back” moves.

BeginnerFull instructions provided1 hour666
Move servo motor by voice recognition

Things used in this project

Hardware components

obniz
Cambrian Robotics obniz
×1
Adafruit servo moter
×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" />

    <link
      rel="stylesheet"
      href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css"
    />
    <link rel="stylesheet" href="/css/starter-sample.css" />
    <script src="https://obniz.io/js/jquery-3.2.1.min.js"></script>

    <script src="https://unpkg.com/[email protected]/obniz.js"></script>

    <style>
      h1 {
        margin-top: 50px;
        text-align: center;
      }
    </style>
  </head>

  <body>
    <div id="obniz-debug"></div>
    <h1 id="voice"></h1>

    <script>
      const obniz = new Obniz("OBNIZ_ID_HERE");

      obniz.onconnect = async function() {
        const servo = obniz.wired("ServoMotor", { gnd: 0, vcc: 1, signal: 2 });
        SpeechRecognition =
          window.webkitSpeechRecognition || window.SpeechRecognition;
        const recognition = new SpeechRecognition();
      
        recognition.onresult = async event => {
          document.getElementById("voice").innerHTML = event.results[0][0].transcript;
          let angle_list = [40, 140, 40, 140];
          if (event.results[0][0].transcript == "I'm back") {
            for (let i = 0; i < angle_list.length; i++) {
              servo.angle(angle_list[i]);
              await obniz.wait(1000);
            }
          }
        };
        recognition.start();
      };

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

Credits

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

Comments