Parameters
-
pin A Number or String address for the Sensor pin (analog).
-
options An object of property parameters.
Property Type Value/Description Default Required pin Number, String Analog Pin. 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. 25ms no threshold Number Any. The change threshold (+/- value). 1 no Property Type Value/Description Default Required pin Number, String Analog Pin. 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. 25ms no threshold Number Any. The change threshold (+/- value). 1 no enabled boolean Whether to start emitting events right away (>= v0.9.12) true
no -
options (experimental) These options can be used with the
Sensor
class, but are considered experimental.Property Type Value/Description Default 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
optionno
Shape
Property Name | Description | Read Only |
---|---|---|
id | A user definable id value. Defaults to a generated uid | No |
pin | The pin address that the Sensor is attached to | No |
threshold | The change threshold (+/- value). Defaults to 1 | No |
boolean | ADC value scaled to a boolean. | Yes |
raw | ADC value (0-1023). | Yes |
analog | ADC reading scaled to 8 bit values (0-255). | Yes |
constrained | ADC reading constrained to 8 bit values (0-255). | Yes |
value | ADC reading, scaled. | Yes |
Property Name | Description | Read Only |
---|---|---|
id | A user definable id value. Defaults to a generated uid | No |
pin | The pin address that the Sensor is attached to | No |
threshold | The change threshold (+/- value). Defaults to 1 | No |
boolean | ADC value scaled to a boolean. | Yes |
raw | ADC value (0-1023). | Yes |
analog | ADC reading scaled to 8 bit values (0-255). | Yes |
constrained | ADC reading constrained to 8 bit values (0-255). | Yes |
value | ADC reading, scaled. | Yes |
freq | The rate in milliseconds to emit the data event. Disables the event if set to null . (>= v0.9.12) | No |
Component Initialization
Analog Sensor (Most common case)
new five.Sensor("A0");
API
-
scaleTo(low, high) (integer) Return the sensor's present value, scaled to a new value within the specified low/high range.
-
fscaleTo(low, high) (float) Return the sensor's present value, scaled to a new value within the specified low/high range.
var sensor = new five.Sensor("A0"); sensor.on("change", function() { // this.value will reflect a scaling from 0-1023 to 0-180 console.log(this.scaleTo(0, 180)); // integer console.log(this.fscaleTo(0, 180)); // float });
var sensor = new five.Sensor("A0"); sensor.on("change", () => { // this.value will reflect a scaling from 0-1023 to 0-180 console.log(sensor.scaleTo(0, 180)); // integer console.log(sensor.fscaleTo(0, 180)); // float });
-
scaleTo([low, high]) (integer) Return the sensor's present value, scaled to a new value within the specified low/high range.
-
fscaleTo([low, high]) (float) Return the sensor's present value, scaled to a new value within the specified low/high range.
var sensor = new five.Sensor("A0"); sensor.on("change", function() { // this.value will reflect a scaling from 0-1023 to 0-180 console.log(this.scaleTo([0, 180])); // integer console.log(this.fscaleTo([0, 180])); // float });
var sensor = new five.Sensor("A0"); sensor.on("change", () => { // this.value will reflect a scaling from 0-1023 to 0-180 console.log(sensor.scaleTo([0, 180])); // integer console.log(sensor.fscaleTo([0, 180])); // float });
-
scale(low, high) Scale the sensor's value to a new value within a specified range.
scale(...)
is deprecated, usescaleTo(...)
-
scale([low, high]) Same as
scale(low, high)
.scale(...)
is deprecated, usescaleTo(...)
-
booleanAt(barrier) Set a midpoint barrier value used to calculate returned value of the .boolean property. The barrier is based on the scaled value, not the raw value. Defaults to 50% (512 when unscaled).
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 });
-
enable() Start emitting "data" and "change" events, at a rate determined by the user defined
freq
in milliseconds. No-op if Sensor is already enabled. (>= v0.9.12) -
disable() Stop emitting "data" and "change" events. No-op if Sensor is already disabled. (>= v0.9.12)
Events
- change The "change" event is emitted whenever the value of the sensor changes more than the threshold value allows.
-
data The "data" event is fired as frequently as the user defined
freq
will allow in milliseconds. ("data" replaced the deprecated "read" event)