forked from lyzadanger/jsot-johnny-five
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path06-compass.js
More file actions
34 lines (31 loc) · 972 Bytes
/
06-compass.js
File metadata and controls
34 lines (31 loc) · 972 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
const five = require('johnny-five');
const board = new five.Board();
const MAGNETIC_DECLINATION = 15; // Portland, Ore., approx. magnetic declination
const ALERT_THRESHOLD = 5; // How many degress from north should count as north?
function isNorth (heading) {
return (heading > 360 - ALERT_THRESHOLD ||
heading < 0 + ALERT_THRESHOLD);
}
/**
* Adjust `rawHeading` for local magnetic declination and then
* bound it between 0-360
*/
function adjustHeading (rawHeading) {
var adjustedHeading = rawHeading - MAGNETIC_DECLINATION;
if (adjustedHeading < 0) { adjustedHeading += 360; }
if (adjustedHeading >= 360) { adjustedHeading -= 360; }
return Math.floor(adjustedHeading);
}
board.on('ready', () => {
const compass = new five.Compass({
controller: 'HMC5883L'
});
const piezo = new five.Piezo({
pin: 11
});
compass.on('data', function () {
if (isNorth(adjustHeading(this.heading))) {
piezo.frequency(587, 500);
}
});
});