Sonar - lyzadanger/johnny-five GitHub Wiki
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.
- pin A String address for the Sonar pin (analog only).
var sonar = new five.Sonar("A0");
- options An object of property parameters.
<tr>
<td>freq</td>
<td>Number</td>
<td>Milliseconds</td>
<td>The frequency in ms of data events. Defaults to 25ms</td>
<td>no</td>
</tr>
<tr>
<td>threshold</td>
<td>Number</td>
<td>Any</td>
<td>The change threshold (+/- value). Defaults to 1</td>
<td>no</td>
</tr>
Property Name | Type | Value(s) | Description | Required |
---|---|---|---|---|
pin | Number, String | "A0", "I1", 5 (Any pin on board) | The Number or String address of the pin the sonar is attached to, ie. "A0" or "I1" | yes (for analog) |
device | String | "SRF02", "SRF08" | A String model id of a Sonar device (I2C only), ie. "SRF02", "SRF08", "SRF10" | yes (for I2C) |
// Create an I2C Sonar object: // // - use device model "SRF08" // - emits data events every 250ms // var sonar = new five.Sonar({ device: "SRF08", freq: 250 });
### Shape
{ id: A user definable id value. Defaults to a generated uid pin: The pin address that the Sonar is attached to cm: Value of current reading in centimeters. READONLY inches: Value of current reading in inches. READONLY value: cm: Value of current reading. }
### Usage
```js
var five = require("johnny-five"),
board = new five.Board();
board.on("ready", function() {
// Create a new analog `sonar` hardware instance.
var sonar = new five.Sonar("A0");
sonar.on("data", function() {
console.log( this.cm );
});
});
- 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)