Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

board.reportDigitalPin(digitalPinNumber, 0) has no effect #185

Open
pichlermi opened this issue Mar 2, 2018 · 3 comments
Open

board.reportDigitalPin(digitalPinNumber, 0) has no effect #185

pichlermi opened this issue Mar 2, 2018 · 3 comments

Comments

@pichlermi
Copy link

pichlermi commented Mar 2, 2018

From the Documentation:

To stop reporting digital values for a pin, call board.reportDigitalPin(digitalPinNumber, 0). To restart, call digitalRead(pin,callback) or use board.reportDigitalPin(digitalPinNumber, 1) if you don't want to call digitalRead again. <<

Reproduce the Error:

board.pinMode( 2, board.MODES.INPUT );
board.digitalRead(2, function(value) {
  console.log("The value of digital pin 2 changed to: " + value);
});
board.reportDigitalPin(2, 0);

The log message is still written to the console, when Pin 2 is toggled.

Hack:

board.pinMode( 2, board.MODES.INPUT );
cb = function(value) {
  console.log("The value of digital pin 2 changed to: " + value);
}
board.digitalRead(2, cb);
board.removeListener( "digital-read-2", cb )

Now, no message is written.

@lilliesAndRoses
Copy link

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);
};

@rwaldron
Copy link
Collaborator

rwaldron commented Apr 5, 2018

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.

@lilliesAndRoses
Copy link

@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);
 });
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants