rwaldron / javascript-robotics

70 stars 43 forks source link

Exporting files Johnny-five #25

Open litwa995 opened 5 years ago

litwa995 commented 5 years ago

Hello This is led.js var five = require("johnny-five"); var board = new five.Board();

board.on("ready", function() { var led = new five.Led(13); var button = require("./button"); });

This is button.js var five = require("johnny-five"), board, button; board.on("ready", function() { var button = new five.Button(2); button.on("down", function() { led.on(); console.log("down"); }); }); module.exports = button;

Below I have an example program. I would like to export the button.js file (LED control) to the led.js file. So that the main code would be separate, in this case led.js, and control separately. How to export the file? And what best to export led.js to button.js, or button.js to led.js? Could someone in this example explain how to do it?

dtex commented 5 years ago

First, I'll say that trying to separate out a program this simple is more trouble than it's worth. I would just do everything in one file like:

var five = require("johnny-five");
var board = new five.Board();

board.on("ready", function() {
    var led = new five.Led(13);
    var button = new five.Button(2);
    button.on("down", function() {
        led.on();
        console.log("down");
    });
});

If you really want to instantiate your button in a separate file then you will have to pass in your johnny-five instance and your led instance within your board's "ready" handler. I'm not saying you should do this, but if you must:

led.js

var five = require("johnny-five");
var button = require("./button");
var board = new five.Board();

board.on("ready", function() {
    var led = new five.Led(13);
    button.init(five, led);
});

button.js

let button = {
    init: function(five, led) {
        var button = new five.Button(2);
        button.on("down", function() {
            led.on();
            console.log("down");
        });
    }
};
module.exports = button;

Note that I haven't tested any of this code. It's all off the cuff

I'd also argue that it makes more sense to require the led into button since the button is controlling things, but that also seems silly since the led instantiation would literally be just one line.

litwa995 commented 5 years ago

Hi What you sent me works. Thanks. :) This was just an example, it is known that it makes no sense to run such a small code. I want to use it for my phoenix hexapod. I build my own pad with josticks and tact switch. I don't want to add control to the program phoenix.js where there are animations, and I have them once more. I plan to write at least 50 :) I've set the minimum threshold for myself.

Below, he will send the code and photo with the current problem. Probably something innocent in the program. As they say, he learns from mistakes. ;)

pan-tilt.js

var five = require("johnny-five");
var joystick = require("./mypad");
var board = new five.Board();

board.on("ready", function() {
    var pan = new five.Servo({
        pin: 38,
        range: [17, 159],
        center: true
    });

    var tilt = new five.Servo({
        pin: 39,
        range: [17, 159],
        center: true
    });
    joystick.init(five, tilt, pan);
});

mypad.js

let joystick = {
  init: function(five, tilt, pan) {

    var joystick = new five.Joystick({
      pins: ["AO", "A1"],
    });
      joystick.on("change", function() {
      console.log("Left Joystick");
      tilt.to(five.Fn.scale(this.y, -1, 1));
      console.log(" y : ", this.y);
      pan.to(five.Fn.scale(this.x, -1, 1));
      console.log(" x : ", this.x);
      console.log("--------------------------------------");
    });
  }
};
module.exports = joystick;

joystick error `