Welcome to Hackster!
Hackster is a community dedicated to learning hardware, from beginner to pro. Join us, it's free!
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 hour625
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/obniz@3.x/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 • 35 followers
Development board "obniz" is controlled via the internet.
Contact

Comments

Please log in or sign up to comment.