The DC motor can be turned or stopped by voice recognition.
Materials- DC motor(I used the gear motor that comes with the AI Robot Kit here.)
- obniz Board
- Mobile batteries, etc.
- PC or Android smartphone
- Chrome
- (It’s easier to see the rotation if you have a tire or flag.)
Please note that the Web Speech API used in this article’s program does not support speech recognition in most browsers other than Chrome. In addition, the iOS version of the Chrome app is not supported, so please use Android when using a smartphone.
How to makeHardware connection
Connect the motor to the obniz board as shown in the table, referring to the DC motor parts library.
obniz DC motor 0 OUT1 (either one of them). 1 OUT2 (either one of them).
Program<!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>
ExcutePress RECODE to record until you press STOP. In the meantime, please talk to them.
The corresponding phrases are as follows.
- 「Turn it around.」 → The DC motor spins.
- 「Stop it.」 → The DC motor stops rotating.
If a pop-up message appears asking for permission to use the microphone, press “Allow”.
What is obniz?Before we get into the project, let's look into obniz.
Here → obniz for DIY electronics
obniz is a cloud-connected IoT development board. You can program on the web browser of any smartphone or computer and the command is sent to obniz through the internet via obniz cloud. By connecting the obniz to the cloud through wifi, users can remotely control devices that are physically connected to obniz.
Thanks to this cloud based approach, you can program with Python, Javascript, or other languages you prefer and control hardware directly. You don't need to integrate firmware into the device side. Recording and analyzing data is also easy with obniz cloud service.
Want to control hardware things with your current Python or Javascript skill? Want to start IoT project but don't know where to start? Want to learn programming with languages you prefer?
obniz will help you broaden your viewpoint and develop both your SW and HW skills.
For more information, please visit our official website → Official Website
Where to get obniz board? → Amazon /Official Store
Comments
Please log in or sign up to comment.