Kids Project. Shake! Animal!

このエントリーをはてなブックマークに追加

Shake to move an animal!

Video

How to make

Material

  • Paper Cup
  • Motor
  • AAA Battery x3

First make a hole at bottom of a cup.

Fix a motor to a cup with double side tape.

Wire motor cable to obniz io0 and io1.

Put battery to case and solder a cable to obniz J1.

Pay attention to short circuit!

Last, use another cup to make an animal.

How to move

Run a program and shake a smartphone!

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 motor;
var obniz = new Obniz("OBNIZ_ID_HERE")
obniz.onconnect = async () => {

  motor = obniz.wired("DCMotor",  {forward:0, back:1});
  motor.power(0);
  motor.move(true);

}

var shakeCount = 0;
var x_last = 0;
var vx = 0;
var vx_last = 0;
var ax = 0;
var ax_avg = 0;
var timer;

window.addEventListener("devicemotion", (event) => {
  
  if (!motor)
    return;
  
  let x = event.accelerationIncludingGravity.x;
  let y = event.accelerationIncludingGravity.y;
  
  vx = x - x_last;
  x_last = x;
  ax = vx_last - vx;
  vx_last = vx;
  ax_avg = Math.abs(ax) * 0.2 + ax_avg * 0.8;
  
  if (!timer) {
    timer = setInterval(()=>{
      var value = ax_avg * 5;
      if(value > 50) {
        value = 50;
      }
      motor.power(value);
      timer = null;
    }, 50);
  }
}, true);


</script>
</body>
</html>

Run Now

The html will be opened to run a program.