The ReflectanceArray
class constructs an array of analog (digital arrays are not currently supported) reflectance sensors like the QTR-8A from Pololu.
Parameters
pins An array Numbers or Strings for the analog sensor pins.
emitter The pin where the emitter LED is attached. For these arrays, there is one pin that controls all of the emitters.
new five.ReflectanceArray({ emitter: 13, pins: ["A0", "A1", "A2", "A3", "A4", "A5"] });
options An object of property parameters.
Property Type Value/Description Default Required pins Array of Numbers, Strings ["A*", ...]
. The pins that the sensors are connected toyes emitter Number, String Digital Pin. The pin that the light emitter is connected to yes freq Number Milliseconds. The frequency in ms of data events. 25ms no
Shape
Property Name | Description | Read Only |
---|---|---|
id | A user definable id value. Defaults to a generated uid | No |
pins | The pin addresses that the sensors are attached to | No |
isOn | Are the emitters on? | Yes |
isCalibrated | Does the array have calibration data? | Yes |
isOnLine | Is the array currently tracking a line? isCalibrated===true | Yes |
sensors | An array of sensor objects that represent each sensor in the array. | Yes |
calibration | The current calibration data. | Yes |
raw | An array of raw values from all the sensors. | Yes |
values | If calibrated, an array of calibrated values (between 0 and 1000). If not calibrated, raw data (between 0 and 1023). | Yes |
line | If calibrated, a value between 0 and (n-1)*1000+1. For example, 6 sensors will have a line between 0 and 5001. | Yes |
Component Initialization
Analog
new five.ReflectanceArray({
emitter: 13,
pins: ["A0", "A1", "A2"], // any number of pins
freq: 25
});
Usage
const { Board, ReflectanceArray } = require("johnny-five");
const board = new Board();
board.on("ready", () => {
const eyes = new ReflectanceArray({
emitter: 13,
pins: ["A0", "A1", "A2"], // any number of pins
freq: 25
});
eyes.on('data', () => console.log("Raw Values: ", eyes.raw ));
eyes.on('line', () => console.log("Line Position: ", eyes.line));
eyes.enable();
});
API
enable() Turn the light emitters on.
const eyes = new five.ReflectanceArray({ emitter: 13, pins: ["A0", "A1"] }); eyes.enable();
disable() turn the light emitters off
const eyes = new five.ReflectanceArray({ emitter: 13, pins: ["A0", "A1"] }); eyes.disable();
calibrate() Read all of the sensors and store the min/max values. Used to calculate calibrated values and line values. You should call
calibrate()
multiple times.const eyes = new five.ReflectanceArray({ emitter: 13, pins: ["A0", "A1"] }); for (var i = 0; i < 100; i++) { eyes.calibrate(); }
calibrateUntil(predicate) A convenience function that will call calibrate until a predicate function returns true. This allows you to decide when calibration is done. This trigger may be user input, time, or execution count. You decide.
const eyes = new five.ReflectanceArray({ emitter: 13, pins: ["A0", "A1"] }); let calibrating = true; eyes.calibrateUntil(() => !calibrating); // calibrate for one second setTimeout(() => calibrating = false, 1000);
loadCalibration(calibration) Prime the array with calibration data. This allows you to load calibration data from a file. You can get it from the device using the
calibration
after it has been calibrated.const eyes = new five.ReflectanceArray({ emitter: 13, pins: ["A0", "A1"] }); eyes.loadCalibration({ min: [37, 39], max: [1010, 1015] });
Events
data The "data" event is fired as frequently as the user defined
freq
will allow in milliseconds. It includes an array of the RAW data from the sensors.calibratedData The "calibratedData" event is fired as frequently as the user defined
freq
will allow in milliseconds. It also requires that the device has been calibrated. It includes an array of calibrated values between0
and1000
.line The "line" event is fired as frequently as the user defined
freq
will allow in milliseconds. It also requires that the divide has been calibrated. It is a single value between0
and(n-1)*1000+1
. An array with 6 sensors will yield a value between0
and5001
.