Sensor - lyzadanger/johnny-five GitHub Wiki
The Sensor
class constructs objects that represent a single analog Sensor attached to the physical board. This class is intentionally generic and will work well with many types of sensors, including:
- Linear Potentiometer (Slider)
- Rotary Potentiometer (Knob)
- Thermistor (Temperature)
- Flex Sensitive Resistor
- Pressure Sensitive Resistor
- Force Sensitive Resistor
- Hall Sensor
- Tilt Sensor
- Photoresistor
- Light Dependent Resistor
...And more, see examples
- pin A Number or String address for the Sensor pin (analog).
var sensor = new five.Sensor("A0");
TinkerKit:
// Attached to TinkerKit's "Input 0"
var sensor = new five.Sensor("I0");
- options An object of property parameters.
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 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 |
{
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: Voltage value scaled to a boolean. READONLY
raw: Voltage value (0-1023). READONLY
analog: Voltage reading _scaled_ to 8 bit values (0-255). READONLY
constrained: Voltage reading _constrained_ to 8 bit values (0-255). READONLY
scaled: Voltage reading scaled to user defined range. READONLY
value: Voltage reading, either raw or scaled. READONLY
}
var five = require("johnny-five"),
board = new five.Board();
board.on("ready", function() {
// Create a new `sensor` hardware instance.
var sensor = new five.Sensor("A0");
sensor.scale([ 0, 10 ]).on("data", function() {
console.log( this.value );
});
});
- 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 );
});
- 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");
// Voltage readings less then 100 will result in the value of the `boolean` property being false.
// Voltage readings greater then 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
});
-
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)