rwaldron / johnny-five

JavaScript Robotics and IoT programming framework, developed at Bocoup.
http://johnny-five.io
Other
13.3k stars 1.77k forks source link

five.Buttons is triggering all buttons #1289

Closed willmendesneto closed 7 years ago

willmendesneto commented 7 years ago

I'm using johnny-five buttons, but testing the code based on documentation example I realise that I can't manage only one buttons per time. The current buttons instance is triggering the events in all buttons.

About the setup

About the code

The used code is:

var five = require("johnny-five"),
  board;

board = new five.Board();

board.on("ready", function() {
  var buttons = new five.Buttons({
    pins: [2, 4],
    invert: true,
  });

  buttons.on("press", function(button) {
    console.log("Pressed: ", button.pin);
  });

  buttons.on("release", function(button) {
    console.log("Released: ", button.pin);
  });
});

And the output is

screen shot 2017-02-11 at 12 23 25 pm

@rwaldron, that's the correct approach or in this scenario we should change the approach and add this in Johnny-Five documentation?

fivdi commented 7 years ago

If I understand the requirement correctly, if button-n is pressed and a "press" event is emitted for button-n, then no events should be emitted for any other button before button-n is released and it's "release" event is emitted. Is this correct?

willmendesneto commented 7 years ago

Not. The main goal with that is share if the button is pressed really or not. How we are using events to handle that, every single button will receive this event in broadcast, but I need to know which button is exactly pressed in that moment.


board.on("ready", function() {
  var buttons = new five.Buttons({
    pins: [2, 4],
    invert: true,
  });

  buttons.on("press", function(button) {
    // how to know which button exactly was pressed? 
    console.log("Pressed: ", button.pin);
  });

  buttons.on("release", function(button) {
    // how to know which button exactly was released?
    console.log("Released: ", button.pin);
  });
});
dtex commented 7 years ago

I'm away from my hardware at the moment, but have you tried something like this:

 buttons[0].on("press", function(button) {
    // how to know which button exactly was pressed? 
    console.log("The first button was pressed");
  });

 buttons[1].on("press", function(button) {
    // how to know which button exactly was pressed? 
    console.log("The second button was pressed");
  });
willmendesneto commented 7 years ago

Yes @dtex and how we are talking about the same event, both are triggered. I already compare the information inside button, but for now, I can't find anything saying that it's activate or not.

dtex commented 7 years ago

If you want to manage them separately couldn't you just use two separate button instances:

var five = require("johnny-five"),
  board;

board = new five.Board();

board.on("ready", function() {
  var leftButton = new five.Button({
    pins: 2,
    invert: true,
  });

  var rightButton = new five.Button({
    pins: 4,
    invert: true,
  });

  leftButton.on("press", function(button) {
    console.log("Left button pressed");
  });

  rightButton.on("press", function(button) {
    console.log("Right button pressed");
  });
});
soundanalogous commented 7 years ago

It sounds like @willmendesneto is saying that when he presses a single button, all buttons fire an event. This sounds like either an issue in the five.Buttons module, or an issue in the user's circuit. Will, are you using internal pull-up resistors or external pull-up or pull-down resistors?

willmendesneto commented 7 years ago

@dtex I tried that one before use this approach and I had the same issue as well. As I said, the buttons are being triggered using the same event, which means that it doesn't matter if I use five.Buttons to handle with all buttons or create an instance per each button using five.Button, both didn't work.

willmendesneto commented 7 years ago

It sounds like @willmendesneto is saying that when he presses a single button, all buttons fire an event. This sounds like either an issue in the five.Buttons module, or an issue in the user's circuit.

@soundanalogous It's exactly that. I guess it's something related to Johnny-Five really since I'm using the same buttons and resistors as fritzing diagram added in johnny-five docs. If we can send in the event some parameter showing that the button is active or not (or even inside button instance) should solve this issue.

fivdi commented 7 years ago

@willmendesneto I just tried this with an Arduino UNO and there were no issues. Everything worked as expected. Both buttons were connected as shown here which is the same as the fritzing you link to above. One change was however made to the program:

var five = require("johnny-five"),
  board;

board = new five.Board();

board.on("ready", function() {
  var buttons = new five.Buttons({
    pins: [2, 4],
//    invert: true,
  });

  buttons.on("press", function(button) {
    console.log("Pressed: ", button.pin);
  });

  buttons.on("release", function(button) {
    console.log("Released: ", button.pin);
  });
});

Note that invert: true was commented out. It doesn't make sense to use invert: true if the buttons are connected to the Arduino as showh here as it will result in unauthentic press events being emitted when the program starts without the buttons actually being pressed. If invert: true is used, the output of the program at startup will look something like this although the buttons were not pressed:

1488900612858 Device(s) /dev/ttyACM0  
1488900612881 Connected /dev/ttyACM0  
1488900616577 Repl Initialized  
>> Pressed:  2
Pressed:  4

If removing invert: true doesn't resolve the issue for you please post some photos of the circuit. This will help to figure out what the problem is.

fivdi commented 7 years ago

@willmendesneto any news on this one?

rwaldron commented 7 years ago

Before any more software trouble shooting can be done, we need to see a clear picture of the actual button circuit in use. I've just now wired up two buttons and ran your exact code and it worked exactly as it should—so I suspect there is something amiss in the circuit.

willmendesneto commented 7 years ago

@fivdi @rwaldron I will take a look and if still not working I'll add specific information about the buttons and resistors. I can send it now, but I will give you some feedback in few hours

willmendesneto commented 7 years ago

Closing this issue right now. Thanks guys!