[Kids Project] Shake Animal!

Shake to move an animals !

Materials

  • obniz x1
  • power supply for obniz x1
  • DC Motor x1
  • paper cup x2
  • AAA battery x3
  • battery case x1
  • paper and wire for decorations

Steps

Body

First, make a hole at bottom of one paper cup.

Fix a motor to a cup with double-sided tape.

Make an animal with the other paper cup and color paper and so on freely.

Wriring

Connect DC Motor to an obniz like the table and the image below. Please adjust the direction of the DC Motors properly, because rotation direction depends on the DC Motors’ direction.

obniz DC Motor
0 OUT1
1 OUT2

Put AAA butteries in battery case to case and solder a cable to obniz J1. Be careful not to short circuit!

Finally, cover the other cup (decorated like animals) to the cup.

Run the program below and shake your smartphone! The stronger you shake it, the stronger the animals move.

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>

 

 

 

 

Recommended