[Kids Project] Piyo Piyo from Smartphone

Create a Bird with motor and speaker, and control it from a Smartphone!

Materials

  • obniz x1
  • power supply for obniz x1
  • Servo Motor SG-5010 x1
  • speaker x1
  • Smartphone x1
  • paper cup x1
  • cardboard x1
  • paper and wire for decorations

Steps

Body

First, create base for a bird. Cut a circle from a cardboard and make a tiny hole for motor.

Next, fix it on top of a motor with double-sided tape.

Attach this servo hone to a servo motor.

Create a bird with cup, color paper and so on freely.

Then, put it on a servo.

fix a cable like this.

Wiring

Connect parts to an obniz like the table and the image below.

obniz parts
0 [speaker] signal
1 [speaker] GND
9 [servo motor] signal
10 [servo motor] Vcc
11 [servo motor] GND

Supply power to the obniz and run the program below. The bird will move depending on Smartphone movement.

If you tilt your Smartphone left and right, the bird will move to the tilting. If you tilt your Smartphone up and down, the bird change its chirping.

Program

<!-- 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:1});
  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>
 

 

 

Recommended