Skip to content

Commit 247734c

Browse files
committed
Examples: update Accelerometer programs syntax
1 parent bff8ebd commit 247734c

14 files changed

+174
-298
lines changed

docs/accelerometer-MMA8452.md

+7-10
Original file line numberDiff line numberDiff line change
@@ -29,16 +29,15 @@ node eg/accelerometer-MMA8452.js
2929

3030

3131
```javascript
32-
var five = require("johnny-five");
33-
var board = new five.Board();
32+
const {Accelerometer, Board} = require("johnny-five");
33+
const board = new Board();
3434

35-
board.on("ready", function() {
36-
37-
var accelerometer = new five.Accelerometer({
35+
board.on("ready", () => {
36+
const accelerometer = new Accelerometer({
3837
controller: "MMA8452"
3938
});
4039

41-
// accelerometer.on("change", function() {
40+
// accelerometer.on("change", () => {
4241
// console.log("accelerometer");
4342
// console.log(" x : ", this.x);
4443
// console.log(" y : ", this.y);
@@ -51,10 +50,8 @@ board.on("ready", function() {
5150
// console.log("--------------------------------------");
5251
// });
5352

54-
["tap", "tap:single", "tap:double"].forEach(function(event) {
55-
accelerometer.on(event, function() {
56-
console.log(event);
57-
});
53+
["tap", "tap:single", "tap:double"].forEach((event) => {
54+
accelerometer.on(event, () => console.log(event));
5855
});
5956
});
6057

docs/accelerometer-adxl335.md

+13-13
Original file line numberDiff line numberDiff line change
@@ -29,25 +29,25 @@ node eg/accelerometer-adxl335.js
2929

3030

3131
```javascript
32-
var five = require("johnny-five");
33-
var board = new five.Board();
32+
const {Accelerometer, Board} = require("johnny-five");
33+
const board = new Board();
3434

