obniz developer team
Published

DC motor driven by voice recognition

If you say "spin", the DC motor will spin, and if you say "stop", it will stop.

IntermediateFull instructions provided1 hour204
DC motor driven by voice recognition

Things used in this project

Hardware components

obniz
Cambrian Robotics obniz
×1
Adafruit DCmoter
×1

Story

Read more

Code

Untitled file

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>Speech for Motor</title>
  </head>
  <body>
    <h2 class="text-center" style="margin:40px">Speech for Motor</h2>
    <div id="startstop-button" class="text-center">
      <button id="start" type="button" class="btn-lg btn-primary">
        record
      </button>
      <button
        id="stop"
        type="button"
        class="btn-lg btn-outline-secondary"
        disabled
      >
        stop
      </button>
    </div>
    <br />
    <div id="speech-script" class="text-center">
      It responds to the words "spin" or "stop.
    </div>
    <script>
      $(() => {
        let obniz = new Obniz("OBNIZ_ID_HERE");
        SpeechRecognition = webkitSpeechRecognition || SpeechRecognition;
        if (!"SpeechRecognition" in window) {
          alert("Your browser does not support voice recognition.");
          console.log("Your browser does not support voice recognition.");
          return;
        }
        const recognition = new SpeechRecognition();
        recognition.interimResults = true;
        recognition.continuous = true;
      
        obniz.onconnect = async () => {
          let motor = obniz.wired("DCMotor", { forward: 0, back: 1 });
      
          recognition.onresult = event => {
            let speechScript = "";
      
            for (let i = event.resultIndex; i < event.results.length; i++) {
              if (event.results[0].isFinal) {
                speechScript = event.results[0][0].transcript;
              }
            }
            if (speechScript.indexOf("spin") > -1) {
              motor.forward();
            } else if (speechScript.indexOf("stop") > -1) {
              motor.stop();
            }
          };
      
          $("#start").click(async e => {
            await recognition.start();
            $("#start").prop("disabled", true);
            $("#start").removeClass("btn-primary");
            $("#start").addClass("btn-outline-secondary");
            $("#stop").prop("disabled", false);
            $("#stop").removeClass("btn-outline-secondary");
            $("#stop").addClass("btn-danger");
          });
      
          $("#stop").click(async e => {
            await recognition.stop();
            $("#start").prop("disabled", false);
            $("#start").removeClass("btn-outline-secondary");
            $("#start").addClass("btn-primary");
            $("#stop").prop("disabled", true);
            $("#stop").removeClass("btn-danger");
            $("#stop").addClass("btn-outline-secondary");
          });
        };
      });

    </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.