Skip to content

Latest commit

 

History

History
74 lines (43 loc) · 1.12 KB

accelerometer-pan-tilt.md

File metadata and controls

74 lines (43 loc) · 1.12 KB

Accelerometer - Pan + Tilt

Run this example from the command line with:

node eg/accelerometer-pan-tilt.js
const { Accelerometer, Board, Servo, Servos } = require("johnny-five");
const board = new Board();

board.on("ready", () => {

  const range = [0, 170];

  // Servo to control panning
  const pan = new Servo({
    pin: 9,
    range
  });

  // Servo to control tilt
  const tilt = new Servo({
    pin: 10,
    range
  });

  // Accelerometer to control pan/tilt
  const accelerometer = new Accelerometer({
    pins: ["A3", "A4", "A5"],
    freq: 250
  });

  // Center all servos
  new Servos([pan, tilt]).center();

  accelerometer.on("acceleration", () => {
    tilt.to(Math.abs(Math.ceil(170 * accelerometer.pitch.toFixed(2)) - 180));
    pan.to(Math.ceil(170 * accelerometer.roll.toFixed(2)));
  });
});

 

License

Copyright (c) 2012-2014 Rick Waldron waldron.rick@gmail.com Licensed under the MIT license. Copyright (c) 2015-2023 The Johnny-Five Contributors Licensed under the MIT license.