Skip to content
Rick Waldron edited this page Apr 24, 2015 · 41 revisions

The Sensor class constructs objects that represent a single analog sensor component attached to the physical board. This class is intentionally generic and will work well with many types of sensors, including (but not limited to):

  • Linear Potentiometer
  • Rotary Potentiometer
  • Flex Sensitive Resistor
  • Pressure Sensitive Resistor
  • Force Sensitive Resistor
  • Hall Sensor
  • Tilt Sensor
  • Photoresistor/Light Dependent Resistor

...And more, see examples

Parameters

  • pin A Number or String address for the Sensor pin (analog).

  • options An object of property parameters.

    Property Type Value(s) Description Required
    pin Number, String Any analog pin name The Number or String address of the pin the sensor is attached to, ie. “A0” or “I1” yes
    freq Number Milliseconds The frequency in ms of data events. Defaults to 25ms no
    threshold Number Any The change threshold (+/- value). Defaults to 1 no
  • options (experimental) These options can be used with the Sensor class, but are considered experimental.

    Property Type Value(s) Description Required
    type String "digital", "analog" Specify that this is a sensor attached to a digital pin. Allows using a digital pin as the value of the pin option no

Shape

{ 
  id: A user definable id value. Defaults to a generated uid
  pin: The pin address that the Sensor is attached to
  threshold: The change threshold (+/- value). Defaults to 1
  boolean: ADC value scaled to a boolean. READONLY
  raw: ADC value (0-1023). READONLY
  analog: ADC reading _scaled_ to 8 bit values (0-255). READONLY
  constrained: ADC reading _constrained_ to 8 bit values (0-255). READONLY
  value: ADC reading, scaled. READONLY
}

Component Initialization

Basic

var sensor = new five.Sensor("A0");

Common Options

//   - attached to pin "A0"
//   - emits data events every 250ms
//   - emits change events when the ADC value has changed by +5/-5
//
var temp = new five.Sensor({
  pin: "A0", 
  freq: 250, 
  threshold: 5
});

Usage

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

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

  var sensor = new five.Sensor("A0");

  sensor.scale([ 0, 10 ]).on("data", function() {
    console.log( this.value );
  });
});

API

  • scale(low, high) Scale the sensor's value to a new value within a specified range.

    var sensor = new five.Sensor("A0");
    
    sensor.scale(0, 180).on("change", function() {
      // this.value will reflect a scaling from 0-1023 to 0-180
      console.log( this.value );
    });
  • scale([low, high]) Same as scale(low, high).

  • booleanAt(barrier) Set a midpoint barrier value used to calculate returned value of the .boolean property. Defaults to 528

    var sensor = new five.Sensor("A0");
    
    // ADC readings less than 100 will result 
    //  in the value of the `boolean` property being false.
    // 
    // ADC readings greater than 100 will result 
    //  in the value of the `boolean` property being true.
    // 
    sensor.booleanAt(100);
  • within(range, handler) When value is within the provided range, execute callback.

    var sensor = new five.Sensor("A0");
    
    sensor.within([ 100, 200 ], function() {
      
      // This is called when the sensor's value property falls within 100-200
    
    });

Events

  • change The "change" event is emitted whenever the value of the sensor changes more then the threshold value allows. The "change" event has aliases that can be used to better reason about the program being written: "slide", "touch", "force", "bend".

  • data The "data" event is fired as frequently as the user defined freq will allow in milliseconds. ("data" replaced the deprecated "read" event)

Examples

Clone this wiki locally