obniz developer team
Published

Kids Project: Smartphone Cheep! Cheep!

Create a board and control it from your smartphone!

BeginnerFull instructions provided1 hour440
Kids Project: Smartphone Cheep! Cheep!

Things used in this project

Hardware components

obniz
Cambrian Robotics obniz
×1
Servo motor SG5010
×1
Speaker, Piezo
Speaker, Piezo
×1

Story

Read more

Code

Untitled file

HTML
<!-- HTML Example -->
<html>
<head>
 <script src="https://obniz.io/js/jquery-3.2.1.min.js"></script>
 <script src="https://unpkg.com/obniz@2.0.2/obniz.js"></script>
</head>
<body>
 <div id="obniz-debug"></div>
 <div id="print"></div>
<script>
var speaker;
var servo;
var lastbeta;
var obniz = new Obniz("OBNIZ_ID_HERE")
obniz.onconnect = async () => {
 speaker = obniz.wired("Speaker", {signal:0, gnd:4});
 servo = obniz.wired("ServoMotor", {signal:9,vcc:10, gnd:11});
}
window.addEventListener("deviceorientation", (event) => {
 if (!speaker) return;
 const beta = event.beta;
 const gamma = event.gamma;
 const alpha = event.alpha;
 console.log(gamma);
 if (beta > lastbeta + 1 || beta < lastbeta - 1 ){
   speaker.play(parseInt((beta - 0) * (7000 - 4000) / (90 - 0) + 4000));
 } else {
   speaker.stop();
 }
 lastbeta = beta;
 servo.angle(parseInt((gamma - (-80)) * (180 - 0) / (80 - (-80)) + 0));
})
</script>
</body>
</html> 

Credits

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

Comments