2

Let's say I am connecting 2 Arduino boards to the computer and I want to use Johnny-five here. Each of boards is used to different tasks, for example one reads sensors, the other controls some LEDs. So it is important to me to read/write signals to appropriate boards.

I am looking for some flexibility here, because I found out that:

Here I don't know which board got key A and which B, and I can't guarantee that keys will not be opposite when I connect my arduinos to another machine:

new five.Boards([ "A", "B" ]);

Here I know exactly which board is connected to which port, but I can't hardcode it if I am planning to connect boards to another machine:

new five.Boards([ "/dev/cu.usbmodem621", "/dev/cu.usbmodem411" ]);

The only idea I have for now is to use a kind of jumpers, by wiring for example pin10 to +5V on board 1, and to the ground on board 2, or even use resistors and have many signal levels (if I plan to connect more boards), then probe the pin and just get info which board I am connected to and assign it to A or B in the array. After that I would run the main code with my program.

My question is: Do you see any other approach giving you guarantee that you "talk" to correct board?

4

1 回答 1

2

Option 1: Use dip switch to set device ID

use a 2 or 4 switch dip switch to identify the board. Each switch could go to an individual Digital input.

The more switches, the more boards you can have

  • 1 bit - 2 boards
  • 2 bit - 4 boards
  • 4 bit - 16 boards
  • 8 bit - 256 boards

In a 2 bit configuration,

  • 00 - Board A
  • 01 - Board B
  • 10 - Board C
  • 11 - Board D

Option 2: Modify Firmata and hardcode a device ID

Extend and customize firmata firmware. I've determined that this isn't a very good option.

Option 3: Use filename/firmware name

I got a lot of good advice on this from the Johnny-Five & Frimata groups. Johnny Five uses the file name for the firmware name, which can be accessed by the board object in Johnny-Five.

firmware: { version: [Object], name: 'AdvancedFirmata.ino' },

In my case, I just renamed the filename when I compile the AdvancedFirmata code and upload it to the device.

firmware: { version: [Object], name: 'boardA.ino' },

This is what I'm going to use in my project to identify different boards through board.io.firmware.name

This seems to be the best solution.

Update: Here is a complete example. In my case, I made things a lot more configurable, but this will work:

var boards = new five.Boards("A","B");
var j5 = {}
boards.on("ready", function(){
  this.each(function(board){

    // Set up LED on board B
    if(board.io.firmware.name == "BoardA.ino"){
      j5.ledA = new five.Led({
        pin: 13,
        board: board
      });
    }

    // Set up LED on board B
    else if (board.io.firmware.name == "BoardB.ino"){
      j5.ledB = new five.Led({
        pin: 13,
        board: board
      });
    }


  });
});

Now you can do:

// Toggle LED A every 500ms
setInterval(function(){
  j5.ledA.toggle();
},500); 

// Toggle LED B every 250ms
setInterval(function(){
  j5.ledB.toggle();
},250); 
于 2016-01-11T02:42:23.050 回答