Open pichlermi opened 6 years ago
Are you trying to read the value of a pin infrequently using analogRead or digitalRead, and do not want the callbacks there after?
I was trying to do that and made my code look as follows
function readPin(pinId) {
board.pinMode( pinId, board.MODES.INPUT );
board.digitalRead(pinId, function(value) {
console.log("The value of digital pin changed to: " + value);
board.reportDigitalPin(pinId, 0);
});
}
With this change, I got warning of maxEventListeners after calling the function readPin 10 times.
I think there is a need of analogReadOnce and digitalReadOnce type of functions. The code there can simply call "this.once" instead of "this.addListener" so that the above code would work and user would not need to tamper with the listeners directly.
Board.prototype.analogReadOnce = function(pin, callback) {
this.reportAnalogPin(pin, 1);
this.once("analog-read-" + pin, callback);
};
The log message is still written to the console, when Pin 2 is toggled.
When you observe this, do you mean that it logs once, or continuously? I can reproduce the behavior in which a single report still occurs, but not continuous reports.
@rwaldron Thanks. The commits that you made also help solve the problem. The only change I made to my code is moved the reportDigitalPin(pinId, 0) as the first thing to do in the callback.
function readPin(pinId) {
board.pinMode( pinId, board.MODES.INPUT );
board.digitalRead(pinId, function(value) {
board.reportDigitalPin(pinId, 0);
console.log("The value of digital pin changed to: " + value);
});
}
From the Documentation:
Reproduce the Error:
The log message is still written to the console, when Pin 2 is toggled.
Hack:
Now, no message is written.