Skip to content
Rick Waldron edited this page Aug 16, 2013 · 10 revisions

The 'Ping' class constructs an object that represents a single sonar ping sensor.

Parameters

  • pin A Number or String address for the ping sensor

  • options An object of property parameters.

Property Name Type Value(s) Description Required
pin Number, String 9, "I1" (Any digital pin on board) The Number or String address of the pin the sensor is attached to, ie. 7 yes
freq Number Milliseconds The frequency in ms of read events. Defaults to 100ms no
pulse Number Milliseconds The frequency in ms of pulses. Defaults to 500ms no
```js // Create a Ping sensor... // // - attached to pin 7 // - emits data events every 250ms // var ping = new five.Ping({ pin: 7, freq: 250 }); ```

Shape

{
  pin: The pin address that the Ping Sensor is attached to.
  freq: The frequency of read events.
  pulse: The frequency of pulses.

  inches: The detected distance in inches. READONLY
  cm: The detected distance in centimetres. READONLY
}

Usage

var five = require("johnny-five"),
    board = new five.Board();

board.on("ready", function() {

  //Create new Ping and show distance on change
  var ping = new five.Ping(7);

  ping.on("change", function( err, value ) {

    console.log('Object is ' + this.cm + ' cm away');
    console.log('Object is ' + this.inches + ' inches away');
  });
});  

API

Ping does not have any API functions

Events

  • change The "change" event is emitted whenever the distance detected by the sensor 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)

Setup

Ping requires a specific version of firmata to be loaded onto the Arduino in order to work.

If you have the Arduino IDE open, you should close it before you start.

First get to the following directory with your Terminal or Git command line:

OS X: /Applications/Arduino.app/Contents/Resources/Java/libraries/

Linux: /usr/share/arduino/libraries/

Windows: C:\Program Files (x86)\Arduino\libraries\

Then proceed with the following in your command line:

    # make a backup of the existing Firmata
    cp -r Firmata Firmata_stable

    # rm Firmata
    rm -r Firmata

    # clone the experimental repo branch
    git clone git://github.com/jgautier/arduino-1.git Firmata

    # enter!
    cd Firmata

    # get the branch with pulseIn
    git checkout -b pulseIn origin/pulseIn

Now open the Arduino IDE.

  • File > Examples > Firmata > Standard Firmata

  • Once that's loaded into the IDE, confirm it is the correct program by searching for "PULSE_IN".

  • You should now be able to Compile and upload this version of Standard Firmata to your Arduino.

If you receive an error about PULSE_IN not being in scope, then copy this:

#define PULSE_IN                0x74 // send a pulse in command

And add it below the following line:

#define REGISTER_NOT_SPECIFIED -1

Examples

Clone this wiki locally