35-
board.on("ready", function() {
36-
var accelerometer = new five.Accelerometer({
35+
board.on("ready", () => {
36+
const accelerometer = new Accelerometer({
3737
controller: "ADXL335",
3838
pins: ["A0", "A1", "A2"]
3939
});
4040

41-
accelerometer.on("change", function() {
41+
accelerometer.on("change", () => {
4242
console.log("accelerometer");
43-
console.log(" x : ", this.x);
44-
console.log(" y : ", this.y);
45-
console.log(" z : ", this.z);
46-
console.log(" pitch : ", this.pitch);
47-
console.log(" roll : ", this.roll);
48-
console.log(" acceleration : ", this.acceleration);
49-
console.log(" inclination : ", this.inclination);
50-
console.log(" orientation : ", this.orientation);
43+
console.log(" x : ", accelerometer.x);
44+
console.log(" y : ", accelerometer.y);
45+
console.log(" z : ", accelerometer.z);
46+
console.log(" pitch : ", accelerometer.pitch);
47+
console.log(" roll : ", accelerometer.roll);
48+
console.log(" acceleration : ", accelerometer.acceleration);
49+
console.log(" inclination : ", accelerometer.inclination);
50+
console.log(" orientation : ", accelerometer.orientation);
5151
console.log("--------------------------------------");
5252
});
5353
});

docs/accelerometer-adxl345.md

+13-13
Original file line numberDiff line numberDiff line change
@@ -29,28 +29,28 @@ node eg/accelerometer-adxl345.js
2929

3030

3131
```javascript
32-
var five = require("johnny-five");
33-
var board = new five.Board();
32+
const {Accelerometer, Board} = require("johnny-five");
33+
const board = new Board();
3434

35-
board.on("ready", function() {
36-
var accelerometer = new five.Accelerometer({
35+
board.on("ready", () => {
36+
const accelerometer = new Accelerometer({
3737
controller: "ADXL345",
3838
// Optionally set the range to one of
3939
// 2, 4, 8, 16 (±g)
4040
// Defaults to ±2g
4141
// range: ...
4242
});
4343

44-
accelerometer.on("change", function() {
44+
accelerometer.on("change", () => {
4545
console.log("accelerometer");
46-
console.log(" x : ", this.x);
47-
console.log(" y : ", this.y);
48-
console.log(" z : ", this.z);
49-
console.log(" pitch : ", this.pitch);
50-
console.log(" roll : ", this.roll);
51-
console.log(" acceleration : ", this.acceleration);
52-
console.log(" inclination : ", this.inclination);
53-
console.log(" orientation : ", this.orientation);
46+
console.log(" x : ", accelerometer.x);
47+
console.log(" y : ", accelerometer.y);
48+
console.log(" z : ", accelerometer.z);
49+
console.log(" pitch : ", accelerometer.pitch);
50+
console.log(" roll : ", accelerometer.roll);
51+
console.log(" acceleration : ", accelerometer.acceleration);
52+
console.log(" inclination : ", accelerometer.inclination);
53+
console.log(" orientation : ", accelerometer.orientation);
5454
console.log("--------------------------------------");
5555
});
5656
});

docs/accelerometer-mma7361.md

+13-13
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,10 @@ node eg/accelerometer-mma7361.js
2929

3030

3131
```javascript
32-
var five = require("johnny-five");
33-
var board = new five.Board();
32+
const {Accelerometer, Board} = require("johnny-five");
33+
const board = new Board();
3434

35-
board.on("ready", function() {
35+
board.on("ready", () => {
3636
// --- Sleep Pin
3737
// The sleepPin is used to enable/disable the device and put it into sleep mode
3838
// You can also tie the sleep pin to high with a 10k resistor and omit
@@ -45,23 +45,23 @@ board.on("ready", function() {
4545
// initializations, you can omit the autoCalibrate and set the zeroV array
4646
// in the options instead
4747

48-
var accelerometer = new five.Accelerometer({
48+
const accelerometer = new Accelerometer({
4949
controller: "MMA7361",
5050
pins: ["A0", "A1", "A2"],
5151
sleepPin: 13,
5252
autoCalibrate: true
5353
});
5454

55-
accelerometer.on("change", function() {
55+
accelerometer.on("change", () => {
5656
console.log("accelerometer");
57-
console.log(" x : ", this.x);
58-
console.log(" y : ", this.y);
59-
console.log(" z : ", this.z);
60-
console.log(" pitch : ", this.pitch);
61-
console.log(" roll : ", this.roll);
62-
console.log(" acceleration : ", this.acceleration);
63-
console.log(" inclination : ", this.inclination);
64-
console.log(" orientation : ", this.orientation);
57+
console.log(" x : ", accelerometer.x);
58+
console.log(" y : ", accelerometer.y);
59+
console.log(" z : ", accelerometer.z);
60+
console.log(" pitch : ", accelerometer.pitch);
61+
console.log(" roll : ", accelerometer.roll);
62+
console.log(" acceleration : ", accelerometer.acceleration);
63+
console.log(" inclination : ", accelerometer.inclination);
64+
console.log(" orientation : ", accelerometer.orientation);
6565
console.log("--------------------------------------");
6666
});
6767
});

docs/accelerometer-mpu6050.md

+13-13
Original file line numberDiff line numberDiff line change
@@ -29,24 +29,24 @@ node eg/accelerometer-mpu6050.js
2929

3030

3131
```javascript
32-
var five = require("johnny-five");
33-
var board = new five.Board();
32+
const {Accelerometer, Board} = require("johnny-five");
33+
const board = new Board();
3434

35-
board.on("ready", function() {
36-
var accelerometer = new five.Accelerometer({
35+
board.on("ready", () => {
36+
const accelerometer = new Accelerometer({
3737
controller: "MPU6050"
3838
});
3939

40-
accelerometer.on("change", function() {
40+
accelerometer.on("change", () => {
4141
console.log("accelerometer");
42-
console.log(" x : ", this.x);
43-
console.log(" y : ", this.y);
44-
console.log(" z : ", this.z);
45-
console.log(" pitch : ", this.pitch);
46-
console.log(" roll : ", this.roll);
47-
console.log(" acceleration : ", this.acceleration);
48-
console.log(" inclination : ", this.inclination);
49-
console.log(" orientation : ", this.orientation);
42+
console.log(" x : ", accelerometer.x);
43+
console.log(" y : ", accelerometer.y);
44+
console.log(" z : ", accelerometer.z);
45+
console.log(" pitch : ", accelerometer.pitch);
46+
console.log(" roll : ", accelerometer.roll);
47+
console.log(" acceleration : ", accelerometer.acceleration);
48+
console.log(" inclination : ", accelerometer.inclination);
49+
console.log(" orientation : ", accelerometer.orientation);
5050
console.log("--------------------------------------");
5151
});
5252
});

docs/accelerometer-pan-tilt.md

+13-21
Original file line numberDiff line numberDiff line change
@@ -18,45 +18,37 @@ node eg/accelerometer-pan-tilt.js
1818

1919

2020
```javascript
21-
var five = require("johnny-five"),
22-
board;
21+
const {Accelerometer, Board, Servo, Servos} = require("johnny-five");
22+
const board = new Board();
2323

24-
board = new five.Board();
24+
board.on("ready", () => {
2525

26-
board.on("ready", function() {
27-
28-
var range, pan, tilt, accel;
29-
30-
range = [0, 170];
26+
const range = [0, 170];
3127

3228
// Servo to control panning
33-
pan = new five.Servo({
29+
const pan = new Servo({
3430
pin: 9,
35-
range: range
31+
range
3632
});
3733

3834
// Servo to control tilt
39-
tilt = new five.Servo({
35+
const tilt = new Servo({
4036
pin: 10,
41-
range: range
37+
range
4238
});
4339

4440
// Accelerometer to control pan/tilt
45-
accel = new five.Accelerometer({
41+
const accelerometer = new Accelerometer({
4642
pins: ["A3", "A4", "A5"],
4743
freq: 250
4844
});
4945

5046
// Center all servos
51-
(five.Servos()).center();
52-
53-
accel.on("acceleration", function() {
54-
// console.log( "acceleration", this.axis );
55-
56-
tilt.to(Math.abs(Math.ceil(170 * this.pitch.toFixed(2)) - 180));
57-
pan.to(Math.ceil(170 * this.roll.toFixed(2)));
47+
new Servos([pan, tilt]).center();
5848

59-
// TODO: Math.abs(v - 180) as inversion function ?
49+
accelerometer.on("acceleration", () => {
50+
tilt.to(Math.abs(Math.ceil(170 * accelerometer.pitch.toFixed(2)) - 180));
51+
pan.to(Math.ceil(170 * accelerometer.roll.toFixed(2)));
6052
});
6153
});
6254

docs/accelerometer.md

+15-66
Original file line numberDiff line numberDiff line change
@@ -29,24 +29,12 @@ node eg/accelerometer.js
2929

3030

3131
```javascript
32-
var five = require("johnny-five"),
33-
board, accel;
32+
const {Accelerometer, Board} = require("johnny-five");
33+
const board = new Board();
3434

35-
board = new five.Board();
35+
board.on("ready", () => {
3636

37-
board.on("ready", function() {
38-
39-
// Create a new analog `Accelerometer` hardware instance.
40-
//
41-
// five.Accelerometer([ x, y[, z] ]);
42-
//
43-
// five.Accelerometer({
44-
// pins: [ x, y[, z] ]
45-
// freq: ms
46-
// });
47-
//
48-
49-
accel = new five.Accelerometer({
37+
const accelerometer = new Accelerometer({
5038
pins: ["A3", "A4", "A5"],
5139

5240
// Adjust the following for your device.
@@ -56,56 +44,17 @@ board.on("ready", function() {
5644
zeroV: 478 // volts in ADC
5745
});
5846

59-
// Accelerometer Event API
60-
61-
62-
// "data"
63-
//
64-
// Fires when X, Y or Z has changed.
65-
//
66-
// The first argument is an object containing raw x, y, z
67-
// values as read from the analog input.
68-
//
69-
accel.on("data", function(data) {
70-
71-
console.log("raw: ", data);
72-
});
73-
74-
// "acceleration"
75-
//
76-
// Fires once every N ms, equal to value of freg
77-
// Defaults to 500ms
78-
//
79-
accel.on("acceleration", function(data) {
80-
81-
console.log("acceleration", data);
82-
});
83-
84-
// "orientation"
85-
//
86-
// Fires when orientation changes
87-
//
88-
accel.on("orientation", function(data) {
89-
90-
console.log("orientation", data);
91-
});
92-
93-
// "inclination"
94-
//
95-
// Fires when inclination changes
96-
//
97-
accel.on("inclination", function(data) {
98-
99-
console.log("inclination", data);
100-
});
101-
102-
// "change"
103-
//
104-
// Fires when X, Y or Z has changed
105-
//
106-
accel.on("change", function(data) {
107-
108-
console.log("change", data);
47+
accelerometer.on("change", () => {
48+
console.log("accelerometer");
49+
console.log(" x : ", accelerometer.x);
50+
console.log(" y : ", accelerometer.y);
51+
console.log(" z : ", accelerometer.z);
52+
console.log(" pitch : ", accelerometer.pitch);
53+
console.log(" roll : ", accelerometer.roll);
54+
console.log(" acceleration : ", accelerometer.acceleration);
55+
console.log(" inclination : ", accelerometer.inclination);
56+
console.log(" orientation : ", accelerometer.orientation);
57+
console.log("--------------------------------------");
10958
});
11059
});
11160

eg/accelerometer-MMA8452.js

+7-10
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
1-
var five = require("../lib/johnny-five.js");
2-
var board = new five.Board();
1+
const {Accelerometer, Board} = require("../lib/johnny-five.js");
2+
const board = new Board();
33

4-
board.on("ready", function() {
5-
6-
var accelerometer = new five.Accelerometer({
4+
board.on("ready", () => {
5+
const accelerometer = new Accelerometer({
76
controller: "MMA8452"
87
});
98

10-
// accelerometer.on("change", function() {
9+
// accelerometer.on("change", () => {
1110
// console.log("accelerometer");
1211
// console.log(" x : ", this.x);
1312
// console.log(" y : ", this.y);
@@ -20,9 +19,7 @@ board.on("ready", function() {
2019
// console.log("--------------------------------------");
2120
// });
2221

23-
["tap", "tap:single", "tap:double"].forEach(function(event) {
24-
accelerometer.on(event, function() {
25-
console.log(event);
26-
});
22+
["tap", "tap:single", "tap:double"].forEach((event) => {
23+
accelerometer.on(event, () => console.log(event));
2724
});
2825
});

0 commit comments

Comments
 (0)