-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Sonar
Scott González edited this page Nov 10, 2015
·
14 revisions
The Sonar
class constructs objects that represent a single analog or I2C Sonar sensor attached to the physical board. This class works with:
-
Maxbotix Analog Sonar
- Any
-
Devantech I2C Sonar
- SRF02
- SRF08
- SRF10
This list will continue to be updated as more Sonar devices are confirmed.
NOTE: The Sonar
class is deprecated. Please use the Proximity class.
-
pin A String address for the Sonar pin (analog only).
-
options An object of property parameters.
Property Type Value/Description Default Required pin Number, String Any pin. The Number or String address of the pin the sonar is attached to, ie. “A0” or “I1” yes (for analog) controller String “SRF02”, “SRF08”. A String model id of a Sonar device (I2C only), ie. “SRF02”, “SRF08”, “SRF10” yes (for I2C) freq Number Milliseconds. The frequency in ms of data events. 25ms no threshold Number Any. The change threshold (+/- value). 1 no
Property Name | Description | Read Only |
---|---|---|
id |
A user definable id value. Defaults to a generated uid | No |
pin |
The pin address that the Sonar is attached to | No |
cm |
Value of current reading in centimeters. | Yes |
inches |
Value of current reading in inches. | Yes |
value |
Value of current reading in cm. | No |
// Create an analog Sonar object:
//
// - attached to pin "A0"
//
new five.Sonar({
pin: "A0",
});
// Create an I2C Sonar object:
//
// - use device model "SRF10"
new five.Sonar({
controller: "SRF10"
});
var five = require("../lib/johnny-five.js");
var board = new five.Board();
board.on("ready", function() {
var sonar = new five.Sonar("A0");
sonar.on("data", function() {
console.log("inches : " + this.in);
console.log("centimeters: " + this.cm);
console.log("-----------------------");
});
});
- within(range[, unit], handler) When value is within the provided range, execute callback.
var sonar = new five.Sonar({
device: "SRF10"
});
sonar.within([ 5, 10 ], "inches", function() {
// This is called when the sonar detects an object 5-10 inches away
});
-
change The "change" event is emitted whenever the value of the sonar changes more then the threshold value allows.
-
data The "data" event is fired as frequently as the user defined
freq
will allow in milliseconds. ("data" replaced the "read" event